Skip to content

Commit

Permalink
[Fix][Connector] Fix connector fetch metadata error in pg&sqlserver (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zixi0825 authored Dec 3, 2023
1 parent 1b65967 commit 0b0e74b
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -98,14 +98,23 @@ public ConnectorResponse getTables(GetTablesRequestParam param) throws SQLExcept
Connection connection = executorClient.getConnection();

List<TableInfo> 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();
Expand Down Expand Up @@ -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<String> primaryKeys = getPrimaryKeys(jdbcConnectionInfo.getCatalog(), dbName, tableName, metaData);
List<ColumnInfo> columns = getColumns(jdbcConnectionInfo.getCatalog(), dbName, tableName, metaData);
List<String> primaryKeys = getPrimaryKeys(catalog, schema, tableName, metaData);
List<ColumnInfo> columns = getColumns(catalog, schema, tableName, metaData);
tableColumnInfo = new TableColumnInfo(tableName, primaryKeys, columns);
}
} catch (SQLException e) {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public class MongodbConnector implements Connector {

private static final List<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0b0e74b

Please sign in to comment.