diff --git a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java index 1d93411b8..08ffbff14 100644 --- a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java +++ b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java @@ -45,6 +45,14 @@ import java.util.logging.Logger; public final class DbxJdbcPlugin { private static final ObjectMapper MAPPER = new ObjectMapper(); private static final int MAX_ROWS = 10_000; + private static final String[] DEFAULT_TABLE_TYPES = new String[] { + "TABLE", + "VIEW", + "BASE TABLE", + "MATERIALIZED VIEW", + "SYSTEM TABLE", + "SYSTEM VIEW" + }; private static final JdbcDriverQuirks DEFAULT_QUIRKS = new JdbcDriverQuirks( false, false, @@ -793,8 +801,8 @@ public final class DbxJdbcPlugin { if (quirks.useOracleMetadata()) { return oracleListTables(conn, oracleEffectiveSchema(conn, schema)); } - String[] types = new String[] {"TABLE", "VIEW", "MATERIALIZED VIEW", "SYSTEM TABLE", "SYSTEM VIEW"}; DatabaseMetaData meta = conn.getMetaData(); + String[] types = jdbcTableTypes(meta); String catalog = metadataCatalog(database, quirks); String schemaPattern = resolveSchemaPattern(meta, database, schema, quirks); appendTables(result, meta, catalog, schemaPattern, types); @@ -815,7 +823,7 @@ public final class DbxJdbcPlugin { String catalog = metadataCatalog(database, quirks); String schemaPattern = resolveSchemaPattern(meta, database, schema, quirks); - String[] tableTypes = new String[] {"TABLE", "VIEW", "MATERIALIZED VIEW", "SYSTEM TABLE", "SYSTEM VIEW"}; + String[] tableTypes = jdbcTableTypes(meta); appendTableObjects(result, meta, catalog, schemaPattern, schema, tableTypes); if (result.isEmpty() && catalog != null) { appendTableObjects(result, meta, null, schemaPattern, schema, tableTypes); @@ -983,6 +991,27 @@ public final class DbxJdbcPlugin { } } + static String[] jdbcTableTypes(DatabaseMetaData meta) throws SQLException { + Set allowed = new HashSet<>(); + for (String type : DEFAULT_TABLE_TYPES) { + allowed.add(type.toUpperCase(Locale.ROOT)); + } + try (ResultSet rs = meta.getTableTypes()) { + List types = new ArrayList<>(); + while (rs.next()) { + String type = rs.getString("TABLE_TYPE"); + if (type != null && allowed.contains(type.toUpperCase(Locale.ROOT))) { + types.add(type); + } + } + if (!types.isEmpty()) { + return types.toArray(new String[0]); + } + } catch (SQLFeatureNotSupportedException | UnsupportedOperationException ignored) { + } + return DEFAULT_TABLE_TYPES; + } + private static void appendTableObjects( ArrayNode result, DatabaseMetaData meta, @@ -1475,6 +1504,10 @@ public final class DbxJdbcPlugin { byte[] bytes = rs.getBytes(index); return bytes == null ? null : binaryToHex(bytes); } + Object temporalValue = readTemporalValue(rs, meta, index); + if (temporalValue != null) { + return temporalValue; + } if (value instanceof Date || value instanceof Time || value instanceof Timestamp || value instanceof TemporalAccessor) { return value.toString(); } @@ -1487,6 +1520,24 @@ public final class DbxJdbcPlugin { return value.toString(); } + private static Object readTemporalValue(ResultSet rs, ResultSetMetaData meta, int index) throws SQLException { + return switch (meta.getColumnType(index)) { + case Types.DATE -> { + Date date = rs.getDate(index); + yield date == null ? null : date.toString(); + } + case Types.TIME -> { + Time time = rs.getTime(index); + yield time == null ? null : time.toString(); + } + case Types.TIMESTAMP -> { + Timestamp timestamp = rs.getTimestamp(index); + yield timestamp == null ? null : timestamp.toString(); + } + default -> null; + }; + } + private static boolean isBinaryColumn(ResultSetMetaData meta, int index) throws SQLException { return switch (meta.getColumnType(index)) { case Types.BINARY, diff --git a/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java b/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java index 85804fb99..e26163d7d 100644 --- a/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java +++ b/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java @@ -5,11 +5,17 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import java.sql.Connection; -import java.sql.DriverManager; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.Date; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.Statement; +import java.sql.Timestamp; +import java.sql.Types; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -94,6 +100,25 @@ final class DbxJdbcPluginTest { assertEquals("中文测试", response.path("result").path("rows").path(0).path(0).asText()); } + @Test + void readValueFormatsDateColumnsWithoutMidnightTime() throws Exception { + Method method = DbxJdbcPlugin.class.getDeclaredMethod("readValue", ResultSet.class, ResultSetMetaData.class, int.class); + method.setAccessible(true); + ResultSet rs = temporalResultSet(Timestamp.valueOf("2026-06-10 00:00:00"), Date.valueOf("2026-06-10")); + + assertEquals("2026-06-10", method.invoke(null, rs, columnMeta(Types.DATE), 1)); + } + + @Test + void readValueKeepsTimestampTimeComponent() throws Exception { + Method method = DbxJdbcPlugin.class.getDeclaredMethod("readValue", ResultSet.class, ResultSetMetaData.class, int.class); + method.setAccessible(true); + Timestamp timestamp = Timestamp.valueOf("2026-06-10 12:34:56"); + ResultSet rs = temporalResultSet(timestamp, Date.valueOf("2026-06-10")); + + assertEquals("2026-06-10 12:34:56.0", method.invoke(null, rs, columnMeta(Types.TIMESTAMP), 1)); + } + @Test void executeQueryHonorsMaxRowsAndAcceptsExecutionOptions() throws Exception { JsonNode response = request("executeQuery", """ @@ -323,6 +348,21 @@ final class DbxJdbcPluginTest { assertEquals(false, DbxJdbcPlugin.preferSchemaDisplayName("SQLUser", "SQLUSER")); } + @Test + void jdbcTableTypesUsesDriverTypesWithinDefaultAllowList() throws Exception { + String[] types = DbxJdbcPlugin.jdbcTableTypes(tableTypesMeta("TABLE", "LOCAL TEMPORARY", "BASE TABLE")); + + assertEquals(List.of("TABLE", "BASE TABLE"), List.of(types)); + } + + @Test + void jdbcTableTypesFallsBackWhenDriverReturnsNoAllowedTypes() throws Exception { + String[] types = DbxJdbcPlugin.jdbcTableTypes(tableTypesMeta("LOCAL TEMPORARY")); + + assertEquals(true, List.of(types).contains("BASE TABLE")); + assertEquals(true, List.of(types).contains("TABLE")); + } + @Test void sqliteCipherUrlUsesPasswordAsKeyWhenKeyIsMissing() { String url = DbxJdbcPlugin.jdbcUrlWithPasswordKey( @@ -567,6 +607,65 @@ final class DbxJdbcPluginTest { ); } + private static ResultSet temporalResultSet(Object objectValue, Date dateValue) { + return (ResultSet) Proxy.newProxyInstance( + DbxJdbcPluginTest.class.getClassLoader(), + new Class[] { ResultSet.class }, + (proxy, method, args) -> switch (method.getName()) { + case "getObject", "getTimestamp" -> objectValue; + case "getDate" -> dateValue; + case "getBytes" -> null; + default -> null; + } + ); + } + + private static ResultSetMetaData columnMeta(int columnType) { + return (ResultSetMetaData) Proxy.newProxyInstance( + DbxJdbcPluginTest.class.getClassLoader(), + new Class[] { ResultSetMetaData.class }, + (proxy, method, args) -> { + if ("getColumnType".equals(method.getName())) { + return columnType; + } + return null; + } + ); + } + + private static DatabaseMetaData tableTypesMeta(String... types) { + return (DatabaseMetaData) Proxy.newProxyInstance( + DbxJdbcPluginTest.class.getClassLoader(), + new Class[] { DatabaseMetaData.class }, + (proxy, method, args) -> { + if ("getTableTypes".equals(method.getName())) { + return tableTypesResultSet(types); + } + return null; + } + ); + } + + private static ResultSet tableTypesResultSet(String[] types) { + return (ResultSet) Proxy.newProxyInstance( + DbxJdbcPluginTest.class.getClassLoader(), + new Class[] { ResultSet.class }, + new java.lang.reflect.InvocationHandler() { + private int index = -1; + + @Override + public Object invoke(Object proxy, Method method, Object[] args) { + return switch (method.getName()) { + case "next" -> ++index < types.length; + case "getString" -> types[index]; + case "close" -> null; + default -> null; + }; + } + } + ); + } + private static JsonNode request(String method, String params) throws Exception { Method handleLine = DbxJdbcPlugin.class.getDeclaredMethod("handleLine", String.class); handleLine.setAccessible(true);