Skip to content

armgit / dbx

Public Imported from GitHub
0

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。

fix(jdbc): preserve Oracle DATE time values

t8y27dabb9e4fb3b
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 f0d89e895a..5978c10cc3 100644
--- a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java
+++ b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java
@@ -691,6 +691,7 @@ public final class DbxJdbcPlugin {
Connection conn = openConnection(connection);
applyExecutionContext(connection, conn, database, schema);
JdbcDriverQuirks quirks = driverQuirks(connection);
+ boolean preserveOracleDateTime = isOracleUrl(jdbcUrl(connection));
try (Statement statement = conn.createStatement()) {
applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks);
String trimmedSql = trimStatementSql(sql);
@@ -715,7 +716,7 @@ public final class DbxJdbcPlugin {
}
ArrayNode row = MAPPER.createArrayNode();
for (int i = 1; i <= columnCount; i++) {
- row.add(MAPPER.valueToTree(readValue(rs, meta, i)));
+ row.add(MAPPER.valueToTree(readValue(rs, meta, i, preserveOracleDateTime)));
}
rows.add(row);
}
@@ -744,6 +745,7 @@ public final class DbxJdbcPlugin {
private final long startNanos;
private final Connection connection;
private final boolean restoreAutoCommit;
+ private final boolean preserveOracleDateTime;
private int rowsReturned;
private ArrayNode pendingRow;
@@ -756,7 +758,8 @@ public final class DbxJdbcPlugin {
int maxRows,
long startNanos,
Connection connection,
- boolean restoreAutoCommit
+ boolean restoreAutoCommit,
+ boolean preserveOracleDateTime
) {
this.id = id;
this.statement = statement;
@@ -767,6 +770,7 @@ public final class DbxJdbcPlugin {
this.startNanos = startNanos;
this.connection = connection;
this.restoreAutoCommit = restoreAutoCommit;
+ this.preserveOracleDateTime = preserveOracleDateTime;
}
}
@@ -784,6 +788,7 @@ public final class DbxJdbcPlugin {
Connection conn = openConnection(connection);
applyExecutionContext(connection, conn, database, schema);
JdbcDriverQuirks quirks = driverQuirks(connection);
+ boolean preserveOracleDateTime = isOracleUrl(jdbcUrl(connection));
boolean restoreAutoCommit = beginPagedQueryTransaction(connection, conn);
Statement statement;
try {
@@ -828,7 +833,8 @@ public final class DbxJdbcPlugin {
maxRows,
start,
conn,
- restoreAutoCommit
+ restoreAutoCommit,
+ preserveOracleDateTime
);
QUERY_SESSIONS.put(sessionId, session);
try {
@@ -901,7 +907,7 @@ public final class DbxJdbcPlugin {
closeQuerySession(session.id);
return queryPageResult(session, rows, false, false);
}
- row = readRow(session.resultSet, session.meta);
+ row = readRow(session.resultSet, session.meta, session.preserveOracleDateTime);
}
rows.add(row);
session.rowsReturned++;
@@ -919,7 +925,7 @@ public final class DbxJdbcPlugin {
return queryPageResult(session, rows, false, false);
}
- session.pendingRow = readRow(session.resultSet, session.meta);
+ session.pendingRow = readRow(session.resultSet, session.meta, session.preserveOracleDateTime);
return queryPageResult(session, rows, false, true);
}
@@ -969,10 +975,14 @@ public final class DbxJdbcPlugin {
}
}
- private static ArrayNode readRow(ResultSet rs, ResultSetMetaData meta) throws SQLException {
+ private static ArrayNode readRow(
+ ResultSet rs,
+ ResultSetMetaData meta,
+ boolean preserveOracleDateTime
+ ) throws SQLException {
ArrayNode row = MAPPER.createArrayNode();
for (int i = 1; i <= meta.getColumnCount(); i++) {
- row.add(MAPPER.valueToTree(readValue(rs, meta, i)));
+ row.add(MAPPER.valueToTree(readValue(rs, meta, i, preserveOracleDateTime)));
}
return row;
}
@@ -2969,7 +2979,12 @@ public final class DbxJdbcPlugin {
return keys;
}
- private static Object readValue(ResultSet rs, ResultSetMetaData meta, int index) throws SQLException {
+ private static Object readValue(
+ ResultSet rs,
+ ResultSetMetaData meta,
+ int index,
+ boolean preserveOracleDateTime
+ ) throws SQLException {
Object value = rs.getObject(index);
if (value == null) {
return null;
@@ -2981,7 +2996,7 @@ public final class DbxJdbcPlugin {
byte[] bytes = rs.getBytes(index);
return bytes == null ? null : binaryToHex(bytes);
}
- Object temporalValue = readTemporalValue(rs, meta, index);
+ Object temporalValue = readTemporalValue(rs, meta, index, preserveOracleDateTime);
if (temporalValue != null) {
return temporalValue;
}
@@ -2997,9 +3012,18 @@ public final class DbxJdbcPlugin {
return value.toString();
}
- private static Object readTemporalValue(ResultSet rs, ResultSetMetaData meta, int index) throws SQLException {
+ private static Object readTemporalValue(
+ ResultSet rs,
+ ResultSetMetaData meta,
+ int index,
+ boolean preserveOracleDateTime
+ ) throws SQLException {
return switch (meta.getColumnType(index)) {
case Types.DATE -> {
+ if (preserveOracleDateTime) {
+ Timestamp timestamp = rs.getTimestamp(index);
+ yield timestamp == null ? null : timestamp.toString();
+ }
Date date = rs.getDate(index);
yield date == null ? null : date.toString();
}
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 81ec9a5638..68c7804d18 100644
--- a/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java
+++ b/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java
@@ -21,6 +21,7 @@ import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
+import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
@@ -250,21 +251,199 @@ final class DbxJdbcPluginTest {
@Test
void readValueFormatsDateColumnsWithoutMidnightTime() throws Exception {
- Method method = DbxJdbcPlugin.class.getDeclaredMethod("readValue", ResultSet.class, ResultSetMetaData.class, int.class);
+ Method method = DbxJdbcPlugin.class.getDeclaredMethod(
+ "readValue",
+ ResultSet.class,
+ ResultSetMetaData.class,
+ int.class,
+ boolean.class
+ );
+ method.setAccessible(true);
+ List<String> calls = new ArrayList<>();
+ ResultSet rs = temporalResultSet(
+ Timestamp.valueOf("2026-06-10 00:00:00"),
+ Date.valueOf("2026-06-10"),
+ calls
+ );
+
+ assertEquals("2026-06-10", method.invoke(null, rs, columnMeta(Types.DATE), 1, false));
+ assertEquals(List.of("getObject", "getDate"), calls);
+ }
+
+ @Test
+ void readValuePreservesOracleDateTimeComponent() throws Exception {
+ Method method = DbxJdbcPlugin.class.getDeclaredMethod(
+ "readValue",
+ ResultSet.class,
+ ResultSetMetaData.class,
+ int.class,
+ boolean.class
+ );
+ method.setAccessible(true);
+ List<String> calls = new ArrayList<>();
+ Timestamp timestamp = Timestamp.valueOf("2026-06-10 12:34:56");
+ ResultSet rs = temporalResultSet(timestamp, Date.valueOf("2026-06-10"), calls);
+
+ assertEquals("2026-06-10 12:34:56.0", method.invoke(null, rs, columnMeta(Types.DATE), 1, true));
+ assertEquals(List.of("getObject", "getTimestamp"), calls);
+ }
+
+ @Test
+ void readValuePreservesOracleDateAtMidnight() throws Exception {
+ Method method = DbxJdbcPlugin.class.getDeclaredMethod(
+ "readValue",
+ ResultSet.class,
+ ResultSetMetaData.class,
+ int.class,
+ boolean.class
+ );
method.setAccessible(true);
- ResultSet rs = temporalResultSet(Timestamp.valueOf("2026-06-10 00:00:00"), Date.valueOf("2026-06-10"));
+ List<String> calls = new ArrayList<>();
+ Timestamp timestamp = Timestamp.valueOf("2026-06-10 00:00:00");
+ ResultSet rs = temporalResultSet(timestamp, Date.valueOf("2026-06-10"), calls);
- assertEquals("2026-06-10", method.invoke(null, rs, columnMeta(Types.DATE), 1));
+ assertEquals("2026-06-10 00:00:00.0", method.invoke(null, rs, columnMeta(Types.DATE), 1, true));
+ assertEquals(List.of("getObject", "getTimestamp"), calls);
}
@Test
void readValueKeepsTimestampTimeComponent() throws Exception {
- Method method = DbxJdbcPlugin.class.getDeclaredMethod("readValue", ResultSet.class, ResultSetMetaData.class, int.class);
+ Method method = DbxJdbcPlugin.class.getDeclaredMethod(
+ "readValue",
+ ResultSet.class,
+ ResultSetMetaData.class,
+ int.class,
+ boolean.class
+ );
method.setAccessible(true);
+ List<String> calls = new ArrayList<>();
Timestamp timestamp = Timestamp.valueOf("2026-06-10 12:34:56");
- ResultSet rs = temporalResultSet(timestamp, Date.valueOf("2026-06-10"));
+ ResultSet rs = temporalResultSet(timestamp, Date.valueOf("2026-06-10"), calls);
+
+ assertEquals("2026-06-10 12:34:56.0", method.invoke(null, rs, columnMeta(Types.TIMESTAMP), 1, false));
+ assertEquals(List.of("getObject", "getTimestamp"), calls);
+ }
+
+ @Test
+ void readValueKeepsTimeColumnsUnchanged() throws Exception {
+ Method method = DbxJdbcPlugin.class.getDeclaredMethod(
+ "readValue",
+ ResultSet.class,
+ ResultSetMetaData.class,
+ int.class,
+ boolean.class
+ );
+ method.setAccessible(true);
+ Time time = Time.valueOf("12:34:56");
+ ResultSet rs = (ResultSet) Proxy.newProxyInstance(
+ DbxJdbcPluginTest.class.getClassLoader(),
+ new Class<?>[] { ResultSet.class },
+ (proxy, invokedMethod, args) -> switch (invokedMethod.getName()) {
+ case "getObject", "getTime" -> time;
+ default -> defaultValue(invokedMethod.getReturnType());
+ }
+ );
+
+ assertEquals("12:34:56", method.invoke(null, rs, columnMeta(Types.TIME), 1, true));
+ }
+
+ @Test
+ void readValueKeepsNullTemporalValues() throws Exception {
+ Method method = DbxJdbcPlugin.class.getDeclaredMethod(
+ "readValue",
+ ResultSet.class,
+ ResultSetMetaData.class,
+ int.class,
+ boolean.class
+ );
+ method.setAccessible(true);
+ List<String> calls = new ArrayList<>();
+ ResultSet rs = temporalResultSet(null, null, calls);
+
+ assertEquals(null, method.invoke(null, rs, columnMeta(Types.DATE), 1, true));
+ assertEquals(List.of("getObject"), calls);
+ }
+
+ @Test
+ void executeQueryPreservesOracleDateTimeComponent() throws Exception {
+ List<String> calls = new ArrayList<>();
+ Driver driver = new OracleDateDriver(
+ new Timestamp[] { Timestamp.valueOf("2026-08-12 10:30:03") },
+ calls
+ );
+ DriverManager.registerDriver(driver);
+ String connection = """
+ { "connection_string": "jdbc:oracle:dbx-date:single" }
+ """;
+ try {
+ JsonNode response = request("executeQuery", """
+ {
+ "connection": %s,
+ "sql": "SELECT created_at FROM events"
+ }
+ """.formatted(connection));
+
+ assertFalse(response.has("error"), response.toString());
+ assertEquals("2026-08-12 10:30:03.0", response.path("result").path("rows").path(0).path(0).asText());
+ assertEquals(List.of("getTimestamp"), calls);
+ } finally {
+ closeAndDeregister(connection, driver);
+ }
+ }
+
+ @Test
+ void pagedQueryPreservesOracleDateTimeAcrossPages() throws Exception {
+ List<String> calls = new ArrayList<>();
+ Driver driver = new OracleDateDriver(
+ new Timestamp[] {
+ Timestamp.valueOf("2026-08-12 10:30:01"),
+ Timestamp.valueOf("2026-08-12 10:30:02"),
+ Timestamp.valueOf("2026-08-12 10:30:03")
+ },
+ calls
+ );
+ DriverManager.registerDriver(driver);
+ String connection = """
+ { "connection_string": "jdbc:oracle:dbx-date:paged" }
+ """;
+ try {
+ JsonNode first = request("executeQueryPage", """
+ {
+ "connection": %s,
+ "sql": "SELECT created_at FROM events",
+ "pageSize": 1,
+ "maxRows": 10
+ }
+ """.formatted(connection));
+
+ assertFalse(first.has("error"), first.toString());
+ assertEquals("2026-08-12 10:30:01.0", first.path("result").path("rows").path(0).path(0).asText());
+ String sessionId = first.path("result").path("session_id").asText();
+
+ JsonNode second = request("fetchQueryPage", """
+ {
+ "connection": %s,
+ "sessionId": "%s",
+ "pageSize": 1
+ }
+ """.formatted(connection, sessionId));
+ assertFalse(second.has("error"), second.toString());
+ assertEquals("2026-08-12 10:30:02.0", second.path("result").path("rows").path(0).path(0).asText());
- assertEquals("2026-06-10 12:34:56.0", method.invoke(null, rs, columnMeta(Types.TIMESTAMP), 1));
+ JsonNode third = request("fetchQueryPage", """
+ {
+ "connection": %s,
+ "sessionId": "%s",
+ "pageSize": 1
+ }
+ """.formatted(connection, second.path("result").path("session_id").asText()));
+ assertFalse(third.has("error"), third.toString());
+ assertEquals("2026-08-12 10:30:03.0", third.path("result").path("rows").path(0).path(0).asText());
+ assertEquals(false, third.path("result").path("has_more").asBoolean());
+ assertEquals(List.of("getTimestamp", "getTimestamp", "getTimestamp"), calls);
+ } finally {
+ closeAndDeregister(connection, driver);
+ }
}
@Test
@@ -1905,15 +2084,20 @@ final class DbxJdbcPluginTest {
);
}
- private static ResultSet temporalResultSet(Object objectValue, Date dateValue) {
+ private static ResultSet temporalResultSet(Object objectValue, Date dateValue, List<String> calls) {
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;
+ (proxy, method, args) -> {
+ if ("getObject".equals(method.getName()) || "getTimestamp".equals(method.getName()) || "getDate".equals(method.getName())) {
+ calls.add(method.getName());
+ }
+ return switch (method.getName()) {
+ case "getObject", "getTimestamp" -> objectValue;
+ case "getDate" -> dateValue;
+ case "getBytes" -> null;
+ default -> null;
+ };
}
);
}
@@ -1923,10 +2107,12 @@ final class DbxJdbcPluginTest {
DbxJdbcPluginTest.class.getClassLoader(),
new Class<?>[] { ResultSetMetaData.class },
(proxy, method, args) -> {
- if ("getColumnType".equals(method.getName())) {
- return columnType;
- }
- return null;
+ return switch (method.getName()) {
+ case "getColumnCount" -> 1;
+ case "getColumnLabel", "getColumnName" -> "CREATED_AT";
+ case "getColumnType" -> columnType;
+ default -> defaultValue(method.getReturnType());
+ };
}
);
}
@@ -2801,6 +2987,108 @@ final class DbxJdbcPluginTest {
);
}
+ private static final class OracleDateDriver implements Driver {
+ private final Timestamp[] values;
+ private final List<String> calls;
+
+ private OracleDateDriver(Timestamp[] values, List<String> calls) {
+ this.values = values;
+ this.calls = calls;
+ }
+
+ @Override
+ public Connection connect(String url, Properties info) {
+ return acceptsURL(url) ? oracleDateConnection(values, calls) : null;
+ }
+
+ @Override
+ public boolean acceptsURL(String url) {
+ return url != null && url.startsWith("jdbc:oracle:dbx-date:");
+ }
+
+ @Override
+ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
+ return new DriverPropertyInfo[0];
+ }
+
+ @Override
+ public int getMajorVersion() {
+ return 1;
+ }
+
+ @Override
+ public int getMinorVersion() {
+ return 0;
+ }
+
+ @Override
+ public boolean jdbcCompliant() {
+ return false;
+ }
+
+ @Override
+ public java.util.logging.Logger getParentLogger() {
+ return java.util.logging.Logger.getGlobal();
+ }
+ }
+
+ private static Connection oracleDateConnection(Timestamp[] values, List<String> calls) {
+ return (Connection) Proxy.newProxyInstance(
+ DbxJdbcPluginTest.class.getClassLoader(),
+ new Class<?>[] { Connection.class },
+ (proxy, method, args) -> switch (method.getName()) {
+ case "createStatement" -> oracleDateStatement(values, calls);
+ case "isClosed" -> false;
+ case "close", "setCatalog", "setSchema" -> null;
+ default -> defaultValue(method.getReturnType());
+ }
+ );
+ }
+
+ private static Statement oracleDateStatement(Timestamp[] values, List<String> calls) {
+ ResultSet resultSet = oracleDateResultSet(values, calls);
+ return (Statement) Proxy.newProxyInstance(
+ DbxJdbcPluginTest.class.getClassLoader(),
+ new Class<?>[] { Statement.class },
+ (proxy, method, args) -> switch (method.getName()) {
+ case "execute" -> true;
+ case "getResultSet" -> resultSet;
+ case "getUpdateCount" -> -1;
+ case "setMaxRows", "setFetchSize", "setQueryTimeout", "close" -> null;
+ default -> defaultValue(method.getReturnType());
+ }
+ );
+ }
+
+ private static ResultSet oracleDateResultSet(Timestamp[] values, List<String> calls) {
+ 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 < values.length;
+ case "getMetaData" -> columnMeta(Types.DATE);
+ case "getObject" -> values[index];
+ case "getDate" -> {
+ calls.add("getDate");
+ yield Date.valueOf(values[index].toLocalDateTime().toLocalDate());
+ }
+ case "getTimestamp" -> {
+ calls.add("getTimestamp");
+ yield values[index];
+ }
+ case "close" -> null;
+ default -> defaultValue(method.getReturnType());
+ };
+ }
+ }
+ );
+ }
+
private static final class BrokenResultSetDriver implements Driver {
private final String urlPrefix;
private final boolean executeReturnsResultSet;