fix(editor): substitute sql parameters before explain

This commit is contained in:
jearhe authored and GitHub committed 2026-09-02 20:36:33 +08:00
1 parent 8305d63ea1
commit d6c4457756
3 files changed
+158 -2

No files matched your search

@@ -1322,6 +1322,128 @@ SELECT @value AS Message;`;
expect(explainTabSql).toHaveBeenCalledWith("tab-1", sql, "oracle", "explain"); expect(explainTabSql).toHaveBeenCalledWith("tab-1", sql, "oracle", "explain");
}); });
it("explains the resolved PostgreSQL statement after SQL parameter input", async () => {
const sql = "SELECT :var;";
const resolvedSql = "SELECT 123;";
const activeTab = ref<QueryTab | undefined>({ ...queryTab("app"), sql });
const activeConnection = ref<ConnectionConfig | undefined>(connection("postgres"));
const activeOutputView = ref<"result" | "summary" | "explain" | "chart">("result");
const queryStore = useQueryStore();
const explainTabSql = vi.spyOn(queryStore, "explainTabSql").mockResolvedValue({ ok: true, sql: resolvedSql });
const execution = useSqlExecution({
activeTab: computed(() => activeTab.value),
activeConnection: computed(() => activeConnection.value),
executableSql: computed(() => sql),
activeOutputView,
});
await execution.tryExplain();
expect(execution.showSqlParameterDialog.value).toBe(true);
expect(execution.sqlParameterNames.value.map((parameter) => parameter.name)).toEqual(["var"]);
expect(explainTabSql).not.toHaveBeenCalled();
await execution.onSqlParametersConfirm(resolvedSql);
expect(explainTabSql).toHaveBeenCalledWith("tab-1", resolvedSql, "postgres", "explain");
});
it("explains the analyzed PostgreSQL statement after SQL parameter input", async () => {
const sql = "SELECT :var;";
const resolvedSql = "SELECT 123;";
const activeTab = ref<QueryTab | undefined>({ ...queryTab("app"), sql });
const activeConnection = ref<ConnectionConfig | undefined>(connection("postgres"));
const activeOutputView = ref<"result" | "summary" | "explain" | "chart">("result");
const queryStore = useQueryStore();
const explainTabSql = vi.spyOn(queryStore, "explainTabSql").mockResolvedValue({ ok: true, sql: resolvedSql });
const execution = useSqlExecution({
activeTab: computed(() => activeTab.value),
activeConnection: computed(() => activeConnection.value),
executableSql: computed(() => sql),
activeOutputView,
});
execution.explainMode.value = "autotrace";
await execution.tryExplain();
expect(execution.showSqlParameterDialog.value).toBe(true);
expect(explainTabSql).not.toHaveBeenCalled();
await execution.onSqlParametersConfirm(resolvedSql);
expect(explainTabSql).toHaveBeenCalledWith("tab-1", resolvedSql, "postgres", "autotrace");
});
it("explains the string-parameter statement with the dialog's escaped substitution", async () => {
const sql = "SELECT :name;";
const resolvedSql = "SELECT 'O''Reilly';";
const activeTab = ref<QueryTab | undefined>({ ...queryTab("app"), sql });
const activeConnection = ref<ConnectionConfig | undefined>(connection("postgres"));
const activeOutputView = ref<"result" | "summary" | "explain" | "chart">("result");
const queryStore = useQueryStore();
const explainTabSql = vi.spyOn(queryStore, "explainTabSql").mockResolvedValue({ ok: true, sql: resolvedSql });
const execution = useSqlExecution({
activeTab: computed(() => activeTab.value),
activeConnection: computed(() => activeConnection.value),
executableSql: computed(() => sql),
activeOutputView,
});
await execution.tryExplain();
expect(execution.showSqlParameterDialog.value).toBe(true);
await execution.onSqlParametersConfirm(resolvedSql);
expect(explainTabSql).toHaveBeenCalledWith("tab-1", resolvedSql, "postgres", "explain");
});
it("explains a PostgreSQL cast statement without opening the parameter dialog", async () => {
const sql = "SELECT '1'::int;";
const activeTab = ref<QueryTab | undefined>({ ...queryTab("app"), sql });
const activeConnection = ref<ConnectionConfig | undefined>(connection("postgres"));
const activeOutputView = ref<"result" | "summary" | "explain" | "chart">("result");
const queryStore = useQueryStore();
const explainTabSql = vi.spyOn(queryStore, "explainTabSql").mockResolvedValue({ ok: true, sql });
const execution = useSqlExecution({
activeTab: computed(() => activeTab.value),
activeConnection: computed(() => activeConnection.value),
executableSql: computed(() => sql),
activeOutputView,
});
await execution.tryExplain();
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(explainTabSql).toHaveBeenCalledWith("tab-1", sql, "postgres", "explain");
});
it("explains a parameter-free statement without opening the parameter dialog", async () => {
const sql = "SELECT * FROM orders;";
const activeTab = ref<QueryTab | undefined>({ ...queryTab("app"), sql });
const activeConnection = ref<ConnectionConfig | undefined>(connection("postgres"));
const activeOutputView = ref<"result" | "summary" | "explain" | "chart">("result");
const queryStore = useQueryStore();
const explainTabSql = vi.spyOn(queryStore, "explainTabSql").mockResolvedValue({ ok: true, sql });
const execution = useSqlExecution({
activeTab: computed(() => activeTab.value),
activeConnection: computed(() => activeConnection.value),
executableSql: computed(() => sql),
activeOutputView,
});
await execution.tryExplain();
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(explainTabSql).toHaveBeenCalledWith("tab-1", sql, "postgres", "explain");
});
it("keeps the new-result-tab intent through Redis command confirmation", async () => { it("keeps the new-result-tab intent through Redis command confirmation", async () => {
const sql = "DEL user:1"; const sql = "DEL user:1";
const activeTab = ref<QueryTab | undefined>({ ...queryTab("0"), sql }); const activeTab = ref<QueryTab | undefined>({ ...queryTab("0"), sql });
@@ -541,9 +541,8 @@ export function useSqlExecution(deps: {
return t("explain.emptySql"); return t("explain.emptySql");
} }
async function tryExplain(sqlOverride?: SqlExecutionOverride) { async function runExplain(sql: string) {
const tab = deps.activeTab.value; const tab = deps.activeTab.value;
const { sql } = await resolvedExecutableSql(sqlOverride);
if (!tab || !sql.trim()) { if (!tab || !sql.trim()) {
toast(t("explain.emptySql")); toast(t("explain.emptySql"));
return; return;
@@ -562,6 +561,19 @@ export function useSqlExecution(deps: {
if (current?.explainError) toast(current.explainError, 5000); if (current?.explainError) toast(current.explainError, 5000);
} }
async function tryExplain(sqlOverride?: SqlExecutionOverride) {
const tab = deps.activeTab.value;
const { sql, sourceOffset } = await resolvedExecutableSql(sqlOverride);
if (!tab || !sql.trim()) {
toast(t("explain.emptySql"));
return;
}
// Resolve SQL template variables exactly like tryExecute so EXPLAIN never runs the raw
// placeholders (e.g. `EXPLAIN (FORMAT JSON) SELECT :var;` fails on PostgreSQL).
if (supportsSqlTemplateParameters(deps.activeConnection.value, sql) && prepareSqlParameterDialog(sql, sourceOffset, {}, (resolvedSql) => runExplain(resolvedSql))) return;
await runExplain(sql);
}
async function onDangerConfirm() { async function onDangerConfirm() {
const sql = pendingDangerSql.value; const sql = pendingDangerSql.value;
const sourceOffset = pendingDangerSourceOffset.value; const sourceOffset = pendingDangerSourceOffset.value;
@@ -151,4 +151,26 @@ describe("queryStore PostgreSQL EXPLAIN ANALYZE", () => {
expect(mocks.buildExplainSql).toHaveBeenCalledWith("questdb", SOURCE_SQL); expect(mocks.buildExplainSql).toHaveBeenCalledWith("questdb", SOURCE_SQL);
}); });
it("wraps a resolved statement whose variables were substituted before explain", async () => {
const resolvedSql = "SELECT 123";
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("pg-1", "shop", "Query", "query", "public");
await store.explainTabSql(tabId, resolvedSql, "postgres");
expect(mocks.buildExplainSql).toHaveBeenCalledWith("postgres", resolvedSql);
});
it("keeps the analyze flag for a resolved statement whose variables were substituted", async () => {
const resolvedSql = "SELECT 123";
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("pg-1", "shop", "Query", "query", "public");
await store.explainTabSql(tabId, resolvedSql, "postgres", "autotrace");
expect(mocks.buildExplainSql).toHaveBeenCalledWith("postgres", resolvedSql, "json", true);
});
}); });