feat(database): table DDL for custom JDBC sources (#7564)

The JDBC bridge builds CREATE TABLE from JDBC metadata (columns, defaults, primary key, indexes, foreign keys) for custom drivers and reports whether a failure is unsupported, a permission problem or a database error.

Fixes #7451
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-02 23:23:59 +08:00
1 джерело 0d94a8973e
коміт 3c8acf8ee3
5 files changed
+315 -3

No files matched your search

+37 -2
Переглянути файл
@@ -55,7 +55,9 @@ public class SelfTest {
"TABLE_TYPE", "BASE TABLE", "REMARKS", "Wrong schema comment")))
: Arrays.equals(a, new Object[]{"root", "app", "%", null})
? rows(List.of(Map.of("TABLE_NAME", "orders", "TABLE_TYPE", "TABLE", "REMARKS", "Orders table")))
: rows(List.of());
: "app".equals(a[1]) && Set.of("ddl", "denied", "failed", "unsupported").contains(a[2])
? rows(List.of(Map.of("TABLE_CAT", "root", "TABLE_SCHEM", "app", "TABLE_NAME", a[2],
"TABLE_TYPE", "TABLE"))) : rows(List.of());
case "getColumns" -> Arrays.equals(a, new Object[]{"archive.db:1", "qa\\_\\%", "%", "%"})
? rows(List.of(
Map.of("TABLE_CAT", "archive.db:1", "TABLE_SCHEM", "qa_%", "TABLE_NAME", "items",
@@ -65,7 +67,22 @@ public class SelfTest {
: "app".equals(a[1]) && "%".equals(a[2]) ? rows(List.of(
Map.of("TABLE_NAME", "orders", "COLUMN_NAME", "id", "TYPE_NAME", "INT", "NULLABLE", 0),
Map.of("TABLE_NAME", "orders", "COLUMN_NAME", "note", "TYPE_NAME", "VARCHAR", "NULLABLE", 1,
"COLUMN_DEF", "'none'", "REMARKS", "free text"))) : rows(List.of());
"COLUMN_DEF", "'none'", "REMARKS", "free text")))
: rows(List.of(Map.of("TABLE_SCHEM", "app", "TABLE_NAME", a[2], "COLUMN_NAME", "entry\"name",
"TYPE_NAME", "VARCHAR", "DATA_TYPE", Types.VARCHAR, "COLUMN_SIZE", 40, "NULLABLE", 0,
"COLUMN_DEF", "'pending'", "ORDINAL_POSITION", 1)));
case "getPrimaryKeys" -> rows(List.of(Map.of("COLUMN_NAME", "entry\"name", "PK_NAME", "pk",
"KEY_SEQ", 1)));
case "getImportedKeys" -> rows(List.of(Map.of("FK_NAME", "fk", "KEY_SEQ", 1, "FKCOLUMN_NAME", "entry\"name",
"PKTABLE_SCHEM", "app", "PKTABLE_NAME", "parent", "PKCOLUMN_NAME", "name",
"UPDATE_RULE", DatabaseMetaData.importedKeyNoAction, "DELETE_RULE", DatabaseMetaData.importedKeyCascade)));
case "getIndexInfo" -> switch ((String) a[2]) {
case "denied" -> throw new SQLException("cannot read index metadata", "42501");
case "failed" -> throw new SQLException("catalog query failed", "HY000");
case "unsupported" -> throw new SQLFeatureNotSupportedException("index metadata unavailable");
default -> rows(List.of(Map.of("TYPE", (int) DatabaseMetaData.tableIndexOther, "INDEX_NAME", "ix",
"COLUMN_NAME", "entry\"name", "NON_UNIQUE", true, "ASC_OR_DESC", "D", "ORDINAL_POSITION", 1)));
};
default -> throw new SQLFeatureNotSupportedException();
});
return proxy(Connection.class, (p, m, a) -> switch (m.getName()) {
@@ -90,6 +107,7 @@ public class SelfTest {
case "next" -> ++at[0] < data.size();
case "getString" -> (String) data.get(at[0]).get((String) a[0]);
case "getInt" -> (Integer) data.get(at[0]).getOrDefault((String) a[0], 0);
case "getBoolean" -> (Boolean) data.get(at[0]).getOrDefault((String) a[0], false);
case "close" -> null;
default -> throw new SQLFeatureNotSupportedException();
});
@@ -519,6 +537,23 @@ public class SelfTest {
var scopedComments = new String(reply(in), StandardCharsets.UTF_8);
assert scopedComments.contains("comment") && scopedComments.contains("archive label")
&& !scopedComments.contains("outside label") : scopedComments;
send(out, 16, "tableDdl", "app", "ddl");
var ddlReply = reply(in);
String ddl = new String(ddlReply, StandardCharsets.UTF_8);
assert ddlReply[0] == 1 && ddl.contains("CREATE TABLE \"app\".\"ddl\"") : ddl;
assert ddl.contains("\"entry\"\"name\" VARCHAR(40) DEFAULT 'pending' NOT NULL")
&& ddl.contains("CONSTRAINT \"pk\" PRIMARY KEY (\"entry\"\"name\")")
&& ddl.contains("REFERENCES \"app\".\"parent\" (\"name\") ON DELETE CASCADE")
&& ddl.contains("CREATE INDEX \"ix\" ON \"app\".\"ddl\" (\"entry\"\"name\" DESC)") : ddl;
int request = 17;
for (var failure : Map.of("denied", "Permission denied while reading indexes",
"failed", "JDBC metadata query failed while reading indexes",
"unsupported", "The JDBC driver does not support reading indexes").entrySet()) {
send(out, request++, "tableDdl", "app", failure.getKey());
var failureReply = reply(in);
assert failureReply[0] == 0 && new String(failureReply, StandardCharsets.UTF_8).contains(failure.getValue())
: new String(failureReply, StandardCharsets.UTF_8);
}
} finally {
child.destroyForcibly().waitFor();
}