github-advanced-security[bot] commented on code in PR #18962:
URL: https://github.com/apache/iotdb/pull/18962#discussion_r4346546908


##########
iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/relational/IoTDBRelationalDatabaseMetadata.java:
##########
@@ -504,14 +424,167 @@
       valuesList.add(valueInRow);
     }
 
-    // Convert Values to ByteBuffer
-    ByteBuffer tsBlock = null;
+    return toResultSet(stmt, fields, tsDataTypeList, valuesList);
+  }
+
+  /** Lists the tables matching the patterns, with their real database and 
table names. */
+  private List<TableInfo> listTables(Statement stmt, String schemaPattern, 
String tableNamePattern)
+      throws SQLException {
+    String sql = informationSchemaQuery("tables", schemaPattern, 
tableNamePattern);
+    try (ResultSet rs = stmt.executeQuery(sql)) {
+      List<TableInfo> tables = new ArrayList<>();
+      while (rs.next()) {
+        tables.add(
+            new TableInfo(
+                rs.getString("database"), rs.getString("table_name"), 
rs.getString("comment")));
+      }
+      return tables;
+    } catch (SQLException e) {
+      LOGGER.error(SHOW_TABLES_ERROR_MSG, e.getMessage());
+      try {
+        return listTablesWithShowStatements(stmt, schemaPattern, 
tableNamePattern);
+      } catch (SQLException fallbackException) {
+        e.addSuppressed(fallbackException);
+        throw e;
+      }
+    }
+  }
+
+  /**
+   * Lists tables with SHOW statements, for users who may not query 
information_schema. SHOW TABLES
+   * cannot filter by pattern, so the patterns are matched here.
+   */
+  private List<TableInfo> listTablesWithShowStatements(
+      Statement stmt, String schemaPattern, String tableNamePattern) throws 
SQLException {
+    List<String> databases = new ArrayList<>();
+    if (isLiteral(schemaPattern)) {
+      databases.add(removeEscape(schemaPattern));
+    } else {
+      try (ResultSet rs = stmt.executeQuery(SHOW_DATABASES_SQL)) {
+        while (rs.next()) {
+          String database = rs.getString(1);
+          if (matchesPattern(database, schemaPattern)) {
+            databases.add(database);
+          }
+        }
+      }
+    }
+
+    List<TableInfo> tables = new ArrayList<>();
+    for (String database : databases) {
+      try (ResultSet rs =
+          stmt.executeQuery("show tables details from " + 
quoteIdentifier(database))) {
+        while (rs.next()) {
+          String tableName = rs.getString("TableName");
+          if (matchesPattern(tableName, tableNamePattern)) {
+            tables.add(new TableInfo(database, tableName, 
rs.getString("Comment")));
+          }
+        }
+      }
+    }
+    return tables;
+  }
+
+  /**
+   * Lists the columns matching the patterns. ORDINAL_POSITION counts every 
column of the table, so
+   * the column name pattern is matched here rather than by the server.
+   */
+  private List<ColumnInfo> listColumns(
+      Statement stmt, String schemaPattern, String tableNamePattern, String 
columnNamePattern)
+      throws SQLException {
+    String sql = informationSchemaQuery("columns", schemaPattern, 
tableNamePattern);
+    try (ResultSet rs = stmt.executeQuery(sql)) {
+      List<ColumnInfo> columns = new ArrayList<>();
+      // information_schema lists the columns of each table in their defined 
order
+      Map<List<String>, Integer> ordinalPositions = new HashMap<>();
+      while (rs.next()) {
+        String database = rs.getString("database");
+        String tableName = rs.getString("table_name");
+        int ordinalPosition =
+            ordinalPositions.merge(Arrays.asList(database, tableName), 1, 
Integer::sum);
+        String columnName = rs.getString("column_name");
+        if (matchesPattern(columnName, columnNamePattern)) {
+          columns.add(
+              new ColumnInfo(
+                  database,
+                  tableName,
+                  columnName,
+                  rs.getString("datatype"),
+                  rs.getString("category"),
+                  rs.getString("comment"),
+                  ordinalPosition));
+        }
+      }
+      return columns;
+    } catch (SQLException e) {
+      LOGGER.error(SHOW_TABLES_ERROR_MSG, e.getMessage());
+      try {
+        return listColumnsWithDescribe(stmt, schemaPattern, tableNamePattern, 
columnNamePattern);
+      } catch (SQLException fallbackException) {
+        e.addSuppressed(fallbackException);
+        throw e;
+      }
+    }
+  }
+
+  /** Lists columns with DESC statements, for users who may not query 
information_schema. */
+  private List<ColumnInfo> listColumnsWithDescribe(
+      Statement stmt, String schemaPattern, String tableNamePattern, String 
columnNamePattern)
+      throws SQLException {
+    List<ColumnInfo> columns = new ArrayList<>();
+    for (TableInfo table : listTablesWithShowStatements(stmt, schemaPattern, 
tableNamePattern)) {
+      String sql =
+          "desc "
+              + quoteIdentifier(table.database())
+              + "."
+              + quoteIdentifier(table.tableName())
+              + " details";
+      try (ResultSet rs = stmt.executeQuery(sql)) {

Review Comment:
   ## SonarCloud / SQL queries should not be dynamically formatted
   
   <!--SONAR_ISSUE_KEY:AaFi4wdXcL4powoCO5E1-->Make sure using a dynamically 
formatted SQL query is safe here. <p>See more on <a 
href="https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaFi4wdXcL4powoCO5E1&open=AaFi4wdXcL4powoCO5E1&pullRequest=18962";>SonarQube
 Cloud</a></p>
   
   [Show more 
details](https://github.com/apache/iotdb/security/code-scanning/423)



##########
iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/relational/IoTDBRelationalDatabaseMetadata.java:
##########
@@ -504,14 +424,167 @@
       valuesList.add(valueInRow);
     }
 
-    // Convert Values to ByteBuffer
-    ByteBuffer tsBlock = null;
+    return toResultSet(stmt, fields, tsDataTypeList, valuesList);
+  }
+
+  /** Lists the tables matching the patterns, with their real database and 
table names. */
+  private List<TableInfo> listTables(Statement stmt, String schemaPattern, 
String tableNamePattern)
+      throws SQLException {
+    String sql = informationSchemaQuery("tables", schemaPattern, 
tableNamePattern);
+    try (ResultSet rs = stmt.executeQuery(sql)) {
+      List<TableInfo> tables = new ArrayList<>();
+      while (rs.next()) {
+        tables.add(
+            new TableInfo(
+                rs.getString("database"), rs.getString("table_name"), 
rs.getString("comment")));
+      }
+      return tables;
+    } catch (SQLException e) {
+      LOGGER.error(SHOW_TABLES_ERROR_MSG, e.getMessage());
+      try {
+        return listTablesWithShowStatements(stmt, schemaPattern, 
tableNamePattern);
+      } catch (SQLException fallbackException) {
+        e.addSuppressed(fallbackException);
+        throw e;
+      }
+    }
+  }
+
+  /**
+   * Lists tables with SHOW statements, for users who may not query 
information_schema. SHOW TABLES
+   * cannot filter by pattern, so the patterns are matched here.
+   */
+  private List<TableInfo> listTablesWithShowStatements(
+      Statement stmt, String schemaPattern, String tableNamePattern) throws 
SQLException {
+    List<String> databases = new ArrayList<>();
+    if (isLiteral(schemaPattern)) {
+      databases.add(removeEscape(schemaPattern));
+    } else {
+      try (ResultSet rs = stmt.executeQuery(SHOW_DATABASES_SQL)) {
+        while (rs.next()) {
+          String database = rs.getString(1);
+          if (matchesPattern(database, schemaPattern)) {
+            databases.add(database);
+          }
+        }
+      }
+    }
+
+    List<TableInfo> tables = new ArrayList<>();
+    for (String database : databases) {
+      try (ResultSet rs =
+          stmt.executeQuery("show tables details from " + 
quoteIdentifier(database))) {

Review Comment:
   ## SonarCloud / SQL queries should not be dynamically formatted
   
   <!--SONAR_ISSUE_KEY:AaFi4wdXcL4powoCO5E0-->Make sure using a dynamically 
formatted SQL query is safe here. <p>See more on <a 
href="https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaFi4wdXcL4powoCO5E0&open=AaFi4wdXcL4powoCO5E0&pullRequest=18962";>SonarQube
 Cloud</a></p>
   
   [Show more 
details](https://github.com/apache/iotdb/security/code-scanning/424)



-- 
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