Просмотр исходного кода

refactor: TdEngineService 代码清理

mqy20260511
humanleft 2 недель назад
Родитель
Сommit
e1532193d2
1 измененных файлов: 9 добавлений и 24 удалений
  1. 9
    24
      iot-platform/src/main/java/com/iot/platform/service/TdEngineService.java

+ 9
- 24
iot-platform/src/main/java/com/iot/platform/service/TdEngineService.java Просмотреть файл

125
         if (name == null || name.isEmpty()) {
125
         if (name == null || name.isEmpty()) {
126
             return "`unknown`";
126
             return "`unknown`";
127
         }
127
         }
128
-        return "`" + name.replaceAll("`", "") + "`";
128
+        return "`" + name.replace("`", "") + "`";
129
     }
129
     }
130
 
130
 
131
     private boolean isValidFieldName(String name) {
131
     private boolean isValidFieldName(String name) {
136
         return name != null && name.matches(ALLOWED_TABLE_NAME);
136
         return name != null && name.matches(ALLOWED_TABLE_NAME);
137
     }
137
     }
138
 
138
 
139
-    private boolean isNumeric(String str) {
140
-        if (str == null || str.isEmpty()) {
141
-            return false;
142
-        }
143
-        try {
144
-            Double.parseDouble(str);
145
-            return true;
146
-        } catch (NumberFormatException e) {
147
-            return false;
148
-        }
149
-    }
150
-
151
     // === 缓存工具方法 ===
139
     // === 缓存工具方法 ===
152
     private String getStableKey(String dbName, String stableName) {
140
     private String getStableKey(String dbName, String stableName) {
153
         return dbName + "." + stableName;
141
         return dbName + "." + stableName;
217
      * 从数据库加载列类型
205
      * 从数据库加载列类型
218
      */
206
      */
219
     private Map<String, String> loadStableColumnTypesFromDB(String dbName, String stableName) {
207
     private Map<String, String> loadStableColumnTypesFromDB(String dbName, String stableName) {
208
+
220
         Map<String, String> columnTypes = new HashMap<>();
209
         Map<String, String> columnTypes = new HashMap<>();
221
         String sql = String.format("DESCRIBE %s.%s", wrapName(dbName), wrapName(stableName));
210
         String sql = String.format("DESCRIBE %s.%s", wrapName(dbName), wrapName(stableName));
222
         try (Connection conn = getConnection();
211
         try (Connection conn = getConnection();
286
             return;
275
             return;
287
         }
276
         }
288
 
277
 
289
-//        String superTableName = extractSuperTableName(table);
290
-
291
         int batchSize = DEFAULT_BATCH_SIZE;
278
         int batchSize = DEFAULT_BATCH_SIZE;
292
         for (int i = 0; i < dataList.size(); i += batchSize) {
279
         for (int i = 0; i < dataList.size(); i += batchSize) {
293
             List<Map<String, Object>> batch = dataList.subList(i, Math.min(i + batchSize, dataList.size()));
280
             List<Map<String, Object>> batch = dataList.subList(i, Math.min(i + batchSize, dataList.size()));
297
         log.info("批量写入成功: {} | 条数: {}", table, dataList.size());
284
         log.info("批量写入成功: {} | 条数: {}", table, dataList.size());
298
     }
285
     }
299
 
286
 
300
-    private String extractSuperTableName(String table) {
301
-        int idx = table.lastIndexOf('_');
302
-        return idx > 0 ? table.substring(0, idx) : table;
303
-    }
304
-
305
     /**
287
     /**
306
      * 收集数据中所有动态列及其类型
288
      * 收集数据中所有动态列及其类型
307
      */
289
      */
308
     private Map<String, String> collectColumnTypes(List<Map<String, Object>> dataList) {
290
     private Map<String, String> collectColumnTypes(List<Map<String, Object>> dataList) {
291
+
309
         Map<String, String> columnTypes = new LinkedHashMap<>();
292
         Map<String, String> columnTypes = new LinkedHashMap<>();
310
         for (Map<String, Object> data : dataList) {
293
         for (Map<String, Object> data : dataList) {
311
             if (data == null) {
294
             if (data == null) {
327
      * 构建批量插入 SQL
310
      * 构建批量插入 SQL
328
      */
311
      */
329
     private String buildInsertSql(String dbName, String table, String superTableName,
312
     private String buildInsertSql(String dbName, String table, String superTableName,
330
-                                   Map<String, String> columnTypes, List<Map<String, Object>> dataList) {
313
+                                  Map<String, String> columnTypes, List<Map<String, Object>> dataList) {
314
+
331
         if (columnTypes.isEmpty()) {
315
         if (columnTypes.isEmpty()) {
332
             return null;
316
             return null;
333
         }
317
         }
365
         if (!hasData) {
349
         if (!hasData) {
366
             return null;
350
             return null;
367
         }
351
         }
368
-        log.info("生成的 INSERT SQL | 列类型: {} | SQL 前100字符: {}", columnTypes, sql.toString().substring(0, Math.min(100, sql.toString().length())));
352
+        log.info("生成的 INSERT SQL | 列类型: {} | SQL 前100字符: {}",
353
+                columnTypes, sql.substring(0, Math.min(100, sql.toString().length())));
369
         sql.setLength(sql.length() - 1);
354
         sql.setLength(sql.length() - 1);
370
         return sql.toString();
355
         return sql.toString();
371
     }
356
     }
495
     /**
480
     /**
496
      * 根据 TdEngine 列类型获取创建列的 SQL 类型
481
      * 根据 TdEngine 列类型获取创建列的 SQL 类型
497
      */
482
      */
498
-    private String getColumnTypeForDDL(String tdType, String columnName) {
483
+    private String getColumnTypeForDDL(String tdType) {
499
         switch (tdType) {
484
         switch (tdType) {
500
             case "BOOL":
485
             case "BOOL":
501
             case "BIGINT":
486
             case "BIGINT":
586
                 }
571
                 }
587
 
572
 
588
                 if (!existingColumns.contains(col)) {
573
                 if (!existingColumns.contains(col)) {
589
-                    String ddlType = getColumnTypeForDDL(colType, col);
574
+                    String ddlType = getColumnTypeForDDL(colType);
590
                     String alterSql = String.format(
575
                     String alterSql = String.format(
591
                             "ALTER STABLE %s.%s ADD COLUMN %s %s",
576
                             "ALTER STABLE %s.%s ADD COLUMN %s %s",
592
                             wrapName(dbName),
577
                             wrapName(dbName),

Загрузка…
Отмена
Сохранить