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(mysql): skip delimiter commands in editor

t8y2c289dda97df1
diff --git a/apps/desktop/src/lib/__tests__/sqlStatementRanges.spec.ts b/apps/desktop/src/lib/__tests__/sqlStatementRanges.spec.ts
index 969b21e79f..768f1319c4 100644
--- a/apps/desktop/src/lib/__tests__/sqlStatementRanges.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sqlStatementRanges.spec.ts
@@ -84,6 +84,11 @@ describe("splitSqlStatementRanges", () => {
const sql = "SELECT $$ a; b $$;\nSELECT 2";
expect(rangeSqlTexts(splitSqlStatementRanges(sql))).toEqual(["SELECT $$ a; b $$", "SELECT 2"]);
});
+
+ it("skips MySQL delimiter commands and empty custom delimiter statements", () => {
+ const sql = "select COUNT(1) FROM your_table;\ndelimiter ;;\nselect COUNT(1) FROM your_table;\n\n;;\ndelimiter ;";
+ expect(rangeSqlTexts(splitSqlStatementRanges(sql, "mysql"))).toEqual(["select COUNT(1) FROM your_table", "select COUNT(1) FROM your_table;"]);
+ });
});
describe("statementRangeAtCursor", () => {
@@ -219,6 +224,12 @@ describe("statementRangeAtCursor", () => {
expect(range?.from).toBe(2);
expect(range?.sql).toBe("SELECT 1");
});
+
+ it("skips MySQL delimiter commands when resolving the cursor statement", () => {
+ const sql = "select COUNT(1) FROM your_table;\ndelimiter ;;\nselect COUNT(1) FROM your_table;\n\n;;\ndelimiter ;";
+ expect(statementRangeAtCursor(sql, indexOf(sql, "COUNT", 2), "mysql")?.sql.trim()).toBe("select COUNT(1) FROM your_table;");
+ expect(statementRangeAtCursor(sql, indexOf(sql, "delimiter"), "mysql")).toBeNull();
+ });
});
describe("fullSqlRange", () => {
@@ -297,6 +308,12 @@ describe("buildExecutionCandidates", () => {
const candidates = buildExecutionCandidates(sql, sql.length);
expect(candidateKinds(candidates)).toEqual(["all"]);
});
+
+ it("uses the MySQL statement body for delimiter scripts", () => {
+ const sql = "select COUNT(1) FROM your_table;\ndelimiter ;;\nselect COUNT(1) FROM your_table;\n\n;;\ndelimiter ;";
+ const candidates = buildExecutionCandidates(sql, indexOf(sql, "COUNT", 2), "mysql");
+ expect(candidateSummaries(candidates)).toEqual(["cursor:select COUNT(1) FROM your_table;", "all:select COUNT(1) FROM your_table;\ndelimiter ;;\nselect COUNT(1) FROM your_table;\n\n;;\ndelimiter ;"]);
+ });
});
describe("hasMultipleExecutionTargets", () => {
@@ -316,6 +333,11 @@ describe("hasMultipleExecutionTargets", () => {
expect(hasMultipleExecutionTargets("GET user:1", "redis")).toBe(false);
expect(hasMultipleExecutionTargets("GET user:1\n# comment\nDEL user:2", "redis")).toBe(true);
});
+
+ it("counts MySQL delimiter scripts by executable statements", () => {
+ const sql = "select COUNT(1) FROM your_table;\ndelimiter ;;\nselect COUNT(1) FROM your_table;\n\n;;\ndelimiter ;";
+ expect(hasMultipleExecutionTargets(sql, "mysql")).toBe(true);
+ });
});
describe("supportsExecutionTargetPicker", () => {
diff --git a/apps/desktop/src/lib/sqlStatementRanges.ts b/apps/desktop/src/lib/sqlStatementRanges.ts
index 32e2ac40e7..7c7a43472f 100644
--- a/apps/desktop/src/lib/sqlStatementRanges.ts
+++ b/apps/desktop/src/lib/sqlStatementRanges.ts
@@ -21,7 +21,7 @@ export function hasMultipleExecutionTargets(sql: string, databaseType?: Database
if (databaseType === "redis") {
return redisExecutableCommandCount(sql) > 1;
}
- return splitSqlStatementRanges(sql).length > 1;
+ return splitSqlStatementRanges(sql, databaseType).length > 1;
}
interface RawStatement {
@@ -110,13 +110,15 @@ const INSERT_BODY_KEYWORDS = new Set(["SELECT", "WITH"]);
* only the statement text (the trailing semicolon and inter-statement
* whitespace are excluded so editor highlights stay tight).
*/
-export function splitSqlStatementRanges(sql: string): RawStatement[] {
+export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType): RawStatement[] {
const statements: RawStatement[] = [];
const len = sql.length;
+ const supportsDelimiterCommands = databaseType === "mysql";
let statementStart = -1;
let statementEnd = -1;
let statementHitStart = 0;
+ let customDelimiter: string | null = null;
let state: QuoteState = "none";
let dollarTag = "";
let i = 0;
@@ -128,9 +130,14 @@ export function splitSqlStatementRanges(sql: string): RawStatement[] {
statementEnd = pos + 1;
};
- const flush = () => {
- if (statementStart !== -1 && statementEnd !== -1 && statementEnd > statementStart) {
- statements.push({ hitFrom: statementHitStart, from: statementStart, to: statementEnd, sql: sql.slice(statementStart, statementEnd) });
+ const flush = (to = statementEnd) => {
+ if (statementStart === -1) {
+ statementEnd = -1;
+ return;
+ }
+ const trimmedTo = trimRangeEnd(sql, statementStart, to);
+ if (trimmedTo > statementStart) {
+ statements.push({ hitFrom: statementHitStart, from: statementStart, to: trimmedTo, sql: sql.slice(statementStart, trimmedTo) });
}
statementStart = -1;
statementEnd = -1;
@@ -215,6 +222,18 @@ export function splitSqlStatementRanges(sql: string): RawStatement[] {
}
// state === "none"
+ if (supportsDelimiterCommands && isAtLineStart(sql, i) && startsDelimiterCommand(sql, i)) {
+ const lineEnd = findLineEnd(sql, i);
+ const delimiter = parseDelimiterCommand(sql.slice(i, lineEnd));
+ if (delimiter !== null) {
+ flush();
+ customDelimiter = delimiter === ";" ? null : delimiter;
+ i = nextLineStart(sql, lineEnd);
+ statementHitStart = i;
+ continue;
+ }
+ }
+
// Line comments consume up to (and including) the newline.
if (ch === "-" && next === "-") {
const newline = sql.indexOf("\n", i);
@@ -269,7 +288,14 @@ export function splitSqlStatementRanges(sql: string): RawStatement[] {
}
}
- if (ch === ";") {
+ if (customDelimiter) {
+ if (sql.startsWith(customDelimiter, i)) {
+ flush(i);
+ i += customDelimiter.length;
+ statementHitStart = i;
+ continue;
+ }
+ } else if (ch === ";") {
flush();
statementHitStart = i + 1;
i += 1;
@@ -299,7 +325,7 @@ export function statementRangeAtCursor(sql: string, cursorPos: number, databaseT
const pos = clampCursor(sql, cursorPos);
if (isCursorOnBlankLine(sql, pos)) return null;
- const statements = splitSqlStatementRanges(sql);
+ const statements = splitSqlStatementRanges(sql, databaseType);
for (let index = 0; index < statements.length; index += 1) {
const statement = statements[index];
const softRanges = splitStatementRangeAtSoftStarts(sql, statement, databaseType);
@@ -767,6 +793,40 @@ function isSqlWhitespace(ch: string): boolean {
return ch === " " || ch === "\t" || ch === "\r" || ch === "\n";
}
+function isAtLineStart(sql: string, pos: number): boolean {
+ for (let i = pos - 1; i >= 0; i -= 1) {
+ const ch = sql[i];
+ if (ch === "\n" || ch === "\r") return true;
+ if (ch !== " " && ch !== "\t") return false;
+ }
+ return true;
+}
+
+function startsDelimiterCommand(sql: string, pos: number): boolean {
+ const prefix = sql.slice(pos, pos + 9);
+ return prefix.toLowerCase() === "delimiter" && (sql[pos + 9] === " " || sql[pos + 9] === "\t");
+}
+
+function parseDelimiterCommand(line: string): string | null {
+ const match = /^delimiter[ \t]+(.+)$/i.exec(line.trim());
+ const delimiter = match?.[1]?.trim();
+ return delimiter ? delimiter : null;
+}
+
+function findLineEnd(sql: string, pos: number): number {
+ const newline = sql.indexOf("\n", pos);
+ const carriageReturn = sql.indexOf("\r", pos);
+ if (newline === -1) return carriageReturn === -1 ? sql.length : carriageReturn;
+ if (carriageReturn === -1) return newline;
+ return Math.min(newline, carriageReturn);
+}
+
+function nextLineStart(sql: string, lineEnd: number): number {
+ if (sql[lineEnd] === "\r" && sql[lineEnd + 1] === "\n") return lineEnd + 2;
+ if (sql[lineEnd] === "\n" || sql[lineEnd] === "\r") return lineEnd + 1;
+ return lineEnd;
+}
+
function rangeFor(statement: RawStatement, sql: string): SqlTextRange {
return {
from: statement.from,
diff --git a/crates/dbx-core/src/sql.rs b/crates/dbx-core/src/sql.rs
index b2d3966f5c..a03ee324a2 100644
--- a/crates/dbx-core/src/sql.rs
+++ b/crates/dbx-core/src/sql.rs
@@ -2029,9 +2029,10 @@ mod tests {
use super::{
contains_or_fuzzy_match, decode_sql_file_bytes, find_statement_at_cursor_for_database, fuzzy_filter_enabled,
fuzzy_like_pattern_with_escape, fuzzy_subsequence_match, optimize_sql_file_import_statements,
- prepare_sql_file_statement, split_sql_script, split_sql_statements_for_database,
- starts_with_executable_sql_keyword, starts_with_executable_sql_keyword_for_database, SqlDialectProfile,
- SqlFileStatementAction, SqlStatementSplitter,
+ prepare_sql_file_statement, split_sql_script, split_sql_statement_ranges_with_options,
+ split_sql_statements_for_database, starts_with_executable_sql_keyword,
+ starts_with_executable_sql_keyword_for_database, SqlDialectProfile, SqlFileStatementAction, SqlParsingOptions,
+ SqlStatementSplitter,
};
#[test]
@@ -2928,6 +2929,41 @@ delimiter ;";
);
}
+ #[test]
+ fn mysql_delimiter_command_skips_empty_custom_delimiter_statement_per_issue_1988() {
+ let sql = "\
+select COUNT(1) FROM your_table;
+delimiter ;;
+select COUNT(1) FROM your_table;
+
+;;
+delimiter ;";
+
+ assert_eq!(
+ split_sql_statements_for_database(sql, DatabaseType::Mysql),
+ vec!["select COUNT(1) FROM your_table", "select COUNT(1) FROM your_table;"]
+ );
+ }
+
+ #[test]
+ fn mysql_delimiter_command_ranges_skip_empty_custom_delimiter_statement_per_issue_1988() {
+ let sql = "\
+select COUNT(1) FROM your_table;
+delimiter ;;
+select COUNT(1) FROM your_table;
+
+;;
+delimiter ;";
+
+ let ranges =
+ split_sql_statement_ranges_with_options(sql, SqlParsingOptions::for_database_type(DatabaseType::Mysql));
+
+ assert_eq!(
+ ranges.iter().map(|range| range.text.as_str()).collect::<Vec<_>>(),
+ vec!["select COUNT(1) FROM your_table", "select COUNT(1) FROM your_table;"]
+ );
+ }
+
#[test]
fn mysql_current_statement_keeps_inline_hash_comment_per_issue_428() {
let sql = "SELECT 1; # mysql comment\n\nSELECT 2 # trailing comment";