[refactor](jdbc catalog) refactor jdbc catalog get databases logic (#32579)
This commit is contained in:
@ -513,11 +513,6 @@ public class PostgreSQLJdbcHMSCachedClient extends JdbcHMSCachedClient {
|
||||
throw new HMSClientException("Do not support in PostgreSQLJdbcHMSCachedClient.");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
throw new HMSClientException("Do not support in PostgreSQLJdbcHMSCachedClient.");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
|
||||
throw new HMSClientException("Do not support in PostgreSQLJdbcHMSCachedClient.");
|
||||
|
||||
@ -27,11 +27,6 @@ public class JdbcClickHouseClient extends JdbcClient {
|
||||
super(jdbcClientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SHOW DATABASES";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getTableTypes() {
|
||||
return new String[] {"TABLE", "VIEW", "SYSTEM TABLE"};
|
||||
|
||||
@ -220,23 +220,22 @@ public abstract class JdbcClient {
|
||||
*/
|
||||
public List<String> getDatabaseNameList() {
|
||||
Connection conn = getConnection();
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
if (isOnlySpecifiedDatabase && includeDatabaseMap.isEmpty() && excludeDatabaseMap.isEmpty()) {
|
||||
return getSpecifiedDatabase(conn);
|
||||
}
|
||||
List<String> remoteDatabaseNames = Lists.newArrayList();
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
String sql = getDatabaseQuery();
|
||||
rs = stmt.executeQuery(sql);
|
||||
while (rs.next()) {
|
||||
remoteDatabaseNames.add(rs.getString(1));
|
||||
if (isOnlySpecifiedDatabase && includeDatabaseMap.isEmpty() && excludeDatabaseMap.isEmpty()) {
|
||||
String currentDatabase = conn.getSchema();
|
||||
remoteDatabaseNames.add(currentDatabase);
|
||||
} else {
|
||||
rs = conn.getMetaData().getSchemas(conn.getCatalog(), null);
|
||||
while (rs.next()) {
|
||||
remoteDatabaseNames.add(rs.getString("TABLE_SCHEM"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JdbcClientException("failed to get database name list from jdbc", e);
|
||||
} finally {
|
||||
close(rs, stmt, conn);
|
||||
close(rs, conn);
|
||||
}
|
||||
return filterDatabaseNames(remoteDatabaseNames);
|
||||
}
|
||||
@ -351,21 +350,7 @@ public abstract class JdbcClient {
|
||||
|
||||
// protected methods,for subclass to override
|
||||
protected String getCatalogName(Connection conn) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract String getDatabaseQuery();
|
||||
|
||||
protected List<String> getSpecifiedDatabase(Connection conn) {
|
||||
List<String> databaseNames = Lists.newArrayList();
|
||||
try {
|
||||
databaseNames.add(conn.getSchema());
|
||||
} catch (SQLException e) {
|
||||
throw new JdbcClientException("failed to get specified database name from jdbc", e);
|
||||
} finally {
|
||||
close(conn);
|
||||
}
|
||||
return databaseNames;
|
||||
return conn.getCatalog();
|
||||
}
|
||||
|
||||
protected String[] getTableTypes() {
|
||||
|
||||
@ -26,7 +26,6 @@ import com.google.common.collect.Lists;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
public class JdbcDB2Client extends JdbcClient {
|
||||
@ -38,35 +37,26 @@ public class JdbcDB2Client extends JdbcClient {
|
||||
@Override
|
||||
public List<String> getDatabaseNameList() {
|
||||
Connection conn = getConnection();
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
if (isOnlySpecifiedDatabase && includeDatabaseMap.isEmpty() && excludeDatabaseMap.isEmpty()) {
|
||||
return getSpecifiedDatabase(conn);
|
||||
}
|
||||
List<String> remoteDatabaseNames = Lists.newArrayList();
|
||||
try {
|
||||
rs = conn.getMetaData().getSchemas(conn.getCatalog(), null);
|
||||
while (rs.next()) {
|
||||
remoteDatabaseNames.add(rs.getString("TABLE_SCHEM").trim());
|
||||
if (isOnlySpecifiedDatabase && includeDatabaseMap.isEmpty() && excludeDatabaseMap.isEmpty()) {
|
||||
String currentDatabase = conn.getSchema().trim();
|
||||
remoteDatabaseNames.add(currentDatabase);
|
||||
} else {
|
||||
rs = conn.getMetaData().getSchemas(conn.getCatalog(), null);
|
||||
while (rs.next()) {
|
||||
remoteDatabaseNames.add(rs.getString("TABLE_SCHEM").trim());
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JdbcClientException("failed to get database name list from jdbc", e);
|
||||
} finally {
|
||||
close(rs, stmt, conn);
|
||||
close(rs, conn);
|
||||
}
|
||||
return filterDatabaseNames(remoteDatabaseNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SELECT schemaname FROM syscat.schemata WHERE DEFINER = CURRENT USER;";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getCatalogName(Connection conn) throws SQLException {
|
||||
return conn.getCatalog();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
|
||||
String db2Type = fieldSchema.getDataTypeName();
|
||||
|
||||
@ -65,21 +65,26 @@ public class JdbcMySQLClient extends JdbcClient {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SHOW DATABASES";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> getSpecifiedDatabase(Connection conn) {
|
||||
List<String> databaseNames = Lists.newArrayList();
|
||||
public List<String> getDatabaseNameList() {
|
||||
Connection conn = getConnection();
|
||||
ResultSet rs = null;
|
||||
List<String> remoteDatabaseNames = Lists.newArrayList();
|
||||
try {
|
||||
databaseNames.add(conn.getCatalog());
|
||||
if (isOnlySpecifiedDatabase && includeDatabaseMap.isEmpty() && excludeDatabaseMap.isEmpty()) {
|
||||
String currentDatabase = conn.getCatalog();
|
||||
remoteDatabaseNames.add(currentDatabase);
|
||||
} else {
|
||||
rs = conn.getMetaData().getCatalogs();
|
||||
while (rs.next()) {
|
||||
remoteDatabaseNames.add(rs.getString("TABLE_CAT"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JdbcClientException("failed to get specified database name from jdbc", e);
|
||||
throw new JdbcClientException("failed to get database name list from jdbc", e);
|
||||
} finally {
|
||||
close(conn);
|
||||
close(rs, conn);
|
||||
}
|
||||
return databaseNames;
|
||||
return filterDatabaseNames(remoteDatabaseNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -162,6 +167,10 @@ public class JdbcMySQLClient extends JdbcClient {
|
||||
return tableSchema;
|
||||
}
|
||||
|
||||
protected String getCatalogName(Connection conn) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Set<String> getFilterInternalDatabases() {
|
||||
return ImmutableSet.<String>builder()
|
||||
.add("mysql")
|
||||
|
||||
@ -58,11 +58,6 @@ public class JdbcOceanBaseClient extends JdbcClient {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return currentClient.getDatabaseQuery();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
|
||||
return currentClient.jdbcTypeToDoris(fieldSchema);
|
||||
|
||||
@ -37,42 +37,11 @@ public class JdbcOracleClient extends JdbcClient {
|
||||
super(jdbcClientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SELECT DISTINCT OWNER FROM all_tables";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getCatalogName(Connection conn) throws SQLException {
|
||||
return conn.getCatalog();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTestQuery() {
|
||||
return "SELECT 1 FROM dual";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getDatabaseNameList() {
|
||||
Connection conn = getConnection();
|
||||
ResultSet rs = null;
|
||||
if (isOnlySpecifiedDatabase && includeDatabaseMap.isEmpty() && excludeDatabaseMap.isEmpty()) {
|
||||
return getSpecifiedDatabase(conn);
|
||||
}
|
||||
List<String> remoteDatabaseNames = Lists.newArrayList();
|
||||
try {
|
||||
rs = conn.getMetaData().getSchemas(conn.getCatalog(), null);
|
||||
while (rs.next()) {
|
||||
remoteDatabaseNames.add(rs.getString("TABLE_SCHEM"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new JdbcClientException("failed to get database name list from jdbc", e);
|
||||
} finally {
|
||||
close(rs, conn);
|
||||
}
|
||||
return filterDatabaseNames(remoteDatabaseNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JdbcFieldSchema> getJdbcColumnsInfo(String localDbName, String localTableName) {
|
||||
Connection conn = getConnection();
|
||||
|
||||
@ -27,12 +27,6 @@ public class JdbcPostgreSQLClient extends JdbcClient {
|
||||
super(jdbcClientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SELECT nspname FROM pg_namespace WHERE has_schema_privilege("
|
||||
+ "'" + jdbcUser + "', nspname, 'USAGE');";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getTableTypes() {
|
||||
return new String[] {"TABLE", "PARTITIONED TABLE", "VIEW", "MATERIALIZED VIEW", "FOREIGN TABLE"};
|
||||
|
||||
@ -26,11 +26,6 @@ public class JdbcSQLServerClient extends JdbcClient {
|
||||
super(jdbcClientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SELECT name FROM sys.schemas";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
|
||||
String originSqlserverType = fieldSchema.getDataTypeName();
|
||||
|
||||
@ -26,11 +26,6 @@ public class JdbcSapHanaClient extends JdbcClient {
|
||||
super(jdbcClientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SELECT SCHEMA_NAME FROM SYS.SCHEMAS WHERE HAS_PRIVILEGES = 'TRUE'";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getTableTypes() {
|
||||
return new String[] {"TABLE", "VIEW", "OLAP VIEW", "JOIN VIEW", "HIERARCHY VIEW", "CALC VIEW"};
|
||||
|
||||
@ -22,24 +22,11 @@ import org.apache.doris.catalog.PrimitiveType;
|
||||
import org.apache.doris.catalog.ScalarType;
|
||||
import org.apache.doris.catalog.Type;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class JdbcTrinoClient extends JdbcClient {
|
||||
protected JdbcTrinoClient(JdbcClientConfig jdbcClientConfig) {
|
||||
super(jdbcClientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseQuery() {
|
||||
return "SHOW SCHEMAS";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getCatalogName(Connection conn) throws SQLException {
|
||||
return conn.getCatalog();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
|
||||
String trinoType = fieldSchema.getDataTypeName();
|
||||
|
||||
Reference in New Issue
Block a user