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(sql): keep procedure bodies together on JDBC and Hive connections (#12578)

lxk9559a3e9805bd82
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts
index c8a2c0dc37..e0313d28f2 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts
@@ -582,6 +582,12 @@ describe("splitSqlStatementRanges", () => {
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "mysql"))).toEqual(["SELECT 1", "SELECT 2"]);
});
+ it("treats hash comments as line comments on generic JDBC and Hive connections", () => {
+ const sql = "SELECT 1 # comment;\n;\nSELECT 2";
+ expect(rangeSqlTexts(splitSqlStatementRanges(sql, "jdbc"))).toEqual(["SELECT 1", "SELECT 2"]);
+ expect(rangeSqlTexts(splitSqlStatementRanges(sql, "hive"))).toEqual(["SELECT 1", "SELECT 2"]);
+ });
+
it("keeps SQL Server temporary table names instead of treating them as hash comments", () => {
const sql = "DROP TABLE IF EXISTS #Temp;\nSELECT * FROM ##GlobalTemp;";
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "sqlserver"))).toEqual(["DROP TABLE IF EXISTS #Temp", "SELECT * FROM ##GlobalTemp"]);
@@ -723,6 +729,61 @@ SELECT f1 + f2 FROM dual;`;
expect(hasMultipleExecutionTargets(argoProcedureFixture, "argo")).toBe(false);
expect(rangeSqlTexts(executableStatementRanges(argoProcedureFixture, "argo"))).toEqual([argoProcedureFixture]);
expect(rangeSqlTexts(splitSqlStatementRanges(argoProcedureFixture, "transwarp"))).toEqual([argoProcedureFixture]);
+ expect(rangeSqlTexts(splitSqlStatementRanges(argoProcedureFixture, "jdbc"))).toEqual([argoProcedureFixture]);
+ expect(rangeSqlTexts(splitSqlStatementRanges(argoProcedureFixture, "hive"))).toEqual([argoProcedureFixture]);
+ });
+
+ it("keeps procedure bodies together on JDBC and Hive connections (#12321)", () => {
+ const procedure = `CREATE OR REPLACE PROCEDURE pro_cbs_check_his(is_date IN VARCHAR(8))
+IS
+BEGIN
+ DELETE FROM transwarp.tmp_cbs_t00_corp_cust_his
+ WHERE data_date = is_date;
+
+ INSERT INTO transwarp.tmp_cbs_t00_corp_cust_his
+ SELECT * FROM transwarp.cbs_t00_corp_cust_his
+ WHERE data_date = is_date;
+
+ COMMIT;
+END;`;
+
+ for (const dbType of ["jdbc", "hive", "argo", "transwarp"] as const) {
+ expect(rangeSqlTexts(splitSqlStatementRanges(procedure, dbType))).toEqual([procedure]);
+ expect(hasMultipleExecutionTargets(procedure, dbType)).toBe(false);
+ expect(rangeSqlTexts(executableStatementRanges(procedure, dbType))).toEqual([procedure]);
+ }
+
+ const procedureWithAs = `CREATE OR REPLACE PROCEDURE pro_cbs_check_his(is_date IN VARCHAR(8))
+AS
+BEGIN
+ DELETE FROM transwarp.tmp_cbs_t00_corp_cust_his
+ WHERE data_date = is_date;
+
+ COMMIT;
+END;`;
+
+ for (const dbType of ["jdbc", "hive", "argo", "transwarp"] as const) {
+ expect(rangeSqlTexts(splitSqlStatementRanges(procedureWithAs, dbType))).toEqual([procedureWithAs]);
+ expect(hasMultipleExecutionTargets(procedureWithAs, dbType)).toBe(false);
+ expect(rangeSqlTexts(executableStatementRanges(procedureWithAs, dbType))).toEqual([procedureWithAs]);
+ }
+ });
+
+ it("splits vanilla Hive string-bodied functions at semicolons on JDBC and Hive connections", () => {
+ const sqlBasic = `CREATE FUNCTION my_udf AS 'com.example.Mask';
+SELECT mask_fn(x) FROM t;`;
+
+ const sqlIfNotExists = `CREATE FUNCTION IF NOT EXISTS my_udf AS 'com.example.Mask';
+SELECT mask_fn(x) FROM t;`;
+
+ const sqlTemporary = `CREATE TEMPORARY FUNCTION my_udf AS 'com.example.Mask';
+SELECT mask_fn(x) FROM t;`;
+
+ for (const dbType of ["jdbc", "hive"] as const) {
+ expect(rangeSqlTexts(splitSqlStatementRanges(sqlBasic, dbType))).toEqual(["CREATE FUNCTION my_udf AS 'com.example.Mask'", "SELECT mask_fn(x) FROM t"]);
+ expect(rangeSqlTexts(splitSqlStatementRanges(sqlIfNotExists, dbType))).toEqual(["CREATE FUNCTION IF NOT EXISTS my_udf AS 'com.example.Mask'", "SELECT mask_fn(x) FROM t"]);
+ expect(rangeSqlTexts(splitSqlStatementRanges(sqlTemporary, dbType))).toEqual(["CREATE TEMPORARY FUNCTION my_udf AS 'com.example.Mask'", "SELECT mask_fn(x) FROM t"]);
+ }
});
it("statement at cursor inside an ArgoDB procedure body returns the whole definition", () => {
diff --git a/apps/desktop/src/lib/sql/sqlStatementRanges.ts b/apps/desktop/src/lib/sql/sqlStatementRanges.ts
index fe84155db1..d07b9ab66f 100644
--- a/apps/desktop/src/lib/sql/sqlStatementRanges.ts
+++ b/apps/desktop/src/lib/sql/sqlStatementRanges.ts
@@ -2047,14 +2047,19 @@ function isPostgresFamilyDatabase(databaseType?: DatabaseType): boolean {
return !!databaseType && POSTGRES_FAMILY_DATABASES.has(databaseType);
}
+function isOracleStyleRoutineDatabase(databaseType?: DatabaseType): boolean {
+ return isPostgresFamilyDatabase(databaseType) || databaseType === "jdbc" || databaseType === "hive";
+}
+
/**
* Whether a statement must stay a single statement instead of being cut at its inner semicolons:
* Oracle PL/SQL blocks on Oracle-like dialects, and the Oracle-style routine bodies that a
- * PostgreSQL-family connection reaches on a GaussDB/openGauss Oracle-compatibility server.
+ * PostgreSQL-family connection reaches on a GaussDB/openGauss Oracle-compatibility server,
+ * as well as generic JDBC and Hive drivers reaching ArgoDB/Inceptor procedure bodies (#12321).
*/
export function keepsOracleStyleBlockTogether(sql: string, databaseType?: DatabaseType, options?: SqlParameterOptions): boolean {
if (isOraclePlSqlStatement(sql, databaseType, options)) return true;
- return isPostgresFamilyDatabase(databaseType) && startsWithOracleStyleRoutineBody(sql);
+ return isOracleStyleRoutineDatabase(databaseType) && startsWithOracleStyleRoutineBody(sql);
}
export function isOraclePlSqlStatement(sql: string, databaseType?: DatabaseType, options?: SqlParameterOptions): boolean {
diff --git a/crates/dbx-sql-core/src/sql.rs b/crates/dbx-sql-core/src/sql.rs
index 5433b0f8ab..cfdf156a43 100644
--- a/crates/dbx-sql-core/src/sql.rs
+++ b/crates/dbx-sql-core/src/sql.rs
@@ -316,6 +316,10 @@ impl SqlDialectProfile {
};
}
+ if matches!(db_type, DatabaseType::Jdbc | DatabaseType::Hive) {
+ return Self { supports_oracle_style_routine_bodies: true, ..Self::default() };
+ }
+
Self::default()
}
@@ -5278,6 +5282,15 @@ SELECT 2;";
assert_eq!(firebird, SqlDialectProfile::firebird());
assert!(firebird.supports_firebird_routine_blocks);
assert!(!firebird.supports_backslash_escaped_quotes);
+
+ for db_type in [DatabaseType::Jdbc, DatabaseType::Hive] {
+ let profile = SqlDialectProfile::for_database_type(db_type);
+ assert!(profile.supports_oracle_style_routine_bodies);
+ assert!(!profile.supports_oracle_plsql_blocks);
+ assert!(!profile.supports_mysql_routine_blocks);
+ assert!(!profile.supports_slash_line_block_delimiter);
+ assert!(profile.supports_backslash_escaped_quotes);
+ }
}
#[test]
@@ -5730,6 +5743,84 @@ END;";
assert_eq!(split_sql_statements_for_database(sql, DatabaseType::Argo), vec![sql.to_string()]);
assert_eq!(split_sql_statements_for_database(sql, DatabaseType::Transwarp), vec![sql.to_string()]);
+ assert_eq!(split_sql_statements_for_database(sql, DatabaseType::Jdbc), vec![sql.to_string()]);
+ assert_eq!(split_sql_statements_for_database(sql, DatabaseType::Hive), vec![sql.to_string()]);
+ }
+
+ #[test]
+ fn jdbc_and_hive_keep_procedure_statement_ranges_intact() {
+ let sql = "\
+CREATE OR REPLACE PROCEDURE pro_cbs_check_his(is_date IN VARCHAR(8))
+IS
+BEGIN
+ DELETE FROM transwarp.tmp_cbs_t00_corp_cust_his
+ WHERE data_date = is_date;
+
+ INSERT INTO transwarp.tmp_cbs_t00_corp_cust_his
+ SELECT * FROM transwarp.cbs_t00_corp_cust_his
+ WHERE data_date = is_date;
+
+ COMMIT;
+END;";
+
+ let sql_as = "\
+CREATE OR REPLACE PROCEDURE pro_cbs_check_his(is_date IN VARCHAR(8))
+AS
+BEGIN
+ DELETE FROM transwarp.tmp_cbs_t00_corp_cust_his
+ WHERE data_date = is_date;
+
+ COMMIT;
+END;";
+
+ for db_type in [DatabaseType::Jdbc, DatabaseType::Hive, DatabaseType::Argo, DatabaseType::Transwarp] {
+ let statements = split_sql_statements_for_database(sql, db_type);
+ assert_eq!(statements, vec![sql.to_string()], "failed for {db_type:?}");
+ let statements_as = split_sql_statements_for_database(sql_as, db_type);
+ assert_eq!(statements_as, vec![sql_as.to_string()], "failed for {db_type:?}");
+ }
+ }
+
+ #[test]
+ fn jdbc_and_hive_split_hiveql_functions_with_string_body() {
+ let sql_basic = "\
+CREATE FUNCTION my_udf AS 'com.example.Mask';
+SELECT mask_fn(x) FROM t;";
+
+ let sql_if_not_exists = "\
+CREATE FUNCTION IF NOT EXISTS my_udf AS 'com.example.Mask';
+SELECT mask_fn(x) FROM t;";
+
+ let sql_temporary = "\
+CREATE TEMPORARY FUNCTION my_udf AS 'com.example.Mask';
+SELECT mask_fn(x) FROM t;";
+
+ for db_type in [DatabaseType::Jdbc, DatabaseType::Hive] {
+ assert_eq!(
+ split_sql_statements_for_database(sql_basic, db_type),
+ vec![
+ "CREATE FUNCTION my_udf AS 'com.example.Mask'".to_string(),
+ "SELECT mask_fn(x) FROM t".to_string(),
+ ],
+ "failed for {db_type:?}"
+ );
+ assert_eq!(
+ split_sql_statements_for_database(sql_if_not_exists, db_type),
+ vec![
+ "CREATE FUNCTION IF NOT EXISTS my_udf AS 'com.example.Mask'".to_string(),
+ "SELECT mask_fn(x) FROM t".to_string(),
+ ],
+ "failed for {db_type:?}"
+ );
+ assert_eq!(
+ split_sql_statements_for_database(sql_temporary, db_type),
+ vec![
+ "CREATE TEMPORARY FUNCTION my_udf AS 'com.example.Mask'".to_string(),
+ "SELECT mask_fn(x) FROM t".to_string(),
+ ],
+ "failed for {db_type:?}"
+ );
+ }
}
#[test]