fix(kingbase): prioritize selected schema search path
This commit is contained in:
1 parent
2b5aaa2a54
commit
ece47c85d6
9 files changed
+146
-72
No files matched your search
@@ -79,26 +79,7 @@ public final class BatchExecutor {
|
||||
}
|
||||
|
||||
private static void applySchema(Connection conn, String schema, Function<String, String> setSchemaSql) throws Exception {
|
||||
if (schema == null || schema.trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
conn.setSchema(schema);
|
||||
return;
|
||||
} catch (Exception | AbstractMethodError ignored) {
|
||||
}
|
||||
try {
|
||||
conn.setCatalog(schema);
|
||||
return;
|
||||
} catch (Exception | AbstractMethodError ignored) {
|
||||
}
|
||||
String schemaSql = setSchemaSql.apply(schema);
|
||||
if (schemaSql == null || schemaSql.trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.execute(schemaSql);
|
||||
}
|
||||
JdbcSchemaSwitcher.apply(conn, schema, setSchemaSql);
|
||||
}
|
||||
|
||||
private static <T> T unchecked(ThrowingSupplier<T> supplier) {
|
||||
|
||||
@@ -565,31 +565,12 @@ public final class JdbcExecutor {
|
||||
}
|
||||
|
||||
private void applySchema(Connection conn, String schema, Function<String, String> setSchemaSql) throws SQLException {
|
||||
if (schema == null || schema.trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// Prefer JDBC standard APIs over database-specific SQL.
|
||||
// setSchema (JDBC 4.1) and setCatalog (JDBC 1.0) work universally
|
||||
// across all JDBC drivers without needing to know the database dialect.
|
||||
try {
|
||||
conn.setSchema(schema);
|
||||
return;
|
||||
} catch (SQLException | AbstractMethodError ignored) {
|
||||
// setSchema not supported by this driver
|
||||
}
|
||||
try {
|
||||
conn.setCatalog(schema);
|
||||
return;
|
||||
} catch (SQLException | AbstractMethodError ignored) {
|
||||
// setCatalog not supported either
|
||||
}
|
||||
// Fallback: execute database-specific SQL (e.g. USE, SET SCHEMA, etc.)
|
||||
String schemaSql = setSchemaSql.apply(schema);
|
||||
if (schemaSql == null || schemaSql.trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.execute(schemaSql);
|
||||
JdbcSchemaSwitcher.apply(conn, schema, setSchemaSql);
|
||||
} catch (SQLException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.dbx.agent;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.function.Function;
|
||||
|
||||
final class JdbcSchemaSwitcher {
|
||||
private JdbcSchemaSwitcher() {
|
||||
}
|
||||
|
||||
static void apply(Connection conn, String schema, Function<String, String> setSchemaSql) throws Exception {
|
||||
if (schema == null || schema.trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SchemaSqlResult schemaSqlResult = applySchemaSql(conn, schema, setSchemaSql);
|
||||
Exception schemaSqlError = schemaSqlResult.error;
|
||||
if (schemaSqlError == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
conn.setSchema(schema);
|
||||
return;
|
||||
} catch (SQLException | AbstractMethodError ignored) {
|
||||
// Some JDBC drivers only expose schema switching through SQL.
|
||||
}
|
||||
try {
|
||||
conn.setCatalog(schema);
|
||||
return;
|
||||
} catch (SQLException | AbstractMethodError ignored) {
|
||||
// Last fallback failed as well; surface the SQL-switch error below.
|
||||
}
|
||||
|
||||
if (schemaSqlResult.attempted) {
|
||||
throw schemaSqlError;
|
||||
}
|
||||
}
|
||||
|
||||
private static SchemaSqlResult applySchemaSql(Connection conn, String schema, Function<String, String> setSchemaSql) {
|
||||
String schemaSql;
|
||||
try {
|
||||
schemaSql = setSchemaSql.apply(schema);
|
||||
} catch (RuntimeException e) {
|
||||
return new SchemaSqlResult(true, e);
|
||||
}
|
||||
if (schemaSql == null || schemaSql.trim().isEmpty()) {
|
||||
return new SchemaSqlResult(false, new SQLException("No schema switch SQL provided"));
|
||||
}
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.execute(schemaSql);
|
||||
return new SchemaSqlResult(true, null);
|
||||
} catch (SQLException | AbstractMethodError e) {
|
||||
return new SchemaSqlResult(true, e instanceof SQLException ? (SQLException) e : new SQLException(e));
|
||||
}
|
||||
}
|
||||
|
||||
private static final class SchemaSqlResult {
|
||||
private final boolean attempted;
|
||||
private final Exception error;
|
||||
|
||||
private SchemaSqlResult(boolean attempted, Exception error) {
|
||||
this.attempted = attempted;
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,30 +71,7 @@ public final class TransactionExecutor {
|
||||
}
|
||||
|
||||
private static void applySchema(Connection conn, String schema, Function<String, String> setSchemaSql) throws Exception {
|
||||
if (schema == null || schema.trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// Prefer JDBC standard APIs over database-specific SQL.
|
||||
try {
|
||||
conn.setSchema(schema);
|
||||
return;
|
||||
} catch (Exception | AbstractMethodError ignored) {
|
||||
// setSchema not supported by this driver
|
||||
}
|
||||
try {
|
||||
conn.setCatalog(schema);
|
||||
return;
|
||||
} catch (Exception | AbstractMethodError ignored) {
|
||||
// setCatalog not supported either
|
||||
}
|
||||
// Fallback: execute database-specific SQL (e.g. USE, SET SCHEMA, etc.)
|
||||
String schemaSql = setSchemaSql.apply(schema);
|
||||
if (schemaSql == null || schemaSql.trim().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.execute(schemaSql);
|
||||
}
|
||||
JdbcSchemaSwitcher.apply(conn, schema, setSchemaSql);
|
||||
}
|
||||
|
||||
private static boolean supportsTransactions(Connection conn) {
|
||||
|
||||
Reference in new issue
Block a user