fix(h2): display JSON columns as text

This commit is contained in:
t8y2 committed 2026-07-03 17:10:57 +08:00
1 parent 2f9a0869a9
commit 76f079f28b
3 files changed
+58 -3

No files matched your search

@@ -14,6 +14,7 @@ import com.dbx.agent.ObjectSource;
import com.dbx.agent.TableInfo;
import com.dbx.agent.TriggerInfo;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
@@ -320,6 +321,20 @@ public final class H2Agent extends AbstractJdbcAgent {
return unchecked(() -> JdbcExecutor.INSTANCE.defaultResultValue(rs, index, sqlType));
}
@Override
protected JdbcExecutor.ColumnAwareResultValueReader resultValueReader() {
return (rs, index, sqlType, columnTypeName) -> {
if (isJsonType(columnTypeName)) {
byte[] bytes = rs.getBytes(index);
// H2 stores JSON as UTF-8 bytes and getString/default JDBC
// handling exposes a hex literal such as 0x5b5d. Decode only
// JSON columns so real binary data keeps the existing hex view.
return rs.wasNull() || bytes == null ? null : new String(bytes, StandardCharsets.UTF_8);
}
return resultValue(rs, index, sqlType);
};
}
static String buildUrl(ConnectParams params) {
String connectionString = params.getConnection_string();
if (connectionString != null && !connectionString.trim().isEmpty()) {
@@ -343,6 +358,10 @@ public final class H2Agent extends AbstractJdbcAgent {
return value instanceof Number ? ((Number) value).intValue() : null;
}
private static boolean isJsonType(String columnTypeName) {
return columnTypeName != null && "JSON".equals(columnTypeName.trim().toUpperCase(Locale.ROOT));
}
public static void main(String[] args) {
new JsonRpcServer(new H2Agent()).run();
}
@@ -3,6 +3,8 @@ package com.dbx.agent.h2;
import com.dbx.agent.BaseDatabaseAgent;
import com.dbx.agent.ConnectParams;
import com.dbx.agent.DatabaseAgent;
import com.dbx.agent.ExecuteQueryOptions;
import com.dbx.agent.QueryResult;
import com.dbx.agent.test.JdbcExecutionBehaviorTest;
import com.dbx.agent.test.JdbcMetadataBehaviorTest;
import java.util.List;
@@ -64,6 +66,36 @@ class H2ExecutionBehaviorTest extends JdbcExecutionBehaviorTest {
protected String rowsSql(int rowCount) {
return "SELECT X FROM SYSTEM_RANGE(1, " + rowCount + ")";
}
@Test
void displaysJsonColumnsAsText() {
withAgent("dbx-agent-h2-json", agent -> {
agent.executeQuery(
"CREATE TABLE EVENTS (ID INT PRIMARY KEY, ACTION_PARAMS JSON, RAW_BYTES VARBINARY)",
null,
new ExecuteQueryOptions()
);
agent.executeQuery(
"INSERT INTO EVENTS VALUES (1, JSON '[]', X'5B5D'), (2, JSON '[{\"type\":\"ACCESS_CONTROL\"}]', X'00FF')",
null,
new ExecuteQueryOptions()
);
QueryResult result = agent.executeQuery(
"SELECT ACTION_PARAMS, RAW_BYTES FROM EVENTS ORDER BY ID",
null,
new ExecuteQueryOptions()
);
Assertions.assertEquals(
List.of(
List.of("[]", "0x5b5d"),
List.of("[{\"type\":\"ACCESS_CONTROL\"}]", "0x00ff")
),
result.getRows()
);
});
}
}
class H2MetadataBehaviorTest extends JdbcMetadataBehaviorTest {