fix(mcp): preserve SQL literals during execution
This commit is contained in:
@@ -81,5 +81,106 @@ export function sqlSafetyFromEnv(env: NodeJS.ProcessEnv = process.env): SqlSafet
|
||||
}
|
||||
|
||||
export function splitSqlStatements(sql: string): string[] {
|
||||
return splitSqlStatementsForSafety(sql);
|
||||
const statements: string[] = [];
|
||||
let statementStart = 0;
|
||||
let index = 0;
|
||||
let state: "none" | "single" | "double" | "backtick" | "bracket" | "lineComment" | "blockComment" | "dollar" = "none";
|
||||
let dollarTag = "";
|
||||
|
||||
const pushStatement = (end: number) => {
|
||||
const statement = sql.slice(statementStart, end).trim();
|
||||
if (statement) statements.push(statement);
|
||||
};
|
||||
|
||||
while (index < sql.length) {
|
||||
const char = sql[index] ?? "";
|
||||
const next = sql[index + 1] ?? "";
|
||||
|
||||
if (state === "lineComment") {
|
||||
if (char === "\n" || char === "\r") state = "none";
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (state === "blockComment") {
|
||||
if (char === "*" && next === "/") {
|
||||
state = "none";
|
||||
index += 2;
|
||||
} else {
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (state === "dollar") {
|
||||
if (sql.startsWith(dollarTag, index)) {
|
||||
index += dollarTag.length;
|
||||
state = "none";
|
||||
} else {
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (state === "single" || state === "double" || state === "backtick") {
|
||||
const quote = state === "single" ? "'" : state === "double" ? '"' : "`";
|
||||
if (char === quote) {
|
||||
if (next === quote) {
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
state = "none";
|
||||
} else if (char === "\\" && next) {
|
||||
// Preserve dialects that accept backslash escapes without letting an escaped quote end the literal.
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (state === "bracket") {
|
||||
if (char === "]") {
|
||||
if (next === "]") {
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
state = "none";
|
||||
}
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "-" && next === "-") {
|
||||
state = "lineComment";
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (char === "#") {
|
||||
state = "lineComment";
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (char === "/" && next === "*") {
|
||||
state = "blockComment";
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (char === "'") state = "single";
|
||||
else if (char === '"') state = "double";
|
||||
else if (char === "`") state = "backtick";
|
||||
else if (char === "[") state = "bracket";
|
||||
else if (char === "$") {
|
||||
const match = /^\$[A-Za-z_0-9]*\$/.exec(sql.slice(index));
|
||||
if (match) {
|
||||
dollarTag = match[0];
|
||||
state = "dollar";
|
||||
index += dollarTag.length;
|
||||
continue;
|
||||
}
|
||||
} else if (char === ";") {
|
||||
pushStatement(index);
|
||||
statementStart = index + 1;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
|
||||
pushStatement(sql.length);
|
||||
return statements;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user