fix(sql): treat GET DIAGNOSTICS targets as MySQL user variables

Refs: #8927
This commit is contained in:
Thanadon Kongkanun authored and GitHub committed 2026-09-12 21:42:32 +08:00
1 parent e03046ba55
commit 4bac6b56de
2 files changed
+52

No files matched your search

@@ -292,6 +292,41 @@ describe("extractSqlParameters", () => {
expect(extractSqlParameters(sql, { databaseType: "mysql" })).toEqual(["input_id", "tenant_id"]);
});
it("ignores MySQL user variables assigned by GET DIAGNOSTICS", () => {
const sql = `
get diagnostics condition 1 @err_state = returned_sqlstate, @err_msg = message_text;
select concat('failed: ', @err_state, ' ', @err_msg) as result;
`;
expect(extractSqlParameters(sql, { databaseType: "mysql" })).toEqual([]);
});
it("ignores GET CURRENT/STACKED DIAGNOSTICS targets and the statement-level row count", () => {
const sql = `
get diagnostics @affected = row_count;
get current diagnostics condition 1 @current_state = returned_sqlstate;
get stacked diagnostics condition 1 @stacked_state = returned_sqlstate;
select @affected, @current_state, @stacked_state;
`;
expect(extractSqlParameters(sql, { databaseType: "mysql" })).toEqual([]);
});
it("keeps ordinary MySQL template parameters next to GET DIAGNOSTICS targets", () => {
const sql = `
get diagnostics condition 1 @err_msg = message_text;
select * from audit_log where tenant_id = @tenant_id and note = @err_msg;
`;
expect(extractSqlParameters(sql, { databaseType: "mysql" })).toEqual(["tenant_id"]);
});
it("keeps a template parameter on a column named get", () => {
const sql = "select get from api_methods where tenant_id = @tenant_id";
expect(extractSqlParameters(sql, { databaseType: "mysql" })).toEqual(["tenant_id"]);
});
it("ignores SQL Server procedure parameters declared in routine definitions", () => {
const sql = `
create procedure dbo.search_orders
+17
View File
@@ -1278,6 +1278,13 @@ function collectNativeSqlServerParameters(sql: string, databaseType?: DatabaseTy
i = collectSelectAssignmentVariables(sql, i + "select".length, declared, databaseType);
continue;
}
if (matchesWord(sql, i, "get")) {
const diagnosticsEnd = readGetDiagnosticsEnd(sql, i);
if (diagnosticsEnd !== null) {
i = collectSetStatementVariables(sql, diagnosticsEnd, declared, databaseType);
continue;
}
}
if ((matchesWord(sql, i, "create") || matchesWord(sql, i, "alter")) && isRoutineDefinitionStart(sql, i)) {
i = collectRoutineDefinitionVariables(sql, i, declared, databaseType);
continue;
@@ -1526,6 +1533,16 @@ function collectExecNamedArgumentStarts(sql: string, start: number, ignoredStart
return i;
}
// MySQL's GET DIAGNOSTICS writes its results into the user variables on the left
// of each assignment (`GET DIAGNOSTICS CONDITION 1 @err = MESSAGE_TEXT`), exactly
// as SET and SELECT ... INTO do. Returns the offset just past the statement's
// leading keywords, or null when `start` is an ordinary `get` identifier.
function readGetDiagnosticsEnd(sql: string, start: number): number | null {
let keyword = readNextKeyword(sql, start + "get".length);
if (keyword?.word === "current" || keyword?.word === "stacked") keyword = readNextKeyword(sql, keyword.end);
return keyword?.word === "diagnostics" ? keyword.end : null;
}
function isRoutineDefinitionStart(sql: string, start: number): boolean {
const keyword = matchesWord(sql, start, "create") ? "create" : matchesWord(sql, start, "alter") ? "alter" : "";
if (!keyword) return false;