You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/08/23 05:38:53 UTC

[GitHub] [shardingsphere] sandynz commented on a diff in pull request #20443: Improve pipeline fetch schema from data source

sandynz commented on code in PR #20443:
URL: https://github.com/apache/shardingsphere/pull/20443#discussion_r952170726


##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/PipelineSchemaTableUtil.java:
##########
@@ -45,24 +46,44 @@ private PipelineSchemaTableUtil() {
      * Get schema tables map from actual data source.
      *
      * @param pipelineDataSourceConfig pipeline data source config
+     * @param schemaName schema name,
      * @param tableName table name
      * @return schema tables map
      */
-    public static Map<String, List<String>> getSchemaTablesMapFromActual(final PipelineDataSourceConfiguration pipelineDataSourceConfig, final String tableName) {
+    public static Map<String, List<String>> getSchemaTablesMapFromActual(final PipelineDataSourceConfiguration pipelineDataSourceConfig, final String schemaName, final String tableName) {
+        log.info("start get schema tables from actual, begin:{}", LocalDateTime.now());
         Map<String, List<String>> result = new HashMap<>();
         try (PipelineDataSourceWrapper dataSource = PipelineDataSourceFactory.newInstance(pipelineDataSourceConfig)) {
             try (Connection connection = dataSource.getConnection()) {
                 DatabaseMetaData metaData = connection.getMetaData();
-                ResultSet resultSet = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
+                String targetSchema = ObjectUtils.defaultIfNull(schemaName, connection.getSchema());
+                ResultSet resultSet = metaData.getTables(connection.getCatalog(), targetSchema, tableName, new String[]{"TABLE"});
                 while (resultSet.next()) {
-                    String schemaName = resultSet.getString("TABLE_SCHEM");
-                    result.computeIfAbsent(schemaName, k -> new ArrayList<>()).add(resultSet.getString("TABLE_NAME"));
+                    result.computeIfAbsent(targetSchema, k -> new ArrayList<>()).add(resultSet.getString("TABLE_NAME"));
                 }
+                log.info("get schema tables success, catalog:{}, schema:{}, table:{}, result:{}, end:{}", targetSchema, connection.getCatalog(), tableName, resultSet, LocalDateTime.now());
             }
         } catch (final SQLException ex) {
-            log.error("Get schema name map error", ex);
-            throw new AddMigrationSourceResourceException(ex.getMessage());
+            log.error("get schema name map error", ex);
+            throw new RuntimeException(ex.getMessage());
         }
         return result;
     }
+    
+    /**
+     * get default schema by connection.getSchema().

Review Comment:
   Javadoc should start with uppercase



##########
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/PipelineSchemaTableUtil.java:
##########
@@ -45,24 +46,44 @@ private PipelineSchemaTableUtil() {
      * Get schema tables map from actual data source.
      *
      * @param pipelineDataSourceConfig pipeline data source config
+     * @param schemaName schema name,
      * @param tableName table name
      * @return schema tables map
      */
-    public static Map<String, List<String>> getSchemaTablesMapFromActual(final PipelineDataSourceConfiguration pipelineDataSourceConfig, final String tableName) {
+    public static Map<String, List<String>> getSchemaTablesMapFromActual(final PipelineDataSourceConfiguration pipelineDataSourceConfig, final String schemaName, final String tableName) {
+        log.info("start get schema tables from actual, begin:{}", LocalDateTime.now());
         Map<String, List<String>> result = new HashMap<>();
         try (PipelineDataSourceWrapper dataSource = PipelineDataSourceFactory.newInstance(pipelineDataSourceConfig)) {
             try (Connection connection = dataSource.getConnection()) {
                 DatabaseMetaData metaData = connection.getMetaData();
-                ResultSet resultSet = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
+                String targetSchema = ObjectUtils.defaultIfNull(schemaName, connection.getSchema());
+                ResultSet resultSet = metaData.getTables(connection.getCatalog(), targetSchema, tableName, new String[]{"TABLE"});
                 while (resultSet.next()) {
-                    String schemaName = resultSet.getString("TABLE_SCHEM");
-                    result.computeIfAbsent(schemaName, k -> new ArrayList<>()).add(resultSet.getString("TABLE_NAME"));
+                    result.computeIfAbsent(targetSchema, k -> new ArrayList<>()).add(resultSet.getString("TABLE_NAME"));
                 }
+                log.info("get schema tables success, catalog:{}, schema:{}, table:{}, result:{}, end:{}", targetSchema, connection.getCatalog(), tableName, resultSet, LocalDateTime.now());
             }
         } catch (final SQLException ex) {
-            log.error("Get schema name map error", ex);
-            throw new AddMigrationSourceResourceException(ex.getMessage());
+            log.error("get schema name map error", ex);
+            throw new RuntimeException(ex.getMessage());
         }
         return result;
     }
+    
+    /**
+     * get default schema by connection.getSchema().
+     *
+     * @param pipelineDataSourceConfig pipeline data source config
+     * @return schema
+     */
+    public static String getDefaultSchema(final PipelineDataSourceConfiguration pipelineDataSourceConfig) {

Review Comment:
   `pipelineDataSourceConfig` could be shorter, e.g. `dataSourceConfig`



-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org