fix(mysql): skip delimiter commands in editor
This commit is contained in:
1 parent
de39c1662d
commit
c289dda97d
3 files changed
+128
-10
No files matched your search
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user