feat: add support starrocks and multiple catalog (#2066)

This commit is contained in:
Hyman_bz
2025-02-19 18:00:22 +08:00
committed by GitHub
parent 86b9d2013a
commit 33268bf3d9
12 changed files with 183 additions and 16 deletions

View File

@@ -17,7 +17,20 @@ import java.util.List;
@Slf4j
public abstract class BaseDbAdaptor implements DbAdaptor {
public List<String> getDBs(ConnectInfo connectionInfo) throws SQLException {
@Override
public List<String> getCatalogs(ConnectInfo connectInfo) throws SQLException {
// Apart from supporting multiple catalog types of data sources, other types will return an
// empty set by default.
return List.of();
}
public List<String> getDBs(ConnectInfo connectionInfo, String catalog) throws SQLException {
// Except for special types implemented separately, the generic logic catalog does not take
// effect.
return getDBs(connectionInfo);
}
protected List<String> getDBs(ConnectInfo connectionInfo) throws SQLException {
List<String> dbs = Lists.newArrayList();
DatabaseMetaData metaData = getDatabaseMetaData(connectionInfo);
try {

View File

@@ -14,7 +14,9 @@ public interface DbAdaptor {
String rewriteSql(String sql);
List<String> getDBs(ConnectInfo connectInfo) throws SQLException;
List<String> getCatalogs(ConnectInfo connectInfo) throws SQLException;
List<String> getDBs(ConnectInfo connectInfo, String catalog) throws SQLException;
List<String> getTables(ConnectInfo connectInfo, String schemaName) throws SQLException;

View File

@@ -18,6 +18,7 @@ public class DbAdaptorFactory {
dbAdaptorMap.put(EngineType.OTHER.getName(), new DefaultDbAdaptor());
dbAdaptorMap.put(EngineType.DUCKDB.getName(), new DuckdbAdaptor());
dbAdaptorMap.put(EngineType.HANADB.getName(), new HanadbAdaptor());
dbAdaptorMap.put(EngineType.STARROCKS.getName(), new StarrocksAdaptor());
}
public static DbAdaptor getEngineAdaptor(String engineType) {

View File

@@ -0,0 +1,42 @@
package com.tencent.supersonic.headless.core.adaptor.db;
import com.google.common.collect.Lists;
import com.tencent.supersonic.headless.core.pojo.ConnectInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import java.sql.*;
import java.util.List;
@Slf4j
public class StarrocksAdaptor extends MysqlAdaptor {
@Override
public List<String> getCatalogs(ConnectInfo connectInfo) throws SQLException {
List<String> catalogs = Lists.newArrayList();
try (Connection con = DriverManager.getConnection(connectInfo.getUrl(),
connectInfo.getUserName(), connectInfo.getPassword());
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SHOW CATALOGS")) {
while (rs.next()) {
catalogs.add(rs.getString(1));
}
}
return catalogs;
}
@Override
public List<String> getDBs(ConnectInfo connectionInfo, String catalog) throws SQLException {
Assert.hasText(catalog, "StarRocks type catalog can not be null or empty");
List<String> dbs = Lists.newArrayList();
try (Connection con = DriverManager.getConnection(connectionInfo.getUrl(),
connectionInfo.getUserName(), connectionInfo.getPassword());
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SHOW DATABASES IN " + catalog)) {
while (rs.next()) {
dbs.add(rs.getString(1));
}
}
return dbs;
}
}