CloudWise-Lukemiao commented on code in PR #12248:
URL: https://github.com/apache/iotdb/pull/12248#discussion_r1542666111


##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java:
##########
@@ -442,6 +461,46 @@ private static void importFromSingleFile(File file) {
     }
   }
 
+  private static void importFromSqlFile(File file) {
+    try (BufferedReader br = new BufferedReader(new 
FileReader(file.getAbsolutePath()))) {
+      String line;
+      while ((line = br.readLine()) != null) {
+        executeSql(line);
+      }
+      ioTPrinter.println(file.getName() + " Import completely!");
+    } catch (IOException e) {
+      ioTPrinter.println("SQL file read exception because: " + e.getMessage());
+    }
+  }
+
+  private static void executeSql(String sql) {
+    try {
+      Statement statement = getConnection().createStatement();

Review Comment:
   After use, it needs to be closed



##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportCsv.java:
##########
@@ -337,31 +368,81 @@ private static void dumpFromSqlFile(String filePath) 
throws IOException {
    * @param index used to create dump file name
    */
   private static void dumpResult(String sql, int index) {
+    if (EXPORT_SQL_TYPE_NAME.equalsIgnoreCase(exportType)) {
+      legalCheck(sql);
+    }
     final String path = targetDirectory + targetFile + index;
     try {
       SessionDataSet sessionDataSet = session.executeQueryStatement(sql, 
timeout);
       List<Object> headers = new ArrayList<>();
       List<String> names = sessionDataSet.getColumnNames();
       List<String> types = sessionDataSet.getColumnTypes();
-      if (Boolean.TRUE.equals(needDataTypePrinted)) {
-        for (int i = 0; i < names.size(); i++) {
-          if (!"Time".equals(names.get(i)) && !"Device".equals(names.get(i))) {
-            headers.add(String.format("%s(%s)", names.get(i), types.get(i)));
-          } else {
-            headers.add(names.get(i));
+      if (EXPORT_SQL_TYPE_NAME.equalsIgnoreCase(exportType)) {
+        writeSqlFile(sessionDataSet, path, names, linesPerFile);
+      } else {
+        if (Boolean.TRUE.equals(needDataTypePrinted)) {
+          for (int i = 0; i < names.size(); i++) {
+            if (!"Time".equals(names.get(i)) && 
!"Device".equals(names.get(i))) {
+              headers.add(String.format("%s(%s)", names.get(i), types.get(i)));
+            } else {
+              headers.add(names.get(i));
+            }
           }
+        } else {
+          headers.addAll(names);
         }
-      } else {
-        headers.addAll(names);
+        writeCsvFile(sessionDataSet, path, headers, linesPerFile);
       }
-      writeCsvFile(sessionDataSet, path, headers, linesPerFile);
       sessionDataSet.closeOperationHandle();
       ioTPrinter.println("Export completely!");
     } catch (StatementExecutionException | IoTDBConnectionException | 
IOException e) {
       ioTPrinter.println("Cannot dump result because: " + e.getMessage());
     }
   }
 
+  private static void legalCheck(String sql) {
+    String sqlLower = sql.toLowerCase();
+    if (sqlLower.contains("count(")

Review Comment:
   Please use regular expressions to determine incomplete coverage by including 
strings, such as count (*), where there are one or more spaces after count。eg: 
`  public static boolean hasAggregateFunction(String sqlQuery) {
           String aggregatePattern = "\\b(COUNT|SUM|AVG|MIN|MAX)\\b\\s*\\(";
           Pattern pattern = Pattern.compile(aggregatePattern, 
Pattern.CASE_INSENSITIVE);
           Matcher matcher = pattern.matcher(sqlQuery.toUpperCase(Locale.ROOT));
           return matcher.find();
       }`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to