mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(database): complete columns of custom JDBC data sources (#4692)
SQL completion for custom JDBC data sources knew tables but no columns: the JDBC completion model left every table's columns empty (no alias. members, no WHERE columns, no SELECT * expansion). The bridge returns a namespace's columns in one DatabaseMetaData.getColumns call (schemaColumns) and the model fills each table; a driver refusing it still completes tables.
This commit is contained in:
Binary file not shown.
@@ -120,6 +120,17 @@ public final class OpenIdeaJdbc {
|
||||
n++;
|
||||
}
|
||||
}
|
||||
} else if (op.equals("schemaColumns")) {
|
||||
// Every column of the namespace in one call (SQL completion), without primary keys.
|
||||
header(out, "table", "name", "type", "nullable", "default");
|
||||
try (ResultSet rs = m.getColumns(catalog, schema, "%", "%")) {
|
||||
int n = 0;
|
||||
while (rs.next() && n < 100000) {
|
||||
row(out, rs.getString("TABLE_NAME"), rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"),
|
||||
rs.getInt("NULLABLE") == 0 ? "0" : "1", rs.getString("COLUMN_DEF"));
|
||||
n++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
header(out, "name", "type", "nullable", "default", "primary");
|
||||
String table = a.get(1);
|
||||
@@ -233,7 +244,7 @@ public final class OpenIdeaJdbc {
|
||||
switch (op) {
|
||||
case "open" -> open(a, reply);
|
||||
case "schemas" -> schemas(reply);
|
||||
case "tables", "columns" -> metadata(op, a, reply);
|
||||
case "tables", "columns", "schemaColumns" -> metadata(op, a, reply);
|
||||
case "namespace" -> {
|
||||
header(reply, "namespace");
|
||||
String name = null;
|
||||
|
||||
@@ -30,6 +30,12 @@ public class SelfTest {
|
||||
case "getDatabaseProductName" -> "Fixture";
|
||||
case "getDatabaseProductVersion" -> "1";
|
||||
case "getIdentifierQuoteString" -> "\"";
|
||||
case "getSearchStringEscape" -> "\\";
|
||||
case "getSchemas" -> rows(List.of(Map.of("TABLE_SCHEM", "app")));
|
||||
case "getColumns" -> "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'"))) : rows(List.of());
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
return proxy(Connection.class, (p, m, a) -> switch (m.getName()) {
|
||||
@@ -39,6 +45,19 @@ public class SelfTest {
|
||||
case "createStatement" -> statement();
|
||||
case "isClosed" -> false;
|
||||
case "isValid" -> !dropped;
|
||||
case "getCatalog" -> null;
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
}
|
||||
|
||||
/** A forward-only result set over `data`, read by column label. */
|
||||
static ResultSet rows(List<Map<String, Object>> data) {
|
||||
int[] at = {-1};
|
||||
return proxy(ResultSet.class, (p, m, a) -> switch (m.getName()) {
|
||||
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 "close" -> null;
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
}
|
||||
@@ -132,6 +151,9 @@ public class SelfTest {
|
||||
assert reply(in)[0] == 0;
|
||||
send(out, 8, "valid");
|
||||
assert new String(reply(in), StandardCharsets.UTF_8).contains("false");
|
||||
send(out, 9, "schemaColumns", "app");
|
||||
var columns = new String(reply(in), StandardCharsets.UTF_8);
|
||||
assert columns.contains("orders") && columns.contains("note") && columns.contains("'none'") : columns;
|
||||
} finally {
|
||||
child.destroyForcibly().waitFor();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user