fix(sql): preserve TDSQL proxy directive

Closes #6043
This commit is contained in:
t8y2 committed 2026-08-12 17:02:19 +00:00
1 parent e18d1ddedf
commit 6f3895edd8
4 files changed
+138 -3

No files matched your search

@@ -13,6 +13,12 @@ describe("resolveExecutableSql", () => {
expect(resolveExecutableSql(sql, selectedSql, { mode: "current", cursorPos: cursorAfterFirstSemicolon })).toBe("select 2;");
});
it("keeps a manually selected proxy directive unchanged", () => {
const selectedSql = "/*proxy*/\nSHOW PROXY STATUS";
expect(resolveExecutableSql("SELECT 1;", selectedSql, { mode: "current", cursorPos: 0 })).toBe(selectedSql);
});
});
describe("executionCandidateForMode", () => {
@@ -1199,6 +1199,44 @@ describe("buildExecutionCandidates", () => {
expect(splitSqlStatementRanges("/*&tenant:mctest*/", "mysql")).toEqual([]);
});
it("preserves only the exact TDSQL proxy directive for the current statement", () => {
const directedSql = "/*proxy*/ \n\tSHOW PROXY STATUS";
const sql = `SELECT 1;\n${directedSql};\nSELECT 2;`;
const candidates = buildExecutionCandidates(sql, indexOf(sql, "STATUS"), "mysql");
expect(executionCandidateForMode(candidates, "current")?.sql).toBe(directedSql);
expect(executionCandidateForMode(candidates, "all")?.sql).toBe(sql);
expect(rangeSqlTexts(executableStatementRanges(sql, "mysql"))).toEqual(["SELECT 1", directedSql, "SELECT 2"]);
expect(splitSqlStatementRanges("/*proxy*/SHOW PROXY STATUS", "mysql")[0]?.sql).toBe("/*proxy*/SHOW PROXY STATUS");
expect(splitSqlStatementRanges("/* ordinary */\n/*proxy*/SHOW PROXY STATUS", "mysql")[0]?.sql).toBe("/*proxy*/SHOW PROXY STATUS");
expect(splitSqlStatementRanges("/*proxy*/", "mysql")).toEqual([]);
});
it.each(["/* ordinary */", "/*unknown*/", "/* proxy */", "/*PROXY*/"])("keeps %s as a non-executable leading comment", (comment) => {
const sql = `${comment}\nSHOW PROXY STATUS`;
const candidates = buildExecutionCandidates(sql, indexOf(sql, "STATUS"), "mysql");
expect(executionCandidateForMode(candidates, "current")?.sql).toBe("SHOW PROXY STATUS");
});
it.each(["/*proxy*/\n/* audit */\nSHOW PROXY STATUS", "/*proxy*/\n-- audit\nSHOW PROXY STATUS"])("does not preserve a proxy directive separated from SQL by another comment", (sql) => {
const candidates = buildExecutionCandidates(sql, indexOf(sql, "STATUS"), "mysql");
expect(executionCandidateForMode(candidates, "current")?.sql).toBe("SHOW PROXY STATUS");
});
it("does not preserve the proxy directive for other database types", () => {
const sql = "/*proxy*/\nSHOW PROXY STATUS";
const candidates = buildExecutionCandidates(sql, indexOf(sql, "STATUS"), "postgres");
expect(executionCandidateForMode(candidates, "current")?.sql).toBe("SHOW PROXY STATUS");
});
it("keeps existing hints without allowing them between the proxy directive and SQL", () => {
expect(splitSqlStatementRanges("/*+ route */\n/*proxy*/SHOW PROXY STATUS", "mysql")[0]?.sql).toBe("/*proxy*/SHOW PROXY STATUS");
expect(splitSqlStatementRanges("/*proxy*/\n/*+ route */\nSHOW PROXY STATUS", "mysql")[0]?.sql).toBe("/*+ route */\nSHOW PROXY STATUS");
});
it("uses the cursor statement for the first candidate when there is no selection", () => {
const sql = "SELECT *\nFROM users\nWHERE active = 1";
const candidates = buildExecutionCandidates(sql, indexOf(sql, "users"));