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,432 @@
|
||||
package com.dbx.agent.gaussdb;
|
||||
|
||||
import com.dbx.agent.BaseDatabaseAgent;
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.ConnectParams;
|
||||
import com.dbx.agent.DatabaseInfo;
|
||||
import com.dbx.agent.ExecuteQueryOptions;
|
||||
import com.dbx.agent.ForeignKeyInfo;
|
||||
import com.dbx.agent.IndexInfo;
|
||||
import com.dbx.agent.JdbcExecutor;
|
||||
import com.dbx.agent.JdbcIdentifiers;
|
||||
import com.dbx.agent.JsonRpcServer;
|
||||
import com.dbx.agent.ObjectInfo;
|
||||
import com.dbx.agent.ObjectSource;
|
||||
import com.dbx.agent.QueryResult;
|
||||
import com.dbx.agent.TableInfo;
|
||||
import com.dbx.agent.TriggerInfo;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
public final class GaussdbAgent extends BaseDatabaseAgent {
|
||||
private Connection connection;
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(ConnectParams params) {
|
||||
uncheckedVoid(() -> {
|
||||
Class.forName("org.opengauss.Driver");
|
||||
connection = DriverManager.getConnection(buildUrl(params), params.getUsername(), params.getPassword());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testConnection(ConnectParams params) {
|
||||
return unchecked(() -> {
|
||||
Class.forName("org.opengauss.Driver");
|
||||
try (Connection conn = DriverManager.getConnection(buildUrl(params), params.getUsername(), params.getPassword())) {
|
||||
return conn.isValid(5);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DatabaseInfo> listDatabases() {
|
||||
return unchecked(() -> {
|
||||
List<DatabaseInfo> result = new ArrayList<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname"
|
||||
); ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(new DatabaseInfo(rs.getString("datname")));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listSchemas() {
|
||||
return unchecked(() -> {
|
||||
List<String> result = new ArrayList<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('pg_catalog','information_schema','pg_toast') ORDER BY schema_name"
|
||||
); ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(rs.getString("schema_name"));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableInfo> listTables(String schema) {
|
||||
return unchecked(() -> {
|
||||
List<TableInfo> result = new ArrayList<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = ? ORDER BY table_name"
|
||||
)) {
|
||||
stmt.setString(1, schema);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String tableType = rs.getString("table_type");
|
||||
if ("BASE TABLE".equals(tableType)) {
|
||||
tableType = "TABLE";
|
||||
}
|
||||
result.add(new TableInfo(rs.getString("table_name"), tableType, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ObjectInfo> listObjects(String schema) {
|
||||
return unchecked(() -> {
|
||||
List<ObjectInfo> result = new ArrayList<>();
|
||||
for (TableInfo table : listTables(schema)) {
|
||||
result.add(new ObjectInfo(table.getName(), table.getTable_type(), schema, table.getComment()));
|
||||
}
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"SELECT routine_name, routine_type FROM information_schema.routines WHERE routine_schema = ? ORDER BY routine_name"
|
||||
)) {
|
||||
stmt.setString(1, schema);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(new ObjectInfo(rs.getString(1), rs.getString(2), schema, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectSource getObjectSource(String schema, String name, String objectType) {
|
||||
return unchecked(() -> {
|
||||
String upperType = objectType.toUpperCase(Locale.ROOT);
|
||||
String sql = switch (upperType) {
|
||||
case "VIEW" -> "SELECT pg_get_viewdef(to_regclass(?), true)";
|
||||
case "FUNCTION" -> """
|
||||
SELECT pg_get_functiondef(p.oid)
|
||||
FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
|
||||
WHERE n.nspname = ? AND p.proname = ? AND p.prokind = 'f'
|
||||
ORDER BY p.oid LIMIT 1
|
||||
""";
|
||||
case "PROCEDURE" -> """
|
||||
SELECT pg_get_functiondef(p.oid)
|
||||
FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
|
||||
WHERE n.nspname = ? AND p.proname = ? AND p.prokind = 'p'
|
||||
ORDER BY p.oid LIMIT 1
|
||||
""";
|
||||
default -> throw new IllegalArgumentException("Unsupported object type: " + objectType);
|
||||
};
|
||||
|
||||
String source;
|
||||
if ("VIEW".equals(upperType)) {
|
||||
try (var stmt = requireConnected().prepareStatement(sql)) {
|
||||
stmt.setString(1, quoteQualifiedIdentifier(schema, name));
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
source = rs.next() ? coalesce(rs.getString(1)) : "";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try (var stmt = requireConnected().prepareStatement(sql)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, name);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
source = rs.next() ? coalesce(rs.getString(1)) : "";
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ObjectSource(name, objectType, schema, source);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ColumnInfo> getColumns(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
Set<String> primaryKeys = new LinkedHashSet<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"""
|
||||
SELECT kcu.column_name
|
||||
FROM information_schema.table_constraints tc
|
||||
JOIN information_schema.key_column_usage kcu
|
||||
ON tc.constraint_name = kcu.constraint_name
|
||||
AND tc.table_schema = kcu.table_schema
|
||||
WHERE tc.constraint_type = 'PRIMARY KEY'
|
||||
AND tc.table_schema = ?
|
||||
AND tc.table_name = ?
|
||||
"""
|
||||
)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
primaryKeys.add(rs.getString("column_name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ColumnInfo> result = new ArrayList<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"""
|
||||
SELECT column_name, data_type, is_nullable, column_default,
|
||||
numeric_precision, numeric_scale, character_maximum_length
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = ? AND table_name = ?
|
||||
ORDER BY ordinal_position
|
||||
"""
|
||||
)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String columnName = rs.getString("column_name");
|
||||
result.add(new ColumnInfo(
|
||||
columnName,
|
||||
rs.getString("data_type"),
|
||||
"YES".equals(rs.getString("is_nullable")),
|
||||
rs.getString("column_default"),
|
||||
primaryKeys.contains(columnName),
|
||||
null,
|
||||
null,
|
||||
intOrNull(rs, "numeric_precision"),
|
||||
intOrNull(rs, "numeric_scale"),
|
||||
intOrNull(rs, "character_maximum_length")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IndexInfo> listIndexes(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
List<IndexInfo> result = new ArrayList<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"""
|
||||
SELECT
|
||||
i.relname AS index_name,
|
||||
am.amname AS index_type,
|
||||
ix.indisunique AS is_unique,
|
||||
ix.indisprimary AS is_primary,
|
||||
array_agg(a.attname ORDER BY k.n) AS columns
|
||||
FROM pg_index ix
|
||||
JOIN pg_class t ON t.oid = ix.indrelid
|
||||
JOIN pg_class i ON i.oid = ix.indexrelid
|
||||
JOIN pg_namespace n ON n.oid = t.relnamespace
|
||||
JOIN pg_am am ON am.oid = i.relam
|
||||
CROSS JOIN LATERAL unnest(ix.indkey) WITH ORDINALITY AS k(attnum, n)
|
||||
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
||||
WHERE n.nspname = ? AND t.relname = ?
|
||||
GROUP BY i.relname, am.amname, ix.indisunique, ix.indisprimary
|
||||
ORDER BY i.relname
|
||||
"""
|
||||
)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
Object[] columnArray = (Object[]) rs.getArray("columns").getArray();
|
||||
List<String> columns = new ArrayList<>();
|
||||
for (Object column : columnArray) {
|
||||
columns.add(String.valueOf(column));
|
||||
}
|
||||
result.add(new IndexInfo(
|
||||
rs.getString("index_name"),
|
||||
columns,
|
||||
rs.getBoolean("is_unique"),
|
||||
rs.getBoolean("is_primary"),
|
||||
null,
|
||||
rs.getString("index_type"),
|
||||
null,
|
||||
null
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ForeignKeyInfo> listForeignKeys(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
List<ForeignKeyInfo> result = new ArrayList<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"""
|
||||
SELECT
|
||||
tc.constraint_name,
|
||||
kcu.column_name,
|
||||
ccu.table_name AS ref_table,
|
||||
ccu.column_name AS ref_column
|
||||
FROM information_schema.table_constraints tc
|
||||
JOIN information_schema.key_column_usage kcu
|
||||
ON tc.constraint_name = kcu.constraint_name
|
||||
AND tc.table_schema = kcu.table_schema
|
||||
JOIN information_schema.constraint_column_usage ccu
|
||||
ON tc.constraint_name = ccu.constraint_name
|
||||
AND tc.table_schema = ccu.table_schema
|
||||
WHERE tc.constraint_type = 'FOREIGN KEY'
|
||||
AND tc.table_schema = ?
|
||||
AND tc.table_name = ?
|
||||
ORDER BY tc.constraint_name
|
||||
"""
|
||||
)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
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 unchecked(() -> {
|
||||
List<TriggerInfo> result = new ArrayList<>();
|
||||
try (var stmt = requireConnected().prepareStatement(
|
||||
"""
|
||||
SELECT trigger_name, event_manipulation, action_timing
|
||||
FROM information_schema.triggers
|
||||
WHERE trigger_schema = ? AND event_object_table = ?
|
||||
ORDER BY trigger_name
|
||||
"""
|
||||
)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(new TriggerInfo(
|
||||
rs.getString("trigger_name"),
|
||||
rs.getString("event_manipulation"),
|
||||
rs.getString("action_timing")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryResult executeQuery(String sql, String schema, ExecuteQueryOptions options) {
|
||||
return JdbcExecutor.INSTANCE.execute(
|
||||
requireConnected(),
|
||||
sql,
|
||||
schema,
|
||||
this::setSchemaSQL,
|
||||
options.getMaxRows(),
|
||||
options.getFetchSize(),
|
||||
options.getTimeoutSecs(),
|
||||
this::getResultValue
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String setSchemaSQL(String schema) {
|
||||
return "SET search_path TO " + JdbcIdentifiers.INSTANCE.doubleQuote(schema);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
uncheckedVoid(() -> {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
connection = null;
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new JsonRpcServer(new GaussdbAgent()).run();
|
||||
}
|
||||
|
||||
private static String buildUrl(ConnectParams params) {
|
||||
return "jdbc:opengauss://" + params.getHost() + ":" + params.getPort() + "/" + params.getDatabase();
|
||||
}
|
||||
|
||||
private Object getResultValue(ResultSet rs, int index, int sqlType) {
|
||||
return unchecked(() -> {
|
||||
Object value;
|
||||
switch (sqlType) {
|
||||
case Types.BIGINT:
|
||||
value = rs.getLong(index);
|
||||
break;
|
||||
case Types.INTEGER:
|
||||
case Types.SMALLINT:
|
||||
case Types.TINYINT:
|
||||
value = rs.getInt(index);
|
||||
break;
|
||||
case Types.FLOAT:
|
||||
case Types.REAL:
|
||||
value = rs.getFloat(index);
|
||||
break;
|
||||
case Types.DOUBLE:
|
||||
value = rs.getDouble(index);
|
||||
break;
|
||||
case Types.DECIMAL:
|
||||
case Types.NUMERIC:
|
||||
value = rs.getBigDecimal(index);
|
||||
break;
|
||||
case Types.BOOLEAN:
|
||||
case Types.BIT:
|
||||
value = rs.getBoolean(index);
|
||||
break;
|
||||
default:
|
||||
value = rs.getString(index);
|
||||
break;
|
||||
}
|
||||
return rs.wasNull() ? null : value;
|
||||
});
|
||||
}
|
||||
|
||||
private static String coalesce(String value) {
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
private static String quoteQualifiedIdentifier(String schema, String name) {
|
||||
return JdbcIdentifiers.INSTANCE.doubleQuote(schema) + "." + JdbcIdentifiers.INSTANCE.doubleQuote(name);
|
||||
}
|
||||
|
||||
private static Integer intOrNull(ResultSet rs, String column) throws Exception {
|
||||
Object value = rs.getObject(column);
|
||||
return value instanceof Number ? ((Number) value).intValue() : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.dbx.agent.gaussdb;
|
||||
|
||||
import com.dbx.agent.BaseDatabaseAgent;
|
||||
import com.dbx.agent.DatabaseAgent;
|
||||
import com.dbx.agent.test.JdbcFakeExecutionBehaviorTest;
|
||||
import com.dbx.agent.test.JdbcMetadataSqlFake;
|
||||
import com.dbx.agent.test.TestSupport;
|
||||
import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class GaussdbAgentTest extends JdbcFakeExecutionBehaviorTest {
|
||||
@Override
|
||||
protected DatabaseAgent createAgent() {
|
||||
return new GaussdbAgent();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String resultSetSql() {
|
||||
return "CALL sample_proc()";
|
||||
}
|
||||
|
||||
@Test
|
||||
void agentExtendsBaseDatabaseAgent() {
|
||||
Assertions.assertTrue(BaseDatabaseAgent.class.isAssignableFrom(GaussdbAgent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsViewSourceWithQuotedRegclassParameter() {
|
||||
GaussdbAgent agent = new GaussdbAgent();
|
||||
TestSupport.setPrivateConnection(agent, JdbcMetadataSqlFake.connection());
|
||||
|
||||
agent.getObjectSource("bad\"schema", "view's name", "VIEW");
|
||||
|
||||
Assertions.assertEquals(
|
||||
Arrays.asList(
|
||||
"SELECT pg_get_viewdef(to_regclass(?), true)",
|
||||
"param:1=\"bad\"\"schema\".\"view's name\""
|
||||
),
|
||||
JdbcMetadataSqlFake.statements
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user