25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(jdbc): support catalogless metadata fallback
t8y21d57bc575f5a
diff --git a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.javaindex 712524cdff..04c120e767 100644--- a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java+++ b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java@@ -109,7 +109,11 @@ public final class DbxJdbcPlugin {case "listDatabases" -> listDatabases(connection);case "listSchemas" -> listSchemas(connection, optionalText(params, "database"));case "listTables" -> listTables(connection, optionalText(params, "database"), optionalText(params, "schema"));- case "list_objects" -> listObjects(connection, optionalText(params, "database"), optionalText(params, "schema"));+ case "listObjects", "list_objects" -> listObjects(+ connection,+ optionalText(params, "database"),+ optionalText(params, "schema")+ );case "getColumns" -> getColumns(connection,optionalText(params, "database"),@@ -269,20 +273,16 @@ public final class DbxJdbcPlugin {Connection conn = openConnection(connection);DatabaseMetaData meta = conn.getMetaData();try (ResultSet rs = meta.getSchemas(emptyToNull(database), null)) {- while (rs.next()) {- String schema = rs.getString("TABLE_SCHEM");- if (schema != null && !schema.isBlank()) {- result.add(schema);- }- }+ appendSchemas(result, rs);} catch (SQLFeatureNotSupportedException ignored) {try (ResultSet rs = meta.getSchemas()) {- while (rs.next()) {- String schema = rs.getString("TABLE_SCHEM");- if (schema != null && !schema.isBlank()) {- result.add(schema);- }- }+ appendSchemas(result, rs);+ }+ }+ if (result.isEmpty() && database != null) {+ try (ResultSet rs = meta.getSchemas(null, null)) {+ appendSchemas(result, rs);+ } catch (SQLFeatureNotSupportedException ignored) {}}if (result.isEmpty() && conn.getSchema() != null) {@@ -295,14 +295,10 @@ public final class DbxJdbcPlugin {ArrayNode result = MAPPER.createArrayNode();String[] types = new String[] {"TABLE", "VIEW", "MATERIALIZED VIEW", "SYSTEM TABLE", "SYSTEM VIEW"};Connection conn = openConnection(connection);- try (ResultSet rs = conn.getMetaData().getTables(emptyToNull(database), emptyToNull(schema), "%", types)) {- while (rs.next()) {- ObjectNode item = MAPPER.createObjectNode();- item.put("name", rs.getString("TABLE_NAME"));- item.put("table_type", rs.getString("TABLE_TYPE"));- putNullable(item, "comment", rs.getString("REMARKS"));- result.add(item);- }+ DatabaseMetaData meta = conn.getMetaData();+ appendTables(result, meta, emptyToNull(database), emptyToNull(schema), types);+ if (result.isEmpty() && database != null) {+ appendTables(result, meta, null, emptyToNull(schema), types);}return result;}@@ -315,15 +311,9 @@ public final class DbxJdbcPlugin {String schemaPattern = emptyToNull(schema);String[] tableTypes = new String[] {"TABLE", "VIEW", "MATERIALIZED VIEW", "SYSTEM TABLE", "SYSTEM VIEW"};- try (ResultSet rs = meta.getTables(catalog, schemaPattern, "%", tableTypes)) {- while (rs.next()) {- ObjectNode item = MAPPER.createObjectNode();- item.put("name", rs.getString("TABLE_NAME"));- item.put("object_type", rs.getString("TABLE_TYPE"));- putNullable(item, "schema", schema);- putNullable(item, "comment", rs.getString("REMARKS"));- result.add(item);- }+ appendTableObjects(result, meta, catalog, schemaPattern, schema, tableTypes);+ if (result.isEmpty() && database != null) {+ appendTableObjects(result, meta, null, schemaPattern, schema, tableTypes);}try (ResultSet rs = meta.getProcedures(catalog, schemaPattern, "%")) {@@ -367,26 +357,88 @@ public final class DbxJdbcPlugin {Connection conn = openConnection(connection);DatabaseMetaData meta = conn.getMetaData();Set<String> primaryKeys = primaryKeys(meta, database, schema, table);- try (ResultSet rs = meta.getColumns(emptyToNull(database), emptyToNull(schema), table, "%")) {- while (rs.next()) {- String name = rs.getString("COLUMN_NAME");- ObjectNode item = MAPPER.createObjectNode();- item.put("name", name);- item.put("data_type", rs.getString("TYPE_NAME"));- item.put("is_nullable", rs.getInt("NULLABLE") != DatabaseMetaData.columnNoNulls);- putNullable(item, "column_default", rs.getString("COLUMN_DEF"));- item.put("is_primary_key", primaryKeys.contains(name));- item.putNull("extra");- putNullable(item, "comment", rs.getString("REMARKS"));- putNullableInt(item, "numeric_precision", rs.getObject("COLUMN_SIZE"));- putNullableInt(item, "numeric_scale", rs.getObject("DECIMAL_DIGITS"));- putNullableInt(item, "character_maximum_length", rs.getObject("COLUMN_SIZE"));- result.add(item);- }+ appendColumns(result, meta, emptyToNull(database), emptyToNull(schema), table, primaryKeys);+ if (result.isEmpty() && database != null) {+ primaryKeys = primaryKeys(meta, null, schema, table);+ appendColumns(result, meta, null, emptyToNull(schema), table, primaryKeys);}return result;}+ private static void appendSchemas(ArrayNode result, ResultSet rs) throws SQLException {+ while (rs.next()) {+ String schema = rs.getString("TABLE_SCHEM");+ if (schema != null && !schema.isBlank()) {+ result.add(schema);+ }+ }+ }++ private static void appendTables(+ ArrayNode result,+ DatabaseMetaData meta,+ String catalog,+ String schema,+ String[] types+ ) throws SQLException {+ try (ResultSet rs = meta.getTables(catalog, schema, "%", types)) {+ while (rs.next()) {+ ObjectNode item = MAPPER.createObjectNode();+ item.put("name", rs.getString("TABLE_NAME"));+ item.put("table_type", rs.getString("TABLE_TYPE"));+ putNullable(item, "comment", rs.getString("REMARKS"));+ result.add(item);+ }+ }+ }++ private static void appendTableObjects(+ ArrayNode result,+ DatabaseMetaData meta,+ String catalog,+ String schemaPattern,+ String schema,+ String[] tableTypes+ ) throws SQLException {+ try (ResultSet rs = meta.getTables(catalog, schemaPattern, "%", tableTypes)) {+ while (rs.next()) {+ ObjectNode item = MAPPER.createObjectNode();+ item.put("name", rs.getString("TABLE_NAME"));+ item.put("object_type", rs.getString("TABLE_TYPE"));+ putNullable(item, "schema", schema);+ putNullable(item, "comment", rs.getString("REMARKS"));+ result.add(item);+ }+ }+ }++ private static void appendColumns(+ ArrayNode result,+ DatabaseMetaData meta,+ String catalog,+ String schema,+ String table,+ Set<String> primaryKeys+ ) throws SQLException {+ try (ResultSet rs = meta.getColumns(catalog, schema, table, "%")) {+ while (rs.next()) {+ String name = rs.getString("COLUMN_NAME");+ ObjectNode item = MAPPER.createObjectNode();+ item.put("name", name);+ item.put("data_type", rs.getString("TYPE_NAME"));+ item.put("is_nullable", rs.getInt("NULLABLE") != DatabaseMetaData.columnNoNulls);+ putNullable(item, "column_default", rs.getString("COLUMN_DEF"));+ item.put("is_primary_key", primaryKeys.contains(name));+ item.putNull("extra");+ putNullable(item, "comment", rs.getString("REMARKS"));+ putNullableInt(item, "numeric_precision", rs.getObject("COLUMN_SIZE"));+ putNullableInt(item, "numeric_scale", rs.getObject("DECIMAL_DIGITS"));+ putNullableInt(item, "character_maximum_length", rs.getObject("COLUMN_SIZE"));+ result.add(item);+ }+ }+ }+private static void closeSharedConnection() {if (sharedConnection != null) {try {diff --git a/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java b/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.javaindex 1e1e2ec558..c6cd7d0884 100644--- a/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java+++ b/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java@@ -47,6 +47,82 @@ final class DbxJdbcPluginTest {assertEquals("APP", response.path("result").path("rows").path(0).path(0).asText());}+ @Test+ void listTablesFallsBackWhenCatalogFiltersEverything() throws Exception {+ request("executeQuery", """+ {+ "connection": %s,+ "sql": "CREATE SCHEMA IF NOT EXISTS app"+ }+ """.formatted(CONNECTION));+ request("executeQuery", """+ {+ "connection": %s,+ "sql": "CREATE TABLE IF NOT EXISTS app.people (id INT PRIMARY KEY, name VARCHAR(30))"+ }+ """.formatted(CONNECTION));++ JsonNode response = request("listTables", """+ {+ "connection": %s,+ "database": "UNRELATED_CATALOG",+ "schema": "APP"+ }+ """.formatted(CONNECTION));++ assertFalse(response.has("error"), response.toString());+ assertEquals("PEOPLE", response.path("result").path(0).path("name").asText());+ }++ @Test+ void listObjectsAcceptsCamelCaseMethodAndFallsBackWhenCatalogFiltersEverything() throws Exception {+ createPeopleTable();++ JsonNode response = request("listObjects", """+ {+ "connection": %s,+ "database": "UNRELATED_CATALOG",+ "schema": "APP"+ }+ """.formatted(CONNECTION));++ assertFalse(response.has("error"), response.toString());+ assertEquals("PEOPLE", response.path("result").path(0).path("name").asText());+ }++ @Test+ void getColumnsFallsBackWhenCatalogFiltersEverything() throws Exception {+ createPeopleTable();++ JsonNode response = request("getColumns", """+ {+ "connection": %s,+ "database": "UNRELATED_CATALOG",+ "schema": "APP",+ "table": "PEOPLE"+ }+ """.formatted(CONNECTION));++ assertFalse(response.has("error"), response.toString());+ assertEquals("ID", response.path("result").path(0).path("name").asText());+ assertEquals(true, response.path("result").path(0).path("is_primary_key").asBoolean());+ }++ private static void createPeopleTable() throws Exception {+ request("executeQuery", """+ {+ "connection": %s,+ "sql": "CREATE SCHEMA IF NOT EXISTS app"+ }+ """.formatted(CONNECTION));+ request("executeQuery", """+ {+ "connection": %s,+ "sql": "CREATE TABLE IF NOT EXISTS app.people (id INT PRIMARY KEY, name VARCHAR(30))"+ }+ """.formatted(CONNECTION));+ }+private static JsonNode request(String method, String params) throws Exception {Method handleLine = DbxJdbcPlugin.class.getDeclaredMethod("handleLine", String.class);handleLine.setAccessible(true);