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;
}
}

View File

@@ -16,6 +16,7 @@ public class DbParameterFactory {
parametersBuilder.put(EngineType.MYSQL.getName(), new MysqlParametersBuilder());
parametersBuilder.put(EngineType.POSTGRESQL.getName(), new PostgresqlParametersBuilder());
parametersBuilder.put(EngineType.HANADB.getName(), new HanadbParametersBuilder());
parametersBuilder.put(EngineType.STARROCKS.getName(), new StarrocksParametersBuilder());
parametersBuilder.put(EngineType.OTHER.getName(), new OtherParametersBuilder());
}

View File

@@ -0,0 +1,16 @@
package com.tencent.supersonic.headless.server.pojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class StarrocksParametersBuilder extends DefaultParametersBuilder {
@Override
public List<DatabaseParameter> build() {
return super.build();
}
}

View File

@@ -76,9 +76,15 @@ public class DatabaseController {
return databaseService.executeSql(sqlExecuteReq, user);
}
@RequestMapping("/getCatalogs")
public List<String> getCatalogs(@RequestParam("id") Long databaseId) throws SQLException {
return databaseService.getCatalogs(databaseId);
}
@RequestMapping("/getDbNames")
public List<String> getDbNames(@RequestParam("id") Long databaseId) throws SQLException {
return databaseService.getDbNames(databaseId);
public List<String> getDbNames(@RequestParam("id") Long databaseId,
@RequestParam(value = "catalog", required = false) String catalog) throws SQLException {
return databaseService.getDbNames(databaseId, catalog);
}
@RequestMapping("/getTables")

View File

@@ -36,7 +36,9 @@ public interface DatabaseService {
void deleteDatabase(Long databaseId);
List<String> getDbNames(Long id) throws SQLException;
List<String> getCatalogs(Long id) throws SQLException;
List<String> getDbNames(Long id, String catalog) throws SQLException;
List<String> getTables(Long id, String db) throws SQLException;

View File

@@ -200,10 +200,17 @@ public class DatabaseServiceImpl extends ServiceImpl<DatabaseDOMapper, DatabaseD
}
@Override
public List<String> getDbNames(Long id) throws SQLException {
public List<String> getCatalogs(Long id) throws SQLException {
DatabaseResp databaseResp = getDatabase(id);
DbAdaptor dbAdaptor = DbAdaptorFactory.getEngineAdaptor(databaseResp.getType());
return dbAdaptor.getDBs(DatabaseConverter.getConnectInfo(databaseResp));
return dbAdaptor.getCatalogs(DatabaseConverter.getConnectInfo(databaseResp));
}
@Override
public List<String> getDbNames(Long id, String catalog) throws SQLException {
DatabaseResp databaseResp = getDatabase(id);
DbAdaptor dbAdaptor = DbAdaptorFactory.getEngineAdaptor(databaseResp.getType());
return dbAdaptor.getDBs(DatabaseConverter.getConnectInfo(databaseResp), catalog);
}
@Override