You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2022/06/22 04:20:30 UTC

[GitHub] [dolphinscheduler] caishunfeng commented on a diff in pull request #10526: add base metadata query (#5954)

caishunfeng commented on code in PR #10526:
URL: https://github.com/apache/dolphinscheduler/pull/10526#discussion_r903262362


##########
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/client/CommonDataSourceClient.java:
##########
@@ -101,6 +117,140 @@ public void checkClient() {
         }
     }
 
+    @Override
+    public List<String> getDatabaseList(String databasePattern) {
+        return this.jdbcTemplate.queryForList(getDatabaseListSql(databasePattern), String.class);
+    }
+
+    protected String getDatabaseListSql(String databasePattern) {
+        throw new UnsupportedOperationException("NOT_SUPPORT");
+    }
+
+    @Override
+    public List<String> getTableList(String dbName, String schemaName, String tablePattern) {
+        return this.jdbcTemplate.queryForList(getTableListSql(dbName, schemaName, tablePattern), String.class);
+    }
+
+    protected String getTableListSql(String dbName, String schemaName, String tablePattern) {
+        throw new UnsupportedOperationException("NOT_SUPPORT");
+    }
+
+    @Override
+    public List<Map<String, Object>> getTableStruct(String dbName, String schemaName, String tableName) {
+        throw new UnsupportedOperationException("NOT_SUPPORT");
+    }
+
+    @Override
+    public MutableTriple<Map<String, String>, List<Map<String, Object>>, List<Map<String, String>>> executeSql(
+        String dbName, String schemaName, Boolean oneSession, String querySql) {
+        List<String> sqlList = assemblingSql(dbName, schemaName, querySql);
+        JdbcTemplate jdbcTemplate = getJdbcTemplate(oneSession);
+        return executeSqlListReturnLast(jdbcTemplate, sqlList);
+    }
+
+    protected JdbcTemplate getJdbcTemplate(Boolean oneSession) {
+        return this.jdbcTemplate;
+    }
+
+    protected String switchEnvironment(String dbName, String schemaName) {
+        return null;
+    }
+
+    private List<String> assemblingSql(String dbName, String schemaName, String querySql) {
+        return Lists.asList(switchEnvironment(dbName, schemaName), querySql.split(";")).stream()
+            .filter(StringUtils::isNotBlank)
+            .collect(Collectors.toList());
+    }
+
+    private String getColumnName(String columnName) {
+        return columnName.contains(".") ? columnName.split("\\.", 2)[1] : columnName;
+    }
+
+    protected MutableTriple<Map<String, String>, List<Map<String, Object>>, List<Map<String, String>>>

Review Comment:
   It is necessary to add some notes and example to explain the  `MutableTriple`.



##########
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/client/CommonDataSourceClient.java:
##########
@@ -101,6 +117,140 @@ public void checkClient() {
         }
     }
 
+    @Override
+    public List<String> getDatabaseList(String databasePattern) {
+        return this.jdbcTemplate.queryForList(getDatabaseListSql(databasePattern), String.class);
+    }
+
+    protected String getDatabaseListSql(String databasePattern) {
+        throw new UnsupportedOperationException("NOT_SUPPORT");
+    }
+
+    @Override
+    public List<String> getTableList(String dbName, String schemaName, String tablePattern) {
+        return this.jdbcTemplate.queryForList(getTableListSql(dbName, schemaName, tablePattern), String.class);
+    }
+
+    protected String getTableListSql(String dbName, String schemaName, String tablePattern) {
+        throw new UnsupportedOperationException("NOT_SUPPORT");
+    }
+
+    @Override
+    public List<Map<String, Object>> getTableStruct(String dbName, String schemaName, String tableName) {
+        throw new UnsupportedOperationException("NOT_SUPPORT");
+    }
+
+    @Override
+    public MutableTriple<Map<String, String>, List<Map<String, Object>>, List<Map<String, String>>> executeSql(
+        String dbName, String schemaName, Boolean oneSession, String querySql) {
+        List<String> sqlList = assemblingSql(dbName, schemaName, querySql);
+        JdbcTemplate jdbcTemplate = getJdbcTemplate(oneSession);
+        return executeSqlListReturnLast(jdbcTemplate, sqlList);
+    }
+
+    protected JdbcTemplate getJdbcTemplate(Boolean oneSession) {
+        return this.jdbcTemplate;
+    }
+
+    protected String switchEnvironment(String dbName, String schemaName) {
+        return null;
+    }
+
+    private List<String> assemblingSql(String dbName, String schemaName, String querySql) {
+        return Lists.asList(switchEnvironment(dbName, schemaName), querySql.split(";")).stream()
+            .filter(StringUtils::isNotBlank)
+            .collect(Collectors.toList());
+    }
+
+    private String getColumnName(String columnName) {
+        return columnName.contains(".") ? columnName.split("\\.", 2)[1] : columnName;
+    }
+
+    protected MutableTriple<Map<String, String>, List<Map<String, Object>>, List<Map<String, String>>>
+    executeSqlListReturnLast(JdbcTemplate jdbcTemplate, List<String> querySqlList) {

Review Comment:
   What's the meaning of `return last`?



##########
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/plugin/DataSourceClientManager.java:
##########
@@ -46,34 +47,38 @@ public class DataSourceClientProvider {
             .build();
     private DataSourcePluginManager dataSourcePluginManager;
 
-    private DataSourceClientProvider() {
+    private DataSourceClientManager() {
         initDataSourcePlugin();
     }
 
     private static class DataSourceClientProviderHolder {
-        private static final DataSourceClientProvider INSTANCE = new DataSourceClientProvider();
+        private static final DataSourceClientManager INSTANCE = new DataSourceClientManager();
     }
 
-    public static DataSourceClientProvider getInstance() {
+    public static DataSourceClientManager getInstance() {
         return DataSourceClientProviderHolder.INSTANCE;
     }
 
-    public Connection getConnection(DbType dbType, ConnectionParam connectionParam) throws ExecutionException {
+    public DataSourceClient getDataSource(DbType dbType, ConnectionParam connectionParam) {
         BaseConnectionParam baseConnectionParam = (BaseConnectionParam) connectionParam;
         String datasourceUniqueId = DataSourceUtils.getDatasourceUniqueId(baseConnectionParam, dbType);
         logger.info("getConnection datasourceUniqueId {}", datasourceUniqueId);
 
-        DataSourceClient dataSourceClient = uniqueId2dataSourceClientCache.get(datasourceUniqueId, () -> {
-            Map<String, DataSourceChannel> dataSourceChannelMap = dataSourcePluginManager.getDataSourceChannelMap();
-            DataSourceChannel dataSourceChannel = dataSourceChannelMap.get(dbType.getDescp());
-            if (null == dataSourceChannel) {
-                throw new RuntimeException(String.format("datasource plugin '%s' is not found", dbType.getDescp()));
-            }
-            return dataSourceChannel.createDataSourceClient(baseConnectionParam, dbType);
-        });
-        return dataSourceClient.getConnection();
+        try {
+            return uniqueId2dataSourceClientCache.get(datasourceUniqueId, () -> {
+                Map<String, DataSourceChannel> dataSourceChannelMap = dataSourcePluginManager.getDataSourceChannelMap();
+                DataSourceChannel dataSourceChannel = dataSourceChannelMap.get(dbType.getDescp());
+                if (null == dataSourceChannel) {
+                    throw DataSourceException.getInstance(String.format("datasource plugin '%s' is not found", dbType.getDescp()));

Review Comment:
   >Directly new an exception is better?
   
   +1



-- 
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: commits-unsubscribe@dolphinscheduler.apache.org

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