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 full precision for InterSystems integer result values

t8y21e78e4e9d81e
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 4b0a338689..60b912be0b 100644
--- a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java
+++ b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java
@@ -881,6 +881,16 @@ public final class DbxJdbcPlugin {
return false;
}
+ private static boolean usesFullPrecisionIntegers(JsonNode connection) {
+ String url = jdbcUrl(connection);
+ if (urlMatchesPrefix(url, "jdbc:cache:") || urlMatchesPrefix(url, "jdbc:iris:")) {
+ return true;
+ }
+ String driverClass = optionalText(connection, "jdbc_driver_class");
+ return "com.intersys.jdbc.CacheDriver".equals(driverClass)
+ || "com.intersystems.jdbc.IRISDriver".equals(driverClass);
+ }
+
private static boolean isPrestoOrTrinoConnection(JsonNode connection) {
String url = jdbcUrl(connection);
if (urlMatchesPrefix(url, "jdbc:presto:") || urlMatchesPrefix(url, "jdbc:trino:")) {
@@ -1022,6 +1032,7 @@ public final class DbxJdbcPlugin {
boolean preserveOracleDateTime = isOracleUrl(jdbcUrl(connection));
ZoneId timestampZone = tdengineTimestampZone(connection, conn);
boolean bitStringColumns = usesBitStringColumns(connection);
+ boolean fullPrecisionIntegers = usesFullPrecisionIntegers(connection);
try (Statement statement = conn.createStatement()) {
applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks);
String trimmedSql = trimStatementSql(sql);
@@ -1051,7 +1062,7 @@ public final class DbxJdbcPlugin {
}
ArrayNode row = MAPPER.createArrayNode();
for (int i = 1; i <= columnCount; i++) {
- row.add(MAPPER.valueToTree(readValue(rs, meta, i, preserveOracleDateTime, timestampZone, bitStringColumns)));
+ row.add(MAPPER.valueToTree(readValue(rs, meta, i, preserveOracleDateTime, timestampZone, bitStringColumns, fullPrecisionIntegers)));
}
rows.add(row);
}
@@ -1146,6 +1157,7 @@ public final class DbxJdbcPlugin {
private final boolean preserveOracleDateTime;
private final ZoneId timestampZone;
private final boolean bitStringColumns;
+ private final boolean fullPrecisionIntegers;
private int rowsReturned;
private ArrayNode pendingRow;
@@ -1161,7 +1173,8 @@ public final class DbxJdbcPlugin {
boolean restoreAutoCommit,
boolean preserveOracleDateTime,
ZoneId timestampZone,
- boolean bitStringColumns
+ boolean bitStringColumns,
+ boolean fullPrecisionIntegers
) {
this.id = id;
this.statement = statement;
@@ -1175,6 +1188,7 @@ public final class DbxJdbcPlugin {
this.preserveOracleDateTime = preserveOracleDateTime;
this.timestampZone = timestampZone;
this.bitStringColumns = bitStringColumns;
+ this.fullPrecisionIntegers = fullPrecisionIntegers;
}
}
@@ -1243,7 +1257,8 @@ public final class DbxJdbcPlugin {
restoreAutoCommit,
preserveOracleDateTime,
timestampZone,
- bitStringColumns
+ bitStringColumns,
+ usesFullPrecisionIntegers(connection)
);
QUERY_SESSIONS.put(sessionId, session);
try {
@@ -1316,7 +1331,7 @@ public final class DbxJdbcPlugin {
closeQuerySession(session.id);
return queryPageResult(session, rows, false, false);
}
- row = readRow(session.resultSet, session.meta, session.preserveOracleDateTime, session.timestampZone, session.bitStringColumns);
+ row = readRow(session.resultSet, session.meta, session.preserveOracleDateTime, session.timestampZone, session.bitStringColumns, session.fullPrecisionIntegers);
}
rows.add(row);
session.rowsReturned++;
@@ -1334,7 +1349,7 @@ public final class DbxJdbcPlugin {
return queryPageResult(session, rows, false, false);
}
- session.pendingRow = readRow(session.resultSet, session.meta, session.preserveOracleDateTime, session.timestampZone, session.bitStringColumns);
+ session.pendingRow = readRow(session.resultSet, session.meta, session.preserveOracleDateTime, session.timestampZone, session.bitStringColumns, session.fullPrecisionIntegers);
return queryPageResult(session, rows, false, true);
}
@@ -1389,11 +1404,12 @@ public final class DbxJdbcPlugin {
ResultSetMetaData meta,
boolean preserveOracleDateTime,
ZoneId timestampZone,
- boolean bitStringColumns
+ boolean bitStringColumns,
+ boolean fullPrecisionIntegers
) throws SQLException {
ArrayNode row = MAPPER.createArrayNode();
for (int i = 1; i <= meta.getColumnCount(); i++) {
- row.add(MAPPER.valueToTree(readValue(rs, meta, i, preserveOracleDateTime, timestampZone, bitStringColumns)));
+ row.add(MAPPER.valueToTree(readValue(rs, meta, i, preserveOracleDateTime, timestampZone, bitStringColumns, fullPrecisionIntegers)));
}
return row;
}
@@ -4355,9 +4371,26 @@ public final class DbxJdbcPlugin {
boolean preserveOracleDateTime,
ZoneId timestampZone,
boolean bitStringColumns
+ ) throws SQLException {
+ return readValue(rs, meta, index, preserveOracleDateTime, timestampZone, bitStringColumns, false);
+ }
+
+ private static Object readValue(
+ ResultSet rs,
+ ResultSetMetaData meta,
+ int index,
+ boolean preserveOracleDateTime,
+ ZoneId timestampZone,
+ boolean bitStringColumns,
+ boolean fullPrecisionIntegers
) throws SQLException {
int columnType = meta.getColumnType(index);
+ if (fullPrecisionIntegers && (columnType == Types.TINYINT || columnType == Types.SMALLINT
+ || columnType == Types.INTEGER || columnType == Types.BIGINT)) {
+ return rs.getBigDecimal(index);
+ }
+
if (columnType == Types.BOOLEAN) {
boolean boolValue = rs.getBoolean(index);
if (!rs.wasNull()) {
diff --git a/plugins/jdbc/src/test/java/app/dbx/jdbc/InterSystemsNumericTest.java b/plugins/jdbc/src/test/java/app/dbx/jdbc/InterSystemsNumericTest.java
new file mode 100644
index 0000000000..3866329eff
--- /dev/null
+++ b/plugins/jdbc/src/test/java/app/dbx/jdbc/InterSystemsNumericTest.java
@@ -0,0 +1,237 @@
+package app.dbx.jdbc;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+final class InterSystemsNumericTest {
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ @AfterEach
+ void close() throws Exception {
+ call("close", MAPPER.createObjectNode());
+ set("registeredDriver", null);
+ set("registeredDriverKey", "");
+ }
+
+ @Test
+ void preservesIntegerPrecisionInQueriesAndCursorPages() throws Exception {
+ String[] values = { "2147483648", "-2147483649", "9223372036854775807",
+ "9223372036854775808", "-9223372036854775809", "0", null, "42" };
+ for (String url : new String[] { "jdbc:Cache://localhost/USER", "jdbc:IRIS://localhost/USER" }) {
+ for (int type : new int[] { Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.BIGINT }) {
+ Fixture fixture = new Fixture(url, type, values, null);
+ JsonNode query = fixture.query("executeQuery");
+ assertFalse(query.has("error"), query.toString());
+ for (int i = 0; i < values.length; i++) {
+ assertValue(values[i], query.path("result").path("rows").path(i).path(0));
+ }
+ JsonNode page = fixture.query("executeQueryPage");
+ int row = 0;
+ while (true) {
+ assertFalse(page.has("error"), page.toString());
+ for (JsonNode item : page.path("result").path("rows")) assertValue(values[row++], item.path(0));
+ if (!page.path("result").path("has_more").asBoolean()) break;
+ ObjectNode params = fixture.params();
+ params.put("sessionId", page.path("result").path("session_id").asText());
+ page = call("fetchQueryPage", params);
+ }
+ assertEquals(values.length, row);
+ assertEquals(2, fixture.closedStatements);
+ close();
+ }
+ }
+ }
+
+ @Test
+ void preservesOtherTypesAndDoesNotTrustVendorNamesInUserText() throws Exception {
+ for (int type : new int[] { Types.LONGVARCHAR, Types.DECIMAL, Types.BOOLEAN,
+ Types.VARBINARY, Types.TIMESTAMP }) {
+ Fixture fixture = new Fixture("jdbc:Cache://localhost/USER", type, new String[] { "42" }, null);
+ JsonNode response = fixture.query("executeQuery");
+ assertFalse(response.has("error"), response.toString());
+ assertEquals(0, fixture.decimalReads);
+ if (type == Types.LONGVARCHAR) assertEquals("42", response.path("result").path("rows").path(0).path(0).asText());
+ if (type == Types.BOOLEAN) assertTrue(response.path("result").path("rows").path(0).path(0).asBoolean());
+ close();
+ }
+ Fixture fixture = new Fixture("jdbc:other:cache", Types.INTEGER, new String[] { "2147483648" }, null);
+ fixture.config.put("name", "InterSystems Caché");
+ fixture.config.put("driver_profile", "cache");
+ fixture.install();
+ JsonNode response = fixture.query("executeQuery");
+ assertEquals("Numeric value out of range", response.path("error").path("message").asText());
+ assertEquals(0, fixture.decimalReads);
+ }
+
+ @Test
+ void preservesDriverFailuresAndClosesFailedQueryStatements() throws Exception {
+ for (String method : new String[] { "executeQuery", "executeQueryPage" }) {
+ Fixture fixture = new Fixture("jdbc:Cache://localhost/USER", Types.INTEGER,
+ new String[] { "42" }, new SQLException("numeric transport failed", "08006"));
+ JsonNode response = fixture.query(method);
+ assertEquals("numeric transport failed", response.path("error").path("message").asText());
+ assertEquals(1, fixture.closedStatements);
+ close();
+ }
+ }
+
+ @Test
+ void recognizesOnlyExactDriverClassesAndClosesCursorEarly() throws Exception {
+ for (String driver : new String[] { "com.intersys.jdbc.CacheDriver", "com.intersystems.jdbc.IRISDriver" }) {
+ Fixture fixture = new Fixture("jdbc:custom:local", Types.INTEGER,
+ new String[] { "2147483648", "-2147483649", "42" }, null);
+ fixture.config.put("jdbc_driver_class", driver);
+ fixture.install();
+ JsonNode page = fixture.query("executeQueryPage");
+ assertFalse(page.has("error"), page.toString());
+ String id = page.path("result").path("session_id").asText();
+ ObjectNode params = fixture.params().put("sessionId", id);
+ assertTrue(call("closeQuerySession", params).path("result").path("ok").asBoolean());
+ assertTrue(call("fetchQueryPage", params).has("error"));
+ assertEquals(1, fixture.closedStatements);
+ close();
+ }
+ }
+
+ private static void assertValue(String expected, JsonNode actual) {
+ if (expected == null) assertTrue(actual.isNull());
+ else {
+ assertTrue(actual.isNumber(), actual.toString());
+ assertEquals(0, new BigDecimal(expected).compareTo(actual.decimalValue()));
+ assertEquals(expected, actual.toString());
+ }
+ }
+
+ private static final class Fixture {
+ final ObjectNode config = MAPPER.createObjectNode();
+ final int type;
+ final String[] values;
+ final SQLException failure;
+ int decimalReads;
+ int closedStatements;
+
+ Fixture(String url, int type, String[] values, SQLException failure) throws Exception {
+ this.type = type;
+ this.values = values;
+ this.failure = failure;
+ config.put("connection_string", url);
+ install();
+ }
+
+ void install() throws Exception {
+ Connection connection = proxy(Connection.class, (p, m, a) -> switch (m.getName()) {
+ case "createStatement" -> statement();
+ case "getAutoCommit" -> true;
+ default -> defaultValue(m.getReturnType());
+ });
+ set("registeredDriver", proxy(Driver.class, (p, m, a) -> defaultValue(m.getReturnType())));
+ set("registeredDriverKey", key("driverKey"));
+ set("sharedConnection", connection);
+ set("sharedConnectionKey", key("connectionKey"));
+ }
+
+ Object key(String name) throws Exception {
+ Method method = DbxJdbcPlugin.class.getDeclaredMethod(name, JsonNode.class);
+ method.setAccessible(true);
+ return method.invoke(null, config);
+ }
+
+ ObjectNode params() {
+ ObjectNode params = MAPPER.createObjectNode();
+ params.set("connection", config);
+ params.put("sql", "SELECT value FROM sample");
+ params.put("pageSize", 2);
+ return params;
+ }
+
+ JsonNode query(String method) throws Exception { return call(method, params()); }
+
+ Statement statement() {
+ ResultSet result = resultSet();
+ return proxy(Statement.class, (p, m, a) -> switch (m.getName()) {
+ case "execute" -> true;
+ case "getResultSet", "executeQuery" -> result;
+ case "close" -> { closedStatements++; yield null; }
+ default -> defaultValue(m.getReturnType());
+ });
+ }
+
+ ResultSet resultSet() {
+ int[] row = { -1 };
+ ResultSetMetaData meta = proxy(ResultSetMetaData.class, (p, m, a) -> switch (m.getName()) {
+ case "getColumnCount" -> 1;
+ case "getColumnType" -> type;
+ case "getColumnLabel", "getColumnName" -> "value";
+ default -> defaultValue(m.getReturnType());
+ });
+ return proxy(ResultSet.class, (p, m, a) -> switch (m.getName()) {
+ case "next" -> ++row[0] < values.length;
+ case "getMetaData" -> meta;
+ case "getBigDecimal" -> {
+ decimalReads++;
+ if (failure != null) throw failure;
+ yield values[row[0]] == null ? null : new BigDecimal(values[row[0]]);
+ }
+ case "getObject" -> {
+ if (failure != null) throw failure;
+ String value = values[row[0]];
+ if (value == null) yield null;
+ if (type == Types.LONGVARCHAR) throw new AssertionError("LONGVARCHAR used getObject");
+ if (type == Types.TIMESTAMP) yield java.sql.Timestamp.valueOf("2026-10-02 00:00:00");
+ if (type == Types.VARBINARY) yield new byte[] { 1, 2 };
+ BigDecimal decimal = new BigDecimal(value);
+ if (type == Types.DECIMAL) yield decimal;
+ try { yield decimal.intValueExact(); }
+ catch (ArithmeticException error) { throw new SQLException("Numeric value out of range"); }
+ }
+ case "getString" -> values[row[0]];
+ case "getBoolean" -> true;
+ case "getBytes" -> new byte[] { 1, 2 };
+ default -> defaultValue(m.getReturnType());
+ });
+ }
+ }
+
+ private static JsonNode call(String method, ObjectNode params) throws Exception {
+ Method handler = DbxJdbcPlugin.class.getDeclaredMethod("handleLine", String.class);
+ handler.setAccessible(true);
+ ObjectNode request = MAPPER.createObjectNode().put("id", 1).put("method", method);
+ request.set("params", params);
+ return (JsonNode) handler.invoke(null, request.toString());
+ }
+
+ private static void set(String name, Object value) throws Exception {
+ Field field = DbxJdbcPlugin.class.getDeclaredField(name);
+ field.setAccessible(true);
+ field.set(null, value);
+ }
+
+ private static <T> T proxy(Class<T> type, InvocationHandler handler) {
+ return type.cast(Proxy.newProxyInstance(InterSystemsNumericTest.class.getClassLoader(), new Class<?>[] { type }, handler));
+ }
+
+ private static Object defaultValue(Class<?> type) {
+ if (type == boolean.class) return false;
+ if (type == int.class) return 0;
+ if (type == long.class) return 0L;
+ return null;
+ }
+}