дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-29 05:52:23 +02:00
feat(database): support DuckDB SQL and browse attached catalogs (#6254)
DuckDB sources get DuckDB's SQL dialect (highlighting, completion, formatting and resolution) and the database tree lists attached catalogs with their schemas, tables and columns. The JDBC bridge gains a catalogs command and catalog-scoped schema and table metadata, matched by exact catalog and schema names; tables in another catalog open and page through the source session. DDL that attaches, detaches or changes a catalog refreshes the tree. Co-authored-by: principalwater <[email protected]>
Цей коміт міститься в:
1 джерело
c04d9381be
коміт
bd396485c2
38 files changed
+1743
-318
No files matched your search
Бінарний файл не відображається.
@@ -124,45 +124,73 @@ public final class OpenIdeaJdbc {
|
||||
return literal.replace(escape, escape + escape).replace("%", escape + "%").replace("_", escape + "_");
|
||||
}
|
||||
|
||||
void schemas(DataOutputStream out) throws Exception {
|
||||
void catalogs(DataOutputStream out) throws Exception {
|
||||
String current = connection.getCatalog();
|
||||
header(out, "catalog", "current");
|
||||
try (ResultSet rs = connection.getMetaData().getCatalogs()) {
|
||||
int n = 0;
|
||||
while (rs.next() && n < 20000) {
|
||||
String name = rs.getString("TABLE_CAT");
|
||||
if (name == null || name.isEmpty()) continue;
|
||||
row(out, name, name.equals(current) ? "1" : "0");
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void schemas(String catalog, DataOutputStream out) throws Exception {
|
||||
var m = connection.getMetaData();
|
||||
header(out, "schema");
|
||||
Set<String> names = new TreeSet<>();
|
||||
try (ResultSet rs = m.getSchemas()) {
|
||||
try (ResultSet rs = catalog == null ? m.getSchemas() : m.getSchemas(catalog, "%")) {
|
||||
while (rs.next() && names.size() < 20000) {
|
||||
if (catalog != null && !catalog.equals(rs.getString("TABLE_CATALOG"))) continue;
|
||||
String s = rs.getString("TABLE_SCHEM");
|
||||
if (s != null && !s.isEmpty()) names.add(s);
|
||||
}
|
||||
} catch (SQLFeatureNotSupportedException ignored) {}
|
||||
catalogs = names.isEmpty();
|
||||
namespaceKnown = true;
|
||||
if (catalogs) {
|
||||
try (ResultSet rs = m.getCatalogs()) {
|
||||
while (rs.next() && names.size() < 20000) {
|
||||
String s = rs.getString(1);
|
||||
if (s != null && !s.isEmpty()) names.add(s);
|
||||
}
|
||||
} catch (SQLFeatureNotSupportedException ignored) {}
|
||||
} catch (SQLFeatureNotSupportedException e) {
|
||||
if (catalog != null) throw e;
|
||||
}
|
||||
if (catalog == null) {
|
||||
catalogs = names.isEmpty();
|
||||
namespaceKnown = true;
|
||||
if (catalogs) {
|
||||
try (ResultSet rs = m.getCatalogs()) {
|
||||
while (rs.next() && names.size() < 20000) {
|
||||
String s = rs.getString(1);
|
||||
if (s != null && !s.isEmpty()) names.add(s);
|
||||
}
|
||||
} catch (SQLFeatureNotSupportedException ignored) {}
|
||||
}
|
||||
if (names.isEmpty()) names.add("");
|
||||
}
|
||||
if (names.isEmpty()) names.add("");
|
||||
for (String s : names) row(out, s);
|
||||
}
|
||||
|
||||
void discoverNamespace() throws Exception {
|
||||
if (!namespaceKnown) schemas(new DataOutputStream(OutputStream.nullOutputStream()));
|
||||
if (!namespaceKnown) schemas(null, new DataOutputStream(OutputStream.nullOutputStream()));
|
||||
}
|
||||
|
||||
static boolean inNamespace(ResultSet rs, String catalog, String schema) throws SQLException {
|
||||
return catalog == null
|
||||
|| (catalog.equals(rs.getString("TABLE_CAT")) && schema.equals(rs.getString("TABLE_SCHEM")));
|
||||
}
|
||||
|
||||
void metadata(String op, List<String> a, DataOutputStream out) throws Exception {
|
||||
discoverNamespace();
|
||||
int catalogIndex = op.equals("columns") ? 2 : 1;
|
||||
String scope = a.size() > catalogIndex ? a.get(catalogIndex) : null;
|
||||
if (scope == null) discoverNamespace();
|
||||
var m = connection.getMetaData();
|
||||
String name = a.get(0);
|
||||
String catalog = catalogs && !name.isEmpty() ? name : connection.getCatalog();
|
||||
String schema = catalogs || name.isEmpty() ? null : pattern(m, name);
|
||||
String catalog = scope != null ? scope : catalogs && !name.isEmpty() ? name : connection.getCatalog();
|
||||
String schemaName = scope == null && (catalogs || name.isEmpty()) ? null : name;
|
||||
String schema = schemaName == null ? null : pattern(m, schemaName);
|
||||
if (op.equals("tables")) {
|
||||
header(out, "name", "kind", "comment");
|
||||
try (ResultSet rs = m.getTables(catalog, schema, "%", null)) {
|
||||
int n = 0;
|
||||
while (rs.next() && n < 20000) {
|
||||
if (!inNamespace(rs, scope, name)) continue;
|
||||
String type = rs.getString("TABLE_TYPE").toLowerCase(Locale.ROOT);
|
||||
if (!Set.of("table","base table","view","materialized view","system table","local temporary","foreign table").contains(type)) continue;
|
||||
row(out, rs.getString("TABLE_NAME"), type, rs.getString("REMARKS"));
|
||||
@@ -175,6 +203,7 @@ public final class OpenIdeaJdbc {
|
||||
try (ResultSet rs = m.getColumns(catalog, schema, "%", "%")) {
|
||||
int n = 0;
|
||||
while (rs.next() && n < 100000) {
|
||||
if (!inNamespace(rs, scope, name)) continue;
|
||||
row(out, rs.getString("TABLE_NAME"), rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"),
|
||||
rs.getInt("NULLABLE") == 0 ? "0" : "1", rs.getString("COLUMN_DEF"), rs.getString("REMARKS"));
|
||||
n++;
|
||||
@@ -184,13 +213,17 @@ public final class OpenIdeaJdbc {
|
||||
header(out, "name", "type", "nullable", "default", "primary");
|
||||
String table = a.get(1);
|
||||
Set<String> keys = new HashSet<>();
|
||||
try (ResultSet rs = m.getPrimaryKeys(catalog, catalogs || name.isEmpty() ? null : name, table)) {
|
||||
while (rs.next()) keys.add(rs.getString("COLUMN_NAME"));
|
||||
try (ResultSet rs = m.getPrimaryKeys(catalog, schemaName, table)) {
|
||||
while (rs.next()) {
|
||||
if (inNamespace(rs, scope, name) && (scope == null || table.equals(rs.getString("TABLE_NAME")))) {
|
||||
keys.add(rs.getString("COLUMN_NAME"));
|
||||
}
|
||||
}
|
||||
} catch (SQLFeatureNotSupportedException ignored) {}
|
||||
try (ResultSet rs = m.getColumns(catalog, schema, pattern(m, table), "%")) {
|
||||
int n = 0;
|
||||
while (rs.next() && n < 20000) {
|
||||
if (!table.equals(rs.getString("TABLE_NAME"))) continue;
|
||||
if (!inNamespace(rs, scope, name) || !table.equals(rs.getString("TABLE_NAME"))) continue;
|
||||
String col = rs.getString("COLUMN_NAME");
|
||||
row(out, col, rs.getString("TYPE_NAME"), rs.getInt("NULLABLE") == 0 ? "0" : "1",
|
||||
rs.getString("COLUMN_DEF"), keys.contains(col) ? "1" : "0");
|
||||
@@ -307,7 +340,8 @@ public final class OpenIdeaJdbc {
|
||||
reply.writeBoolean(true);
|
||||
switch (op) {
|
||||
case "open" -> open(a, reply, opener);
|
||||
case "schemas" -> schemas(reply);
|
||||
case "catalogs" -> catalogs(reply);
|
||||
case "schemas" -> schemas(a.isEmpty() ? null : a.get(0), reply);
|
||||
case "tables", "columns", "schemaColumns" -> metadata(op, a, reply);
|
||||
case "namespace" -> {
|
||||
header(reply, "namespace");
|
||||
|
||||
@@ -36,13 +36,36 @@ public class SelfTest {
|
||||
case "getDatabaseProductVersion" -> "1";
|
||||
case "getIdentifierQuoteString" -> "\"";
|
||||
case "getSearchStringEscape" -> "\\";
|
||||
case "getSchemas" -> rows(List.of(Map.of("TABLE_SCHEM", "app")));
|
||||
case "getTables" -> rows(List.of(Map.of(
|
||||
"TABLE_NAME", "orders", "TABLE_TYPE", "TABLE", "REMARKS", "Orders table")));
|
||||
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'", "REMARKS", "free text"))) : rows(List.of());
|
||||
case "getCatalogs" -> rows(List.of(
|
||||
Map.of("TABLE_CAT", "root"), Map.of("TABLE_CAT", "archive.db:1")));
|
||||
case "getSchemas" -> a == null
|
||||
? rows(List.of(Map.of("TABLE_SCHEM", "app")))
|
||||
: Arrays.equals(a, new Object[]{"archive.db:1", "%"})
|
||||
? rows(List.of(
|
||||
Map.of("TABLE_CATALOG", "archive.db:1", "TABLE_SCHEM", "qa_%"),
|
||||
Map.of("TABLE_CATALOG", "root", "TABLE_SCHEM", "outside_catalog")))
|
||||
: rows(List.of());
|
||||
case "getTables" -> Arrays.equals(a, new Object[]{"archive.db:1", "qa\\_\\%", "%", null})
|
||||
? rows(List.of(
|
||||
Map.of("TABLE_CAT", "archive.db:1", "TABLE_SCHEM", "qa_%", "TABLE_NAME", "items",
|
||||
"TABLE_TYPE", "BASE TABLE", "REMARKS", "Archived items"),
|
||||
Map.of("TABLE_CAT", "root", "TABLE_SCHEM", "qa_%", "TABLE_NAME", "outside_catalog",
|
||||
"TABLE_TYPE", "BASE TABLE", "REMARKS", "Wrong catalog comment"),
|
||||
Map.of("TABLE_CAT", "archive.db:1", "TABLE_SCHEM", "qaABC", "TABLE_NAME", "outside_schema",
|
||||
"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());
|
||||
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",
|
||||
"COLUMN_NAME", "note", "TYPE_NAME", "VARCHAR", "NULLABLE", 1, "REMARKS", "archive label"),
|
||||
Map.of("TABLE_CAT", "root", "TABLE_SCHEM", "qa_%", "TABLE_NAME", "outside_catalog",
|
||||
"COLUMN_NAME", "note", "TYPE_NAME", "VARCHAR", "NULLABLE", 1, "REMARKS", "outside label")))
|
||||
: "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());
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
return proxy(Connection.class, (p, m, a) -> switch (m.getName()) {
|
||||
@@ -55,7 +78,7 @@ public class SelfTest {
|
||||
case "createStatement" -> statement();
|
||||
case "isClosed" -> false;
|
||||
case "isValid" -> !dropped;
|
||||
case "getCatalog" -> null;
|
||||
case "getCatalog" -> "root";
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
}
|
||||
@@ -434,9 +457,30 @@ public class SelfTest {
|
||||
var columns = new String(reply(in), StandardCharsets.UTF_8);
|
||||
assert columns.contains("orders") && columns.contains("note") && columns.contains("'none'") : columns;
|
||||
assert columns.contains("comment") && columns.contains("free text") : columns;
|
||||
send(out, 10, "tables", "app");
|
||||
send(out, 10, "catalogs");
|
||||
var catalogs = reply(in);
|
||||
assert catalogs[0] == 1 : new String(catalogs, StandardCharsets.UTF_8);
|
||||
var catalogRows = new String(catalogs, StandardCharsets.UTF_8);
|
||||
assert catalogRows.contains("archive.db:1") && catalogRows.contains("root") : catalogRows;
|
||||
send(out, 11, "schemas", "archive.db:1");
|
||||
var scoped = new String(reply(in), StandardCharsets.UTF_8);
|
||||
assert scoped.contains("qa_%") && !scoped.contains("outside_catalog") : scoped;
|
||||
send(out, 12, "tables", "qa_%", "archive.db:1");
|
||||
var tables = new String(reply(in), StandardCharsets.UTF_8);
|
||||
assert tables.contains("orders") && tables.contains("Orders table") : tables;
|
||||
assert tables.contains("items") && tables.contains("Archived items")
|
||||
&& !tables.contains("outside_catalog") && !tables.contains("outside_schema")
|
||||
&& !tables.contains("Wrong catalog comment") && !tables.contains("Wrong schema comment") : tables;
|
||||
send(out, 13, "schemas");
|
||||
var unscoped = new String(reply(in), StandardCharsets.UTF_8);
|
||||
assert unscoped.contains("app") && !unscoped.contains("qa_%") : unscoped;
|
||||
send(out, 14, "tables", "app");
|
||||
var genericTables = new String(reply(in), StandardCharsets.UTF_8);
|
||||
assert genericTables.contains("orders") && genericTables.contains("Orders table")
|
||||
&& !genericTables.contains("items") : genericTables;
|
||||
send(out, 15, "schemaColumns", "qa_%", "archive.db:1");
|
||||
var scopedComments = new String(reply(in), StandardCharsets.UTF_8);
|
||||
assert scopedComments.contains("comment") && scopedComments.contains("archive label")
|
||||
&& !scopedComments.contains("outside label") : scopedComments;
|
||||
} finally {
|
||||
child.destroyForcibly().waitFor();
|
||||
}
|
||||
|
||||
Reference in new issue
Заблокувати користувача