fix(jdbc): apply query execution context

This commit is contained in:
t8y2
2026-05-16 10:49:54 +08:00
parent 2024171c8d
commit 3be6f4486a
6 changed files with 165 additions and 10 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{
"id": "jdbc",
"name": "DBX JDBC Plugin",
"version": "0.1.0",
"version": "0.1.1",
"protocol_version": 1,
"description": "Adds optional JDBC driver support to DBX.",
"executable": "bin/dbx-jdbc-plugin",
+17
View File
@@ -18,10 +18,27 @@
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
@@ -100,7 +100,12 @@ public final class DbxJdbcPlugin {
result.put("ok", true);
yield result;
}
case "executeQuery" -> executeQuery(connection, requireText(params, "sql"));
case "executeQuery" -> executeQuery(
connection,
requireText(params, "sql"),
optionalText(params, "database"),
optionalText(params, "schema")
);
case "listDatabases" -> listDatabases(connection);
case "listSchemas" -> listSchemas(connection, optionalText(params, "database"));
case "listTables" -> listTables(connection, optionalText(params, "database"), optionalText(params, "schema"));
@@ -180,9 +185,10 @@ public final class DbxJdbcPlugin {
return sharedConnection;
}
private static JsonNode executeQuery(JsonNode connection, String sql) throws SQLException {
private static JsonNode executeQuery(JsonNode connection, String sql, String database, String schema) throws SQLException {
long start = System.nanoTime();
Connection conn = openConnection(connection);
applyExecutionContext(conn, database, schema);
try (Statement statement = conn.createStatement()) {
statement.setMaxRows(MAX_ROWS + 1);
boolean hasResultSet = statement.execute(sql);
@@ -222,6 +228,21 @@ public final class DbxJdbcPlugin {
}
}
private static void applyExecutionContext(Connection conn, String database, String schema) throws SQLException {
if (database != null) {
try {
conn.setCatalog(database);
} catch (SQLFeatureNotSupportedException | AbstractMethodError ignored) {
}
}
if (schema != null) {
try {
conn.setSchema(schema);
} catch (SQLFeatureNotSupportedException | AbstractMethodError ignored) {
}
}
}
private static JsonNode listDatabases(JsonNode connection) throws SQLException {
ArrayNode result = MAPPER.createArrayNode();
Connection conn = openConnection(connection);
@@ -0,0 +1,58 @@
package app.dbx.jdbc;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
final class DbxJdbcPluginTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String CONNECTION = """
{
"connection_string": "jdbc:h2:mem:dbx_ctx;DB_CLOSE_DELAY=-1",
"username": "sa"
}
""";
@AfterEach
void closeConnection() throws Exception {
request("close", """
{ "connection": %s }
""".formatted(CONNECTION));
}
@Test
void executeQueryAppliesSchemaContext() throws Exception {
request("executeQuery", """
{
"connection": %s,
"sql": "CREATE SCHEMA IF NOT EXISTS app"
}
""".formatted(CONNECTION));
JsonNode response = request("executeQuery", """
{
"connection": %s,
"schema": "APP",
"sql": "SELECT SCHEMA() AS schema_name"
}
""".formatted(CONNECTION));
assertFalse(response.has("error"), response.toString());
assertEquals("APP", response.path("result").path("rows").path(0).path(0).asText());
}
private static JsonNode request(String method, String params) throws Exception {
Method handleLine = DbxJdbcPlugin.class.getDeclaredMethod("handleLine", String.class);
handleLine.setAccessible(true);
String line = """
{ "id": 1, "method": "%s", "params": %s }
""".formatted(method, params);
return MAPPER.valueToTree(handleLine.invoke(null, line));
}
}