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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(jdbc): keep Oracle PL/SQL units whole on generic JDBC connect
Jeff0feb065e4807
diff --git a/crates/dbx-core/src/query/mod.rs b/crates/dbx-core/src/query/mod.rsindex ed34eb1430..784e15821e 100644--- a/crates/dbx-core/src/query/mod.rs+++ b/crates/dbx-core/src/query/mod.rs@@ -861,6 +861,22 @@ async fn connection_database_type(state: &AppState, connection_id: &str) -> Opticonfigs.get(connection_id).map(|config| config.db_type)}+async fn connection_statement_split_database_type(state: &AppState, connection_id: &str) -> Option<DatabaseType> {+ let configs = state.configs.read().await;+ configs.get(connection_id).map(statement_split_database_type)+}++/// A generic JDBC connection to Oracle runs PL/SQL through the JDBC plugin, so its+/// script is split with the Oracle rules. The plain `Jdbc` profile would cut every+/// routine body at its first inner semicolon and replace the object with the fragment.+fn statement_split_database_type(config: &ConnectionConfig) -> DatabaseType {+ if crate::schema::is_oracle_external_driver_config(config) {+ DatabaseType::Oracle+ } else {+ config.db_type+ }+}+async fn connection_sql_compatibility_mode(state: &AppState,pool_key: &str,@@ -3559,8 +3575,9 @@ async fn execute_multi_core_with_options_for_client_and_progress_typed_inner(let compatibility_mode = connection_sql_compatibility_mode(state, &pool_key, db_type).await;let complete_sqlserver_agent_batch =is_sqlserver_agent && sqlserver_agent_batch::requires_complete_results(sql, &options);+ let split_db_type = connection_statement_split_database_type(state, connection_id).await;let execution_plan =- query_execution_plan_with_compatibility(sql, db_type, is_sqlserver_agent, compatibility_mode.as_deref());+ query_execution_plan_with_compatibility(sql, split_db_type, is_sqlserver_agent, compatibility_mode.as_deref());let continue_on_error = options.continue_on_error && !execution_plan.stop_on_error;let statements = execution_plan.statements;if statements.is_empty() {@@ -10375,6 +10392,22 @@ for line in sys.stdin:assert_eq!(plan.statements, vec!["DECLARE @x TABLE (id INT);\nINSERT INTO @x VALUES (1);", "SELECT 2;"]);}+ #[test]+ fn jdbc_oracle_execution_plan_keeps_plsql_units_whole() {+ let mut oracle = test_connection_config(DatabaseType::Jdbc);+ oracle.connection_string = Some("jdbc:oracle:thin:@//127.0.0.1:1521/FREEPDB1".to_string());+ let function = "CREATE OR REPLACE EDITIONABLE FUNCTION \"APP\".\"F\" (p VARCHAR2) RETURN VARCHAR2 IS\n\+ l_ret VARCHAR2(200);\nBEGIN\n SELECT p INTO l_ret FROM dual;\n RETURN l_ret;\nEND;";++ let plan = query_execution_plan(function, Some(statement_split_database_type(&oracle)), false);++ assert_eq!(plan.statements, vec![function]);++ let mut other = test_connection_config(DatabaseType::Jdbc);+ other.connection_string = Some("jdbc:h2:mem:test".to_string());+ assert_eq!(statement_split_database_type(&other), DatabaseType::Jdbc);+ }+#[test]fn ordinary_agent_execution_plan_still_splits_semicolon_statements() {let plan = query_execution_plan("SELECT 1; SELECT 2;", Some(DatabaseType::Dameng), false);diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rsindex d58baa1ef7..9b214b5029 100644--- a/crates/dbx-core/src/schema/mod.rs+++ b/crates/dbx-core/src/schema/mod.rs@@ -10111,7 +10111,7 @@ fn mysql_external_driver_url(config: &ConnectionConfig) -> Option<bool> {.map(|value| value.to_ascii_lowercase().starts_with("jdbc:mysql:"))}-fn is_oracle_external_driver_config(config: &ConnectionConfig) -> bool {+pub(crate) fn is_oracle_external_driver_config(config: &ConnectionConfig) -> bool {if config.db_type != DatabaseType::Jdbc {return false;}diff --git a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.javaindex 69e0679848..9dfaeeff74 100644--- a/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java+++ b/plugins/jdbc/src/main/java/app/dbx/jdbc/DbxJdbcPlugin.java@@ -57,6 +57,7 @@ import java.util.Set;import java.util.TreeMap;import java.util.UUID;import java.util.logging.Logger;+import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.stream.Collectors;@@ -76,6 +77,18 @@ public final class DbxJdbcPlugin {"^SELECT\\s+\\*\\s+FROM\\s+(?:SYSTEM|\\\"SYSTEM\\\")\\s*\\.\\s*(?:CATALOG|\\\"CATALOG\\\")$",Pattern.CASE_INSENSITIVE);+ // Mirrors `trimStatementSQL` in the native oracle-go agent: a PL/SQL unit must keep the+ // `;` after its final END, while a plain Oracle SQL statement must not end with one.+ private static final Pattern ORACLE_PLSQL_BLOCK_START = Pattern.compile(+ "^\\s*(?:DECLARE|BEGIN|CREATE\\s+(?:OR\\s+REPLACE\\s+)?(?:(?:EDITIONABLE|NONEDITIONABLE)\\s+)?"+ + "(?:FUNCTION|PROCEDURE|TRIGGER|PACKAGE(?:\\s+BODY)?|TYPE(?:\\s+BODY)?))\\b",+ Pattern.CASE_INSENSITIVE | Pattern.DOTALL+ );+ private static final Pattern ORACLE_PLSQL_BLOCK_END = Pattern.compile("\\bEND\\s*;\\s*$", Pattern.CASE_INSENSITIVE);+ private static final Pattern ORACLE_NAMED_PLSQL_BLOCK_END = Pattern.compile(+ "\\bEND\\s+([A-Z0-9_$#]+)\\s*;\\s*$",+ Pattern.CASE_INSENSITIVE+ );private static final String[] DEFAULT_TABLE_TYPES = new String[] {"TABLE","VIEW",@@ -1413,7 +1426,7 @@ public final class DbxJdbcPlugin {trackActiveStatement(statement);if (connectionState().closing) throw new SQLException("JDBC logical session is closed");applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks);- String trimmedSql = trimStatementSql(sql);+ String trimmedSql = trimStatementSql(sql, isOracleUrl(jdbcUrl(connection)));String effectiveSql = rewritePhoenixSystemCatalogQuery(connection, conn, trimmedSql);ExecutedStatement executed = executeStatementForResultWithAdhocRetry(connection, statement, effectiveSql, quirks);ObjectNode result = MAPPER.createObjectNode();@@ -1600,7 +1613,7 @@ public final class DbxJdbcPlugin {try {if (connectionState().closing) throw new SQLException("JDBC logical session is closed");applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks);- String trimmedSql = trimStatementSql(sql);+ String trimmedSql = trimStatementSql(sql, isOracleUrl(jdbcUrl(connection)));String effectiveSql = rewritePhoenixSystemCatalogQuery(connection, conn, trimmedSql);ExecutedStatement executed = executeStatementForResultWithAdhocRetry(connection, statement, effectiveSql, quirks);ResultSet rs = executed.resultSet();@@ -2285,6 +2298,44 @@ public final class DbxJdbcPlugin {return sql == null ? "" : sql.trim().replaceFirst(";\\s*$", "");}+ static String trimStatementSql(String sql, boolean oracle) {+ if (!oracle) {+ return trimStatementSql(sql);+ }+ String trimmed = stripTrailingOracleSlashDelimiter(sql == null ? "" : sql.trim());+ return isOraclePlSqlBlock(trimmed) ? trimmed : trimStatementSql(trimmed);+ }++ private static String stripTrailingOracleSlashDelimiter(String sql) {+ if (!sql.endsWith("/")) {+ return sql;+ }+ int slash = sql.length() - 1;+ int lineStart = sql.lastIndexOf('\n', slash - 1) + 1;+ if (!sql.substring(lineStart, slash).isBlank()) {+ return sql;+ }+ String beforeSlash = sql.substring(0, lineStart).trim();+ // A standalone `/` line is the SQL*Plus command that runs a PL/SQL unit; the+ // driver needs only the unit itself.+ return isOraclePlSqlBlock(beforeSlash) ? beforeSlash : sql;+ }++ private static boolean isOraclePlSqlBlock(String sql) {+ if (!ORACLE_PLSQL_BLOCK_START.matcher(stripLeadingSqlComments(sql)).find()) {+ return false;+ }+ if (ORACLE_PLSQL_BLOCK_END.matcher(sql).find()) {+ return true;+ }+ Matcher named = ORACLE_NAMED_PLSQL_BLOCK_END.matcher(sql);+ if (!named.find()) {+ return false;+ }+ String label = named.group(1).toUpperCase(Locale.ROOT);+ return !label.equals("IF") && !label.equals("LOOP") && !label.equals("CASE");+ }+private static boolean isSafeAutotraceSql(String sql) {String stripped = stripCommentsAndLiterals(trimStatementSql(sql));if (stripped.isBlank()) {diff --git a/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java b/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.javaindex 2bee556ccc..87072dcca9 100644--- a/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java+++ b/plugins/jdbc/src/test/java/app/dbx/jdbc/DbxJdbcPluginTest.java@@ -6472,4 +6472,27 @@ final class DbxJdbcPluginTest {response.path("error").path("message").asText());}++ @Test+ void oracleStatementTrimKeepsThePlSqlUnitTerminator() {+ List<String> units = List.of(+ "DECLARE\n l NUMBER;\nBEGIN\n l := 1;\nEND;",+ "CREATE OR REPLACE EDITIONABLE FUNCTION \"APP\".\"F\" (p VARCHAR2) RETURN VARCHAR2 IS\n"+ + " l VARCHAR2(200);\nBEGIN\n RETURN l;\nEND;",+ "CREATE OR REPLACE PROCEDURE p AS\nBEGIN\n NULL;\nEND;",+ "CREATE OR REPLACE PACKAGE BODY pkg AS\n FUNCTION f RETURN NUMBER IS\n BEGIN\n RETURN 1;\n END;\nEND pkg;",+ "-- leading comment\nBEGIN\n NULL;\nEND;"+ );+ for (String unit : units) {+ assertEquals(unit, DbxJdbcPlugin.trimStatementSql(unit, true));+ assertEquals(unit, DbxJdbcPlugin.trimStatementSql(unit + "\n/", true));+ }+ }++ @Test+ void oracleStatementTrimStillDropsThePlainSqlSemicolon() {+ assertEquals("SELECT 1 FROM DUAL", DbxJdbcPlugin.trimStatementSql("SELECT 1 FROM DUAL;", true));+ assertEquals("BEGIN\n NULL;\nEND IF", DbxJdbcPlugin.trimStatementSql("BEGIN\n NULL;\nEND IF;", true));+ assertEquals("BEGIN\n NULL;\nEND", DbxJdbcPlugin.trimStatementSql("BEGIN\n NULL;\nEND;", false));+ }}