feat(agents): move agents into dbx repo
This commit is contained in:
1 parent
ab35fda299
commit
3a25487dea
215 files changed
+29740
-15
No files matched your search
@@ -0,0 +1,349 @@
|
||||
package com.dbx.agent.kingbase;
|
||||
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.ConnectParams;
|
||||
import com.dbx.agent.DatabaseInfo;
|
||||
import com.dbx.agent.ForeignKeyInfo;
|
||||
import com.dbx.agent.IndexInfo;
|
||||
import com.dbx.agent.JdbcIdentifiers;
|
||||
import com.dbx.agent.JsonRpcServer;
|
||||
import com.dbx.agent.ObjectInfo;
|
||||
import com.dbx.agent.ObjectSource;
|
||||
import com.dbx.agent.PostgresLikeAgent;
|
||||
import com.dbx.agent.PostgresLikeAgentProfile;
|
||||
import com.dbx.agent.TableInfo;
|
||||
import com.dbx.agent.TriggerInfo;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public final class KingbaseAgent extends PostgresLikeAgent {
|
||||
public static final PostgresLikeAgentProfile KINGBASE_PROFILE = new PostgresLikeAgentProfile(
|
||||
"com.kingbase8.Driver",
|
||||
"jdbc:kingbase8://{host}:{port}/{database}"
|
||||
);
|
||||
|
||||
public KingbaseAgent() {
|
||||
super(KINGBASE_PROFILE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterConnect(ConnectParams params, Connection connection) {
|
||||
if (params.isMysql_compat_mode()) {
|
||||
setMysqlCompatMode(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DatabaseInfo> listDatabases() {
|
||||
return unchecked(() -> {
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement("SELECT current_database() AS database_name");
|
||||
ResultSet rs = stmt.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return Collections.singletonList(new DatabaseInfo(rs.getString("database_name")));
|
||||
}
|
||||
}
|
||||
return Collections.singletonList(new DatabaseInfo(getConfiguredDatabase()));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listSchemas() {
|
||||
return unchecked(() -> {
|
||||
List<String> result = new ArrayList<>();
|
||||
String sql = "SELECT schema_name " +
|
||||
"FROM information_schema.schemata " +
|
||||
"WHERE UPPER(schema_name) <> 'INFORMATION_SCHEMA' " +
|
||||
"AND UPPER(schema_name) NOT LIKE 'SYS%' " +
|
||||
"AND UPPER(schema_name) NOT LIKE 'XLOG%' " +
|
||||
"ORDER BY schema_name";
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(rs.getString("schema_name"));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableInfo> listTables(String schema) {
|
||||
return listTables(schema, "table_type = 'BASE TABLE'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ObjectInfo> listObjects(String schema) {
|
||||
List<ObjectInfo> result = new ArrayList<>();
|
||||
for (TableInfo table : listTables(schema)) {
|
||||
result.add(new ObjectInfo(table.getName(), table.getTable_type(), effectiveSchema(schema), table.getComment()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectSource getObjectSource(String schema, String name, String objectType) {
|
||||
if (!"VIEW".equalsIgnoreCase(objectType)) {
|
||||
return new ObjectSource(name, objectType, effectiveSchema(schema), "");
|
||||
}
|
||||
return unchecked(() -> {
|
||||
String source = "";
|
||||
String sql = "SELECT view_definition " +
|
||||
"FROM information_schema.views " +
|
||||
"WHERE table_schema = " + sqlString(effectiveSchema(schema)) +
|
||||
" AND table_name = " + sqlString(name);
|
||||
try (Statement stmt = requireConnected().createStatement()) {
|
||||
try (ResultSet rs = stmt.executeQuery(sql)) {
|
||||
if (rs.next()) {
|
||||
source = coalesce(rs.getString("view_definition"));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ObjectSource(name, objectType, effectiveSchema(schema), source);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ColumnInfo> getColumns(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
Set<String> primaryKeys = primaryKeys(schema, table);
|
||||
List<ColumnInfo> result = new ArrayList<>();
|
||||
String sql = "SELECT column_name, data_type, is_nullable, column_default, " +
|
||||
"numeric_precision, numeric_scale, character_maximum_length " +
|
||||
"FROM information_schema.columns " +
|
||||
"WHERE table_schema = " + sqlString(effectiveSchema(schema)) +
|
||||
" AND table_name = " + sqlString(table) + " " +
|
||||
"ORDER BY ordinal_position";
|
||||
try (Statement stmt = requireConnected().createStatement()) {
|
||||
try (ResultSet rs = stmt.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
String columnName = rs.getString("column_name");
|
||||
result.add(new ColumnInfo(
|
||||
columnName,
|
||||
rs.getString("data_type"),
|
||||
"YES".equalsIgnoreCase(coalesce(rs.getString("is_nullable"))),
|
||||
rs.getString("column_default"),
|
||||
primaryKeys.contains(columnName),
|
||||
null,
|
||||
null,
|
||||
intObject(rs, "numeric_precision"),
|
||||
intObject(rs, "numeric_scale"),
|
||||
intObject(rs, "character_maximum_length")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IndexInfo> listIndexes(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
Map<String, ConstraintIndexBuilder> indexes = new LinkedHashMap<>();
|
||||
String sql = "SELECT tc.constraint_name, tc.constraint_type, kcu.column_name " +
|
||||
"FROM information_schema.table_constraints tc " +
|
||||
"JOIN information_schema.key_column_usage kcu " +
|
||||
"ON kcu.constraint_schema = tc.constraint_schema " +
|
||||
"AND kcu.constraint_name = tc.constraint_name " +
|
||||
"AND kcu.table_schema = tc.table_schema " +
|
||||
"AND kcu.table_name = tc.table_name " +
|
||||
"WHERE tc.table_schema = " + sqlString(effectiveSchema(schema)) +
|
||||
" AND tc.table_name = " + sqlString(table) + " " +
|
||||
"AND tc.constraint_type IN ('PRIMARY KEY', 'UNIQUE') " +
|
||||
"ORDER BY tc.constraint_name, kcu.ordinal_position";
|
||||
try (Statement stmt = requireConnected().createStatement()) {
|
||||
try (ResultSet rs = stmt.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
String name = rs.getString("constraint_name");
|
||||
String type = rs.getString("constraint_type");
|
||||
ConstraintIndexBuilder builder = indexes.get(name);
|
||||
if (builder == null) {
|
||||
builder = new ConstraintIndexBuilder(name, "PRIMARY KEY".equalsIgnoreCase(type));
|
||||
indexes.put(name, builder);
|
||||
}
|
||||
builder.columns.add(rs.getString("column_name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
List<IndexInfo> result = new ArrayList<>();
|
||||
for (ConstraintIndexBuilder index : indexes.values()) {
|
||||
result.add(new IndexInfo(index.name, index.columns, true, index.primary, null, index.primary ? "PRIMARY KEY" : "UNIQUE", null, null));
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ForeignKeyInfo> listForeignKeys(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
List<ForeignKeyInfo> result = new ArrayList<>();
|
||||
String sql = "SELECT fk.constraint_name, fk.column_name, pk.table_name AS ref_table, pk.column_name AS ref_column " +
|
||||
"FROM information_schema.table_constraints tc " +
|
||||
"JOIN information_schema.key_column_usage fk " +
|
||||
"ON fk.constraint_schema = tc.constraint_schema " +
|
||||
"AND fk.constraint_name = tc.constraint_name " +
|
||||
"AND fk.table_schema = tc.table_schema " +
|
||||
"AND fk.table_name = tc.table_name " +
|
||||
"JOIN information_schema.referential_constraints rc " +
|
||||
"ON rc.constraint_schema = tc.constraint_schema " +
|
||||
"AND rc.constraint_name = tc.constraint_name " +
|
||||
"JOIN information_schema.key_column_usage pk " +
|
||||
"ON pk.constraint_schema = rc.unique_constraint_schema " +
|
||||
"AND pk.constraint_name = rc.unique_constraint_name " +
|
||||
"AND pk.ordinal_position = fk.position_in_unique_constraint " +
|
||||
"WHERE tc.table_schema = " + sqlString(effectiveSchema(schema)) +
|
||||
" AND tc.table_name = " + sqlString(table) + " " +
|
||||
"AND tc.constraint_type = 'FOREIGN KEY' " +
|
||||
"ORDER BY fk.constraint_name, fk.ordinal_position";
|
||||
try (Statement stmt = requireConnected().createStatement()) {
|
||||
try (ResultSet rs = stmt.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
result.add(new ForeignKeyInfo(
|
||||
rs.getString("constraint_name"),
|
||||
rs.getString("column_name"),
|
||||
rs.getString("ref_table"),
|
||||
rs.getString("ref_column")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TriggerInfo> listTriggers(String schema, String table) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String setSchemaSQL(String schema) {
|
||||
return "SET search_path TO " + JdbcIdentifiers.INSTANCE.doubleQuote(effectiveSchema(schema));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object resultValue(ResultSet rs, int index, int sqlType, String columnTypeName) {
|
||||
if (isTemporalType(sqlType, columnTypeName)) {
|
||||
return unchecked(() -> {
|
||||
Object value = rs.getTimestamp(index);
|
||||
return rs.wasNull() ? null : value.toString();
|
||||
});
|
||||
}
|
||||
return super.resultValue(rs, index, sqlType, columnTypeName);
|
||||
}
|
||||
|
||||
private static boolean isTemporalType(int sqlType, String columnTypeName) {
|
||||
switch (sqlType) {
|
||||
case Types.DATE:
|
||||
case Types.TIME:
|
||||
case Types.TIME_WITH_TIMEZONE:
|
||||
case Types.TIMESTAMP:
|
||||
case Types.TIMESTAMP_WITH_TIMEZONE:
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (columnTypeName == null) {
|
||||
return false;
|
||||
}
|
||||
String normalized = columnTypeName.trim().toLowerCase(Locale.ROOT);
|
||||
return normalized.equals("date")
|
||||
|| normalized.equals("time")
|
||||
|| normalized.equals("datetime")
|
||||
|| normalized.startsWith("timestamp");
|
||||
}
|
||||
|
||||
private Set<String> primaryKeys(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
Set<String> primaryKeys = new LinkedHashSet<>();
|
||||
String sql = "SELECT kcu.column_name " +
|
||||
"FROM information_schema.table_constraints tc " +
|
||||
"JOIN information_schema.key_column_usage kcu " +
|
||||
"ON kcu.constraint_schema = tc.constraint_schema " +
|
||||
"AND kcu.constraint_name = tc.constraint_name " +
|
||||
"AND kcu.table_schema = tc.table_schema " +
|
||||
"AND kcu.table_name = tc.table_name " +
|
||||
"WHERE tc.table_schema = " + sqlString(effectiveSchema(schema)) +
|
||||
" AND tc.table_name = " + sqlString(table) + " " +
|
||||
"AND tc.constraint_type = 'PRIMARY KEY' " +
|
||||
"ORDER BY kcu.ordinal_position";
|
||||
try (Statement stmt = requireConnected().createStatement()) {
|
||||
try (ResultSet rs = stmt.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
primaryKeys.add(rs.getString("column_name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
return primaryKeys;
|
||||
});
|
||||
}
|
||||
|
||||
private List<TableInfo> listTables(String schema, String tableTypePredicate) {
|
||||
return unchecked(() -> {
|
||||
List<TableInfo> result = new ArrayList<>();
|
||||
String sql = "SELECT table_name, table_type " +
|
||||
"FROM information_schema.tables " +
|
||||
"WHERE table_schema = " + sqlString(effectiveSchema(schema)) + " AND " + tableTypePredicate + " " +
|
||||
"ORDER BY table_name";
|
||||
try (Statement stmt = requireConnected().createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
result.add(new TableInfo(rs.getString(1), normalizeTableType(rs.getString(2))));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private String effectiveSchema(String schema) {
|
||||
if (schema != null && !schema.trim().isEmpty()) {
|
||||
return schema;
|
||||
}
|
||||
return "PUBLIC";
|
||||
}
|
||||
|
||||
private static Integer intObject(ResultSet rs, String column) throws Exception {
|
||||
Object value = rs.getObject(column);
|
||||
return value instanceof Number ? ((Number) value).intValue() : null;
|
||||
}
|
||||
|
||||
private static String normalizeTableType(String type) {
|
||||
if (type == null || type.trim().isEmpty()) return "TABLE";
|
||||
if ("BASE TABLE".equalsIgnoreCase(type)) return "TABLE";
|
||||
return type;
|
||||
}
|
||||
|
||||
private static String coalesce(String value) {
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
private static String sqlString(String value) {
|
||||
return "'" + coalesce(value).replace("'", "''") + "'";
|
||||
}
|
||||
|
||||
private static final class ConstraintIndexBuilder {
|
||||
final String name;
|
||||
final List<String> columns = new ArrayList<>();
|
||||
final boolean primary;
|
||||
|
||||
ConstraintIndexBuilder(String name, boolean primary) {
|
||||
this.name = name;
|
||||
this.primary = primary;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new JsonRpcServer(new KingbaseAgent()).run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package com.dbx.agent.kingbase;
|
||||
|
||||
import com.dbx.agent.DatabaseAgent;
|
||||
import com.dbx.agent.test.JdbcFakeExecutionBehaviorTest;
|
||||
import com.dbx.agent.test.TestSupport;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class KingbaseAgentTest extends JdbcFakeExecutionBehaviorTest {
|
||||
@Override
|
||||
protected DatabaseAgent createAgent() {
|
||||
return new KingbaseAgent();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String resultSetSql() {
|
||||
return "CALL sample_proc()";
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaresKingbasePostgresLikeProfile() {
|
||||
KingbaseAgent agent = new KingbaseAgent();
|
||||
|
||||
Assertions.assertEquals("com.kingbase8.Driver", agent.getProfile().getDriverClass());
|
||||
Assertions.assertEquals("jdbc:kingbase8://{host}:{port}/{database}", agent.getProfile().getUrlTemplate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mysqlCompatListDatabasesUsesCurrentDatabase() {
|
||||
List<String> sql = new ArrayList<>();
|
||||
KingbaseAgent agent = new KingbaseAgent();
|
||||
agent.setMysqlCompatMode(true);
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(sql, resultSet(
|
||||
new String[]{"database_name"},
|
||||
new Object[][]{{"TEST"}}
|
||||
)));
|
||||
|
||||
Assertions.assertEquals("TEST", agent.listDatabases().get(0).getName());
|
||||
Assertions.assertEquals("SELECT current_database() AS database_name", sql.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mysqlCompatListTablesUsesInformationSchema() {
|
||||
List<String> sql = new ArrayList<>();
|
||||
KingbaseAgent agent = new KingbaseAgent();
|
||||
agent.setMysqlCompatMode(true);
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(sql, resultSet(
|
||||
new String[]{"table_name", "table_type"},
|
||||
new Object[][]{{"test_timestamps", "BASE TABLE"}}
|
||||
)));
|
||||
|
||||
Assertions.assertEquals("test_timestamps", agent.listTables("PUBLIC").get(0).getName());
|
||||
Assertions.assertTrue(sql.get(0).contains("FROM information_schema.tables"));
|
||||
Assertions.assertFalse(sql.get(0).contains("SHOW"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mysqlCompatTimestampTypeNameIsReadAsTimestampText() throws Exception {
|
||||
Timestamp timestamp = Timestamp.valueOf("2026-06-22 11:29:00");
|
||||
KingbaseAgent agent = new KingbaseAgent();
|
||||
agent.setMysqlCompatMode(true);
|
||||
|
||||
Object value = readResultValue(agent, timestampResultSet(timestamp), Types.BINARY, "timestamp");
|
||||
|
||||
Assertions.assertEquals("2026-06-22 11:29:00.0", value);
|
||||
}
|
||||
|
||||
private static Connection preparedConnection(List<String> sql, ResultSet rs) {
|
||||
PreparedStatement statement = proxy(PreparedStatement.class, (method, args) -> {
|
||||
if ("executeQuery".equals(method.getName())) {
|
||||
return rs;
|
||||
}
|
||||
if ("close".equals(method.getName())) {
|
||||
return null;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
Statement plainStatement = proxy(Statement.class, (method, args) -> {
|
||||
if ("executeQuery".equals(method.getName())) {
|
||||
sql.add(String.valueOf(args[0]));
|
||||
return rs;
|
||||
}
|
||||
if ("close".equals(method.getName())) {
|
||||
return null;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
return proxy(Connection.class, (method, args) -> {
|
||||
if ("prepareStatement".equals(method.getName())) {
|
||||
sql.add(String.valueOf(args[0]));
|
||||
return statement;
|
||||
}
|
||||
if ("createStatement".equals(method.getName())) {
|
||||
return plainStatement;
|
||||
}
|
||||
if ("isClosed".equals(method.getName())) {
|
||||
return false;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
}
|
||||
|
||||
private static ResultSet resultSet(String[] columns, Object[][] rows) {
|
||||
int[] index = {-1};
|
||||
return proxy(ResultSet.class, (method, args) -> {
|
||||
switch (method.getName()) {
|
||||
case "next":
|
||||
index[0] += 1;
|
||||
return index[0] < rows.length;
|
||||
case "getString":
|
||||
Object key = args[0];
|
||||
if (key instanceof Number) {
|
||||
return rows[index[0]][((Number) key).intValue() - 1];
|
||||
}
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
if (columns[i].equalsIgnoreCase(String.valueOf(key))) {
|
||||
return rows[index[0]][i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
case "wasNull":
|
||||
return false;
|
||||
case "close":
|
||||
return null;
|
||||
default:
|
||||
return defaultValue(method.getReturnType());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static ResultSet timestampResultSet(Timestamp timestamp) {
|
||||
return proxy(ResultSet.class, (method, args) -> {
|
||||
switch (method.getName()) {
|
||||
case "getTimestamp":
|
||||
return timestamp;
|
||||
case "getBytes":
|
||||
throw new AssertionError("timestamp should not be read as bytes");
|
||||
case "wasNull":
|
||||
return false;
|
||||
default:
|
||||
return defaultValue(method.getReturnType());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static <T> T proxy(Class<T> type, MethodHandler handler) {
|
||||
InvocationHandler invocationHandler = (Object unused, Method method, Object[] args) -> handler.handle(method, args);
|
||||
return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, invocationHandler));
|
||||
}
|
||||
|
||||
private static Object defaultValue(Class<?> type) {
|
||||
if (Boolean.TYPE.equals(type)) return false;
|
||||
if (Byte.TYPE.equals(type)) return (byte) 0;
|
||||
if (Short.TYPE.equals(type)) return (short) 0;
|
||||
if (Integer.TYPE.equals(type)) return 0;
|
||||
if (Long.TYPE.equals(type)) return 0L;
|
||||
if (Float.TYPE.equals(type)) return 0f;
|
||||
if (Double.TYPE.equals(type)) return 0.0d;
|
||||
if (Character.TYPE.equals(type)) return '\0';
|
||||
return null;
|
||||
}
|
||||
|
||||
private interface MethodHandler {
|
||||
Object handle(Method method, Object[] args) throws Throwable;
|
||||
}
|
||||
|
||||
private static Object readResultValue(KingbaseAgent agent, ResultSet rs, int sqlType, String columnTypeName) throws Exception {
|
||||
Method method = KingbaseAgent.class.getDeclaredMethod("resultValue", ResultSet.class, int.class, int.class, String.class);
|
||||
method.setAccessible(true);
|
||||
return method.invoke(agent, rs, 1, sqlType, columnTypeName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user