From 0b0e74b8590de376eea08a48a8da6a14a4122ec3 Mon Sep 17 00:00:00 2001 From: zixi0825 Date: Sun, 3 Dec 2023 23:07:01 +0800 Subject: [PATCH] [Fix][Connector] Fix connector fetch metadata error in pg&sqlserver (#300) --- .../connector/plugin/DatabendConnector.java | 7 +--- .../connector/plugin/DmConfigBuilder.java | 8 ++--- .../connector/plugin/DmConnector.java | 17 --------- .../connector/plugin/JdbcConnector.java | 35 ++++++++++++++----- .../connector/plugin/MongodbConnector.java | 1 - .../connector/plugin/MysqlConnector.java | 10 ------ .../plugin/PostgreSqlConfigBuilder.java | 7 ++-- .../connector/plugin/PostgreSqlConnector.java | 10 ------ .../plugin/SqlServerConfigBuilder.java | 6 ++-- .../connector/plugin/SqlServerConnector.java | 10 ------ 10 files changed, 36 insertions(+), 75 deletions(-) diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-databend/src/main/java/io/datavines/connector/plugin/DatabendConnector.java b/datavines-connector/datavines-connector-plugins/datavines-connector-databend/src/main/java/io/datavines/connector/plugin/DatabendConnector.java index f570358b1..9ecdbda50 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-databend/src/main/java/io/datavines/connector/plugin/DatabendConnector.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-databend/src/main/java/io/datavines/connector/plugin/DatabendConnector.java @@ -31,14 +31,9 @@ public class DatabendConnector extends JdbcConnector { protected static final String[] TABLE_TYPES = new String[]{TABLE}; - @Override - public ResultSet getMetadataColumns(DatabaseMetaData metaData, String catalog, String schema, String tableName, String columnName) throws SQLException { - return metaData.getColumns(schema, null, tableName, columnName); - } - @Override public ResultSet getMetadataTables(DatabaseMetaData metaData, String catalog, String schema) throws SQLException { - return metaData.getTables(schema, null, null, TABLE_TYPES); + return metaData.getTables(catalog, schema, null, TABLE_TYPES); } @Override diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConfigBuilder.java b/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConfigBuilder.java index 499be7598..7179280bc 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConfigBuilder.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConfigBuilder.java @@ -22,15 +22,11 @@ public class DmConfigBuilder extends JdbcConfigBuilder{ @Override - protected InputParam getDatabaseInput(boolean isEn) { - // DM only need schema. - // To avoid affecting the original code structure, the schema will be stored in the database here - return getInputParam("database", + protected InputParam getSchemaInput(boolean isEn) { + return getInputParam("schema", isEn ? "schema" : "模式", isEn ? "please enter schema, which is usually the same as the username" : "请填入模式, 一般和用户名相同", 1, Validate.newBuilder().setRequired(true).setMessage(isEn ? "please enter schema" : "请填入模式").build(), null); } - - } diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConnector.java b/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConnector.java index 12887cfa8..ff213772e 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConnector.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-dm/src/main/java/io/datavines/connector/plugin/DmConnector.java @@ -35,21 +35,4 @@ protected ResultSet getMetadataDatabases(Connection connection) throws SQLExcept // DM only need schema return metaData.getSchemas(); } - - protected ResultSet getMetadataTables(DatabaseMetaData metaData, String catalog, String schema) throws SQLException { - // DM only need schema - return metaData.getTables(null, schema, null, TABLE_TYPES); - } - - protected ResultSet getMetadataColumns(DatabaseMetaData metaData, - String catalog, String schema, - String tableName, String columnName) throws SQLException { - // DM only need schema - return metaData.getColumns(null, schema, tableName, columnName); - } - - protected ResultSet getPrimaryKeys(DatabaseMetaData metaData,String catalog, String schema, String tableName) throws SQLException { - // DM only need schema - return metaData.getPrimaryKeys(null, schema, tableName); - } } diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-jdbc/src/main/java/io/datavines/connector/plugin/JdbcConnector.java b/datavines-connector/datavines-connector-plugins/datavines-connector-jdbc/src/main/java/io/datavines/connector/plugin/JdbcConnector.java index b4fc2ae86..2626abd75 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-jdbc/src/main/java/io/datavines/connector/plugin/JdbcConnector.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-jdbc/src/main/java/io/datavines/connector/plugin/JdbcConnector.java @@ -23,9 +23,9 @@ import io.datavines.common.datasource.jdbc.entity.TableColumnInfo; import io.datavines.common.param.*; import io.datavines.common.utils.JSONUtils; +import io.datavines.common.utils.StringUtils; import io.datavines.connector.api.Connector; import io.datavines.common.datasource.jdbc.utils.JdbcDataSourceUtils; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,14 +98,23 @@ public ConnectorResponse getTables(GetTablesRequestParam param) throws SQLExcept Connection connection = executorClient.getConnection(); List tableList = null; - ResultSet tables = null; + ResultSet tables; try { DatabaseMetaData metaData = connection.getMetaData(); - String schema = param.getDataBase(); + String catalog; + String schema; + + if (StringUtils.isNotEmpty(jdbcConnectionInfo.getCatalog())) { + catalog = jdbcConnectionInfo.getCatalog(); + schema = param.getDataBase(); + } else { + catalog = param.getDataBase(); + schema = StringUtils.isEmptyOrNullStr(jdbcConnectionInfo.getSchema()) ? null : jdbcConnectionInfo.getSchema(); + } tableList = new ArrayList<>(); - tables = getMetadataTables(metaData, jdbcConnectionInfo.getCatalog(), schema); + tables = getMetadataTables(metaData, catalog, schema); if (null == tables) { return builder.result(tableList).build(); @@ -147,12 +156,22 @@ public ConnectorResponse getColumns(GetColumnsRequestParam param) throws SQLExce TableColumnInfo tableColumnInfo = null; try { - String dbName = param.getDataBase(); + String catalog; + String schema; + + if (StringUtils.isNotEmpty(jdbcConnectionInfo.getCatalog())) { + catalog = jdbcConnectionInfo.getCatalog(); + schema = param.getDataBase(); + } else { + catalog = param.getDataBase(); + schema = StringUtils.isEmptyOrNullStr(jdbcConnectionInfo.getSchema()) ? null : jdbcConnectionInfo.getSchema(); + } + String tableName = param.getTable(); if (null != connection) { DatabaseMetaData metaData = connection.getMetaData(); - List primaryKeys = getPrimaryKeys(jdbcConnectionInfo.getCatalog(), dbName, tableName, metaData); - List columns = getColumns(jdbcConnectionInfo.getCatalog(), dbName, tableName, metaData); + List primaryKeys = getPrimaryKeys(catalog, schema, tableName, metaData); + List columns = getColumns(catalog, schema, tableName, metaData); tableColumnInfo = new TableColumnInfo(tableName, primaryKeys, columns); } } catch (SQLException e) { @@ -254,6 +273,6 @@ protected ResultSet getMetadataColumns(DatabaseMetaData metaData, } protected ResultSet getPrimaryKeys(DatabaseMetaData metaData,String catalog, String schema, String tableName) throws SQLException { - return metaData.getPrimaryKeys(schema, null, tableName); + return metaData.getPrimaryKeys(catalog, schema, tableName); } } diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-mongodb/src/main/java/io/datavines/connector/plugin/MongodbConnector.java b/datavines-connector/datavines-connector-plugins/datavines-connector-mongodb/src/main/java/io/datavines/connector/plugin/MongodbConnector.java index 66f7a8f6d..b24f6b0b0 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-mongodb/src/main/java/io/datavines/connector/plugin/MongodbConnector.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-mongodb/src/main/java/io/datavines/connector/plugin/MongodbConnector.java @@ -57,7 +57,6 @@ public class MongodbConnector implements Connector { private static final List SYSTEM_TABLE = Arrays.asList("system.indexes", "system.users", "system.version", "system.views"); - @Override public ConnectorResponse getDatabases(GetDatabasesRequestParam param) throws SQLException { ConnectorResponse.ConnectorResponseBuilder builder = ConnectorResponse.builder(); diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-mysql/src/main/java/io/datavines/connector/plugin/MysqlConnector.java b/datavines-connector/datavines-connector-plugins/datavines-connector-mysql/src/main/java/io/datavines/connector/plugin/MysqlConnector.java index 397654f02..a40ff07c9 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-mysql/src/main/java/io/datavines/connector/plugin/MysqlConnector.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-mysql/src/main/java/io/datavines/connector/plugin/MysqlConnector.java @@ -32,16 +32,6 @@ public BaseJdbcDataSourceInfo getDatasourceInfo(JdbcConnectionInfo jdbcConnectio return new MysqlDataSourceInfo(jdbcConnectionInfo); } - @Override - public ResultSet getMetadataColumns(DatabaseMetaData metaData, String catalog, String schema, String tableName, String columnName) throws SQLException { - return metaData.getColumns(schema, null, tableName, columnName); - } - - @Override - public ResultSet getMetadataTables(DatabaseMetaData metaData, String catalog, String schema) throws SQLException { - return metaData.getTables(schema, null, null, TABLE_TYPES); - } - @Override public ResultSet getMetadataDatabases(Connection connection) throws SQLException { DatabaseMetaData metaData = connection.getMetaData(); diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConfigBuilder.java b/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConfigBuilder.java index d0f60c431..743bb9c98 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConfigBuilder.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConfigBuilder.java @@ -24,9 +24,8 @@ public class PostgreSqlConfigBuilder extends JdbcConfigBuilder { @Override protected InputParam getSchemaInput(boolean isEn) { return getInputParam("schema", - isEn ? "schema" : "Schema", - isEn ? "please enter schema" : "请填入 Schema", 1, - Validate.newBuilder().setRequired(true).setMessage(isEn ? "please enter schema" : "请填入 Schema").build(), - null); + isEn ? "schema" : "模式", + isEn ? "please enter schema" : "请填入模式", 1, + Validate.newBuilder().setRequired(true).setMessage(isEn ? "please enter schema" : "请填入模式").build(), null); } } diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConnector.java b/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConnector.java index 69dd98c92..976f01e32 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConnector.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-postgresql/src/main/java/io/datavines/connector/plugin/PostgreSqlConnector.java @@ -31,16 +31,6 @@ public BaseJdbcDataSourceInfo getDatasourceInfo(JdbcConnectionInfo jdbcConnectio return new PostgreSqlDataSourceInfo(jdbcConnectionInfo); } - @Override - public ResultSet getMetadataColumns(DatabaseMetaData metaData, String catalog, String schema, String tableName, String columnName) throws SQLException { - return metaData.getColumns(schema, null, tableName, columnName); - } - - @Override - public ResultSet getMetadataTables(DatabaseMetaData metaData, String catalog, String schema) throws SQLException { - return metaData.getTables(schema, null, null, TABLE_TYPES); - } - @Override public ResultSet getMetadataDatabases(Connection connection) throws SQLException { DatabaseMetaData metaData = connection.getMetaData(); diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConfigBuilder.java b/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConfigBuilder.java index 8e8cacec6..a5963a2e1 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConfigBuilder.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConfigBuilder.java @@ -32,9 +32,9 @@ protected InputParam getPropertiesInput(boolean isEn) { @Override protected InputParam getSchemaInput(boolean isEn) { return getInputParam("schema", - isEn ? "schema" : "Schema", - isEn ? "please enter schema" : "请填入 Schema", 1, - Validate.newBuilder().setRequired(true).setMessage(isEn ? "please enter schema" : "请填入 Schema").build(), + isEn ? "schema" : "模式", + isEn ? "please enter schema" : "请填入模式", 1, + Validate.newBuilder().setRequired(true).setMessage(isEn ? "please enter schema" : "请填入模式").build(), null); } } diff --git a/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConnector.java b/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConnector.java index 6e171616c..c2c8cdf5c 100644 --- a/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConnector.java +++ b/datavines-connector/datavines-connector-plugins/datavines-connector-sqlserver/src/main/java/io/datavines/connector/plugin/SqlServerConnector.java @@ -31,16 +31,6 @@ public BaseJdbcDataSourceInfo getDatasourceInfo(JdbcConnectionInfo jdbcConnectio return new SqlServerDataSourceInfo(jdbcConnectionInfo); } - @Override - public ResultSet getMetadataColumns(DatabaseMetaData metaData, String catalog, String schema, String tableName, String columnName) throws SQLException { - return metaData.getColumns(schema, null, tableName, columnName); - } - - @Override - public ResultSet getMetadataTables(DatabaseMetaData metaData, String catalog, String schema) throws SQLException { - return metaData.getTables(schema, null, null, TABLE_TYPES); - } - @Override public ResultSet getMetadataDatabases(Connection connection) throws SQLException { DatabaseMetaData metaData = connection.getMetaData();