feat(editor): configure SQL variable syntax
This commit is contained in:
1 parent
54632f4c16
commit
1a309b6b63
18 files changed
+450
-24
No files matched your search
@@ -276,6 +276,45 @@ DEALLOCATE PREPARE stmt;`;
|
||||
});
|
||||
});
|
||||
|
||||
describe("enabledSyntaxes option", () => {
|
||||
const mixedSql = "select ? as a, :named as b, ${shell_name} as c, #{mybatis_name} as d, @sql_server_name as e";
|
||||
|
||||
it("extracts every syntax when the option is omitted (backward compatible)", () => {
|
||||
expect(extractSqlParameters(mixedSql)).toEqual(["?1", "named", "shell_name", "mybatis_name", "sql_server_name"]);
|
||||
});
|
||||
|
||||
it("only extracts the enabled syntaxes", () => {
|
||||
expect(extractSqlParameters(mixedSql, { enabledSyntaxes: ["named"] })).toEqual(["named"]);
|
||||
expect(extractSqlParameters(mixedSql, { enabledSyntaxes: ["shell", "mybatis"] })).toEqual(["shell_name", "mybatis_name"]);
|
||||
});
|
||||
|
||||
it("extracts nothing when no syntax is enabled", () => {
|
||||
expect(extractSqlParameters(mixedSql, { enabledSyntaxes: [] })).toEqual([]);
|
||||
});
|
||||
|
||||
it("leaves disabled-syntax tokens untouched when substituting", () => {
|
||||
// Only :named is enabled, so every other token survives verbatim.
|
||||
expect(substituteSqlParameters(mixedSql, { named: { kind: "number", value: "2" } }, { enabledSyntaxes: ["named"] })).toBe("select ? as a, 2 as b, ${shell_name} as c, #{mybatis_name} as d, @sql_server_name as e");
|
||||
});
|
||||
|
||||
it("does not consume the positional counter for disabled positional placeholders", () => {
|
||||
expect(substituteSqlParameters("select ?, ?", {}, { enabledSyntaxes: ["named"] })).toBe("select ?, ?");
|
||||
});
|
||||
|
||||
it("keeps #{name} out of hash-comment handling when mybatis is disabled", () => {
|
||||
expect(extractSqlParameters("select #{mybatis_name} from t", { enabledSyntaxes: ["shell"] })).toEqual([]);
|
||||
expect(substituteSqlParameters("select #{mybatis_name} from t", {}, { enabledSyntaxes: ["shell"] })).toBe("select #{mybatis_name} from t");
|
||||
});
|
||||
|
||||
it("intersects the enabled set with the saphana named-parameter rule", () => {
|
||||
const sql = "select :named as a, ${shell_name} as b";
|
||||
// saphana already disables :name; enabling named cannot re-enable it.
|
||||
expect(extractSqlParameters(sql, { databaseType: "saphana", enabledSyntaxes: ["named", "shell"] })).toEqual(["shell_name"]);
|
||||
// A non-saphana database with named disabled also drops :name.
|
||||
expect(extractSqlParameters(sql, { enabledSyntaxes: ["shell"] })).toEqual(["shell_name"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sqlParameterLiteral", () => {
|
||||
it("falls back to quoted strings for invalid boolean input", () => {
|
||||
expect(sqlParameterLiteral({ kind: "boolean", value: "maybe" })).toBe("'maybe'");
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { reactive } from "vue";
|
||||
import { DEFAULT_SQL_VARIABLE_SYNTAX_TOGGLES, enabledSqlParameterSyntaxes, normalizeSqlVariableSyntaxOverrides, resolveSqlVariableSyntaxToggles } from "@/lib/sql/sqlVariableSyntax";
|
||||
|
||||
describe("resolveSqlVariableSyntaxToggles", () => {
|
||||
it("enables every syntax when there are no overrides", () => {
|
||||
expect(resolveSqlVariableSyntaxToggles(undefined, "mysql")).toEqual(DEFAULT_SQL_VARIABLE_SYNTAX_TOGGLES);
|
||||
expect(resolveSqlVariableSyntaxToggles({}, "mysql")).toEqual(DEFAULT_SQL_VARIABLE_SYNTAX_TOGGLES);
|
||||
});
|
||||
|
||||
it("enables every syntax for a database type without an entry", () => {
|
||||
expect(resolveSqlVariableSyntaxToggles({ mysql: { shell: false } }, "postgres")).toEqual(DEFAULT_SQL_VARIABLE_SYNTAX_TOGGLES);
|
||||
});
|
||||
|
||||
it("enables every syntax when the database type is unknown", () => {
|
||||
expect(resolveSqlVariableSyntaxToggles({ mysql: { shell: false } }, undefined)).toEqual(DEFAULT_SQL_VARIABLE_SYNTAX_TOGGLES);
|
||||
});
|
||||
|
||||
it("applies only the disabled syntaxes for the matching database type", () => {
|
||||
expect(resolveSqlVariableSyntaxToggles({ mysql: { sqlserver: false, atSet: false } }, "mysql")).toEqual({
|
||||
positional: true,
|
||||
named: true,
|
||||
shell: true,
|
||||
mybatis: true,
|
||||
sqlserver: false,
|
||||
atSet: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("enabledSqlParameterSyntaxes", () => {
|
||||
it("returns the five placeholder syntaxes and never atSet", () => {
|
||||
expect(enabledSqlParameterSyntaxes(DEFAULT_SQL_VARIABLE_SYNTAX_TOGGLES)).toEqual(["positional", "named", "shell", "mybatis", "sqlserver"]);
|
||||
});
|
||||
|
||||
it("filters out disabled placeholder syntaxes", () => {
|
||||
expect(enabledSqlParameterSyntaxes({ positional: false, named: true, shell: false, mybatis: true, sqlserver: false, atSet: true })).toEqual(["named", "mybatis"]);
|
||||
});
|
||||
|
||||
it("ignores the atSet toggle entirely", () => {
|
||||
expect(enabledSqlParameterSyntaxes({ positional: true, named: true, shell: true, mybatis: true, sqlserver: true, atSet: false })).toEqual(["positional", "named", "shell", "mybatis", "sqlserver"]);
|
||||
expect(enabledSqlParameterSyntaxes({ positional: false, named: false, shell: false, mybatis: false, sqlserver: false, atSet: true })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeSqlVariableSyntaxOverrides", () => {
|
||||
it("returns an empty object for non-object input", () => {
|
||||
expect(normalizeSqlVariableSyntaxOverrides(undefined)).toEqual({});
|
||||
expect(normalizeSqlVariableSyntaxOverrides(null)).toEqual({});
|
||||
expect(normalizeSqlVariableSyntaxOverrides([])).toEqual({});
|
||||
expect(normalizeSqlVariableSyntaxOverrides("mysql")).toEqual({});
|
||||
});
|
||||
|
||||
it("keeps only syntaxes explicitly set to false", () => {
|
||||
expect(normalizeSqlVariableSyntaxOverrides({ mysql: { positional: true, shell: false, atSet: false } })).toEqual({ mysql: { shell: false, atSet: false } });
|
||||
});
|
||||
|
||||
it("drops entries with no disabled syntaxes", () => {
|
||||
expect(normalizeSqlVariableSyntaxOverrides({ mysql: { positional: true }, postgres: {} })).toEqual({});
|
||||
});
|
||||
|
||||
it("ignores non-boolean values and unknown keys", () => {
|
||||
expect(normalizeSqlVariableSyntaxOverrides({ mysql: { shell: "false", named: 0, bogus: false, sqlserver: false } })).toEqual({ mysql: { sqlserver: false } });
|
||||
});
|
||||
|
||||
it("skips non-object database entries", () => {
|
||||
expect(normalizeSqlVariableSyntaxOverrides({ mysql: null, postgres: [], sqlserver: { shell: false } })).toEqual({ sqlserver: { shell: false } });
|
||||
});
|
||||
|
||||
it("is stable across a round-trip", () => {
|
||||
const normalized = normalizeSqlVariableSyntaxOverrides({ mysql: { shell: false }, oracle: { atSet: false, named: false } });
|
||||
expect(normalizeSqlVariableSyntaxOverrides(normalized)).toEqual(normalized);
|
||||
});
|
||||
|
||||
it("reads through a Vue reactive proxy without throwing (settings store is reactive; structuredClone would throw DataCloneError here)", () => {
|
||||
const overrides = reactive({ mysql: { shell: false, atSet: false } });
|
||||
const cloned = normalizeSqlVariableSyntaxOverrides(overrides);
|
||||
expect(cloned).toEqual({ mysql: { shell: false, atSet: false } });
|
||||
// Detached from the reactive source: mutating the clone must not touch the proxy.
|
||||
cloned.mysql = {};
|
||||
expect(overrides.mysql).toEqual({ shell: false, atSet: false });
|
||||
});
|
||||
});
|
||||
@@ -47,6 +47,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
|
||||
"updateDownloadSource",
|
||||
"toolbarItems",
|
||||
"snippets",
|
||||
"sqlVariableSyntaxOverrides",
|
||||
] as const satisfies readonly (keyof EditorSettings)[];
|
||||
|
||||
export type EditorSettingsDraftKey = (typeof EDITOR_SETTINGS_DRAFT_KEYS)[number];
|
||||
|
||||
@@ -23,6 +23,8 @@ interface ParameterOccurrence extends SqlParameterDescriptor {
|
||||
|
||||
export interface SqlParameterOptions {
|
||||
databaseType?: DatabaseType;
|
||||
// Which placeholder syntaxes are recognized. Undefined enables all of them.
|
||||
enabledSyntaxes?: readonly SqlParameterSyntax[];
|
||||
}
|
||||
|
||||
const PARAMETER_NAME_RE = /^[\p{L}_][\p{L}\p{N}_]*$/u;
|
||||
@@ -78,6 +80,8 @@ function findSqlParameterOccurrences(sql: string, options?: SqlParameterOptions)
|
||||
const occurrences: ParameterOccurrence[] = [];
|
||||
const nativeSqlServerParameters = collectNativeSqlServerParameters(sql);
|
||||
const supportsNamedParameters = options?.databaseType !== "saphana";
|
||||
const enabledSyntaxes = options?.enabledSyntaxes ? new Set(options.enabledSyntaxes) : null;
|
||||
const isSyntaxEnabled = (syntax: SqlParameterSyntax) => !enabledSyntaxes || enabledSyntaxes.has(syntax);
|
||||
let i = 0;
|
||||
let dollarQuoteEnd = "";
|
||||
let positionalIndex = 0;
|
||||
@@ -110,14 +114,14 @@ function findSqlParameterOccurrences(sql: string, options?: SqlParameterOptions)
|
||||
i = skipBlockComment(sql, i + 2);
|
||||
continue;
|
||||
}
|
||||
if (ch === "?") {
|
||||
if (ch === "?" && isSyntaxEnabled("positional")) {
|
||||
positionalIndex += 1;
|
||||
const key = `?${positionalIndex}`;
|
||||
occurrences.push({ key, name: key, syntax: "positional", token: "?", start: i, end: i + 1 });
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === ":" && supportsNamedParameters) {
|
||||
if (ch === ":" && supportsNamedParameters && isSyntaxEnabled("named")) {
|
||||
const name = readParameterName(sql, i + 1);
|
||||
if (name && sql[i - 1] !== ":" && sql[i + 1] !== "=") {
|
||||
occurrences.push({
|
||||
@@ -132,7 +136,7 @@ function findSqlParameterOccurrences(sql: string, options?: SqlParameterOptions)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (ch === "$" && next === "{") {
|
||||
if (ch === "$" && next === "{" && isSyntaxEnabled("shell")) {
|
||||
const end = sql.indexOf("}", i + 2);
|
||||
if (end !== -1) {
|
||||
const name = sql.slice(i + 2, end).trim();
|
||||
@@ -143,7 +147,7 @@ function findSqlParameterOccurrences(sql: string, options?: SqlParameterOptions)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ch === "#" && next === "{") {
|
||||
if (ch === "#" && next === "{" && isSyntaxEnabled("mybatis")) {
|
||||
const end = sql.indexOf("}", i + 2);
|
||||
if (end !== -1) {
|
||||
const name = sql.slice(i + 2, end).trim();
|
||||
@@ -158,7 +162,7 @@ function findSqlParameterOccurrences(sql: string, options?: SqlParameterOptions)
|
||||
i = skipLine(sql, i + 1);
|
||||
continue;
|
||||
}
|
||||
if (ch === "@") {
|
||||
if (ch === "@" && isSyntaxEnabled("sqlserver")) {
|
||||
const name = readParameterName(sql, i + 1);
|
||||
if (name && next !== "@" && sql[i - 1] !== "@" && !nativeSqlServerParameters.declared.has(name.toLowerCase()) && !nativeSqlServerParameters.ignoredStarts.has(i)) {
|
||||
occurrences.push({
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
// Per-database-type configuration for SQL variable/placeholder substitution.
|
||||
//
|
||||
// DBX runs two client-side substitution systems before sending SQL to a backend:
|
||||
// the placeholder parameter dialog (`sqlParameters.ts`, five syntaxes) and the
|
||||
// `@set name = value;` expansion (`sqlVariables.ts`). This module lets users opt
|
||||
// out of individual syntaxes per database type. Every toggle defaults to `true`,
|
||||
// so an empty/absent config reproduces the historical "always substitute" behaviour.
|
||||
//
|
||||
// Storage is sparse: only syntaxes explicitly turned off (`false`) are persisted,
|
||||
// keyed by database type. Anything not stored resolves to enabled.
|
||||
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
import type { SqlParameterSyntax } from "@/lib/sql/sqlParameters";
|
||||
import { manifestDatabaseTypes } from "@/lib/database/databaseDriverManifest";
|
||||
|
||||
export interface SqlVariableSyntaxToggles {
|
||||
positional: boolean; // ?
|
||||
named: boolean; // :name
|
||||
shell: boolean; // ${name}
|
||||
mybatis: boolean; // #{name}
|
||||
sqlserver: boolean; // @name
|
||||
atSet: boolean; // @set name = value; (expandSqlVariables)
|
||||
}
|
||||
|
||||
export const DEFAULT_SQL_VARIABLE_SYNTAX_TOGGLES: SqlVariableSyntaxToggles = {
|
||||
positional: true,
|
||||
named: true,
|
||||
shell: true,
|
||||
mybatis: true,
|
||||
sqlserver: true,
|
||||
atSet: true,
|
||||
};
|
||||
|
||||
// Fixed order for iterating the toggles in the settings UI.
|
||||
export const SQL_VARIABLE_SYNTAX_KEYS = ["positional", "named", "shell", "mybatis", "sqlserver", "atSet"] as const satisfies readonly (keyof SqlVariableSyntaxToggles)[];
|
||||
|
||||
// Display tokens (code symbols, not translated) shown next to each toggle.
|
||||
export const SQL_VARIABLE_SYNTAX_TOKENS: Record<keyof SqlVariableSyntaxToggles, string> = {
|
||||
positional: "?",
|
||||
named: ":name",
|
||||
shell: "${name}",
|
||||
mybatis: "#{name}",
|
||||
sqlserver: "@name",
|
||||
atSet: "@set …;",
|
||||
};
|
||||
|
||||
// The first five toggles map one-to-one onto placeholder parameter syntaxes.
|
||||
const PARAMETER_SYNTAX_KEYS = ["positional", "named", "shell", "mybatis", "sqlserver"] as const satisfies readonly SqlParameterSyntax[];
|
||||
|
||||
export type SqlVariableSyntaxOverrides = Partial<Record<DatabaseType, Partial<SqlVariableSyntaxToggles>>>;
|
||||
|
||||
// Database types offered in the settings selector — every connectable type.
|
||||
export const SQL_VARIABLE_SYNTAX_DATABASE_TYPES: DatabaseType[] = manifestDatabaseTypes();
|
||||
|
||||
/**
|
||||
* Resolve the effective toggles for a database type. Any syntax not explicitly
|
||||
* disabled in `overrides` is enabled. A pure function safe to call on every
|
||||
* execution.
|
||||
*/
|
||||
export function resolveSqlVariableSyntaxToggles(overrides: SqlVariableSyntaxOverrides | undefined, dbType: DatabaseType | undefined): SqlVariableSyntaxToggles {
|
||||
const partial = dbType ? overrides?.[dbType] : undefined;
|
||||
return {
|
||||
positional: partial?.positional ?? true,
|
||||
named: partial?.named ?? true,
|
||||
shell: partial?.shell ?? true,
|
||||
mybatis: partial?.mybatis ?? true,
|
||||
sqlserver: partial?.sqlserver ?? true,
|
||||
atSet: partial?.atSet ?? true,
|
||||
};
|
||||
}
|
||||
|
||||
/** Derive the enabled placeholder parameter syntaxes (excludes `atSet`). */
|
||||
export function enabledSqlParameterSyntaxes(toggles: SqlVariableSyntaxToggles): SqlParameterSyntax[] {
|
||||
return PARAMETER_SYNTAX_KEYS.filter((key) => toggles[key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize persisted overrides into a sparse structure: drop non-object entries
|
||||
* and only keep syntaxes explicitly set to `false`. Absence resolves to enabled.
|
||||
*/
|
||||
export function normalizeSqlVariableSyntaxOverrides(value: unknown): SqlVariableSyntaxOverrides {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
||||
const result: SqlVariableSyntaxOverrides = {};
|
||||
for (const [dbType, raw] of Object.entries(value as Record<string, unknown>)) {
|
||||
if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue;
|
||||
const partial: Partial<SqlVariableSyntaxToggles> = {};
|
||||
for (const key of SQL_VARIABLE_SYNTAX_KEYS) {
|
||||
if ((raw as Record<string, unknown>)[key] === false) partial[key] = false;
|
||||
}
|
||||
if (Object.keys(partial).length > 0) result[dbType as DatabaseType] = partial;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user