mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
Snowflake is a built-in data source kind: connection dialog, key-pair, browser single sign-on, password or token sign-in, the database tree, SQL consoles, and Snowflake-aware SQL assistance and syntax colours. The driver speaks Snowflake's session protocol over the existing reqwest + rustls stack, with no JDBC/ODBC or native library and no new package. Metadata is read with SHOW / DESCRIBE, which need no running warehouse; introspected names are kept on disk for completion. Co-authored-by: vvnadolskyy <12991410+vvnadolskyy@users.noreply.github.com>
67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
/**
|
|
* Query parameters of console statements (`:id`, `?`, `?1`, `@name`, `$1`),
|
|
* filled in before execution as IDEA does: each value is inserted as SQL
|
|
* text, so strings carry their quotes. Named parameters share one value;
|
|
* every bare `?` is its own.
|
|
*/
|
|
import type { DbDriver } from "@/platform/ipc";
|
|
import { lex } from "./lexer";
|
|
|
|
export interface SqlParam {
|
|
/** Shown in the dialog and the key of its value (`:id`, `?2`). */
|
|
name: string;
|
|
from: number;
|
|
to: number;
|
|
/** A bare `?`, numbered in order. */
|
|
positional?: boolean;
|
|
}
|
|
|
|
/** Parameters of `text`; bare `?` are numbered from `numbered + 1` (earlier statements' count). */
|
|
export function findParams(text: string, driver: DbDriver, numbered = 0): SqlParam[] {
|
|
const toks = lex(text, driver).tokens;
|
|
const out: SqlParam[] = [];
|
|
let positional = numbered;
|
|
let typedEnd = -1;
|
|
toks.forEach((t, i) => {
|
|
if (t.from < typedEnd) return;
|
|
if (driver === "clickhouse" && t.kind === "punct" && t.text === "{") {
|
|
const end = toks.slice(i + 1).find((candidate) => candidate.kind === "punct" && candidate.text === "}");
|
|
const name = end ? text.slice(t.from, end.to) : "";
|
|
if (end && /^\{[A-Za-z_][A-Za-z0-9_]*\s*:\s*[^{}]+\}$/.test(name)) {
|
|
out.push({ name, from: t.from, to: end.to });
|
|
typedEnd = end.to;
|
|
return;
|
|
}
|
|
}
|
|
if (t.kind === "param") {
|
|
// MySQL `@var` / `@@var` are session variables.
|
|
if (driver === "mysql" && t.text.startsWith("@")) return;
|
|
// Snowflake `@stage` references, `$1` columns, `$name` session variables and `file://` locations.
|
|
if (driver === "snowflake" && /^[@$]|^\w+:\/\//.test(t.text)) return;
|
|
if (toks[i - 1]?.text === "@") return;
|
|
out.push({ name: t.text, from: t.from, to: t.to });
|
|
} else if (t.kind === "punct" && t.text === "?" && driver !== "postgres") {
|
|
// PostgreSQL's `?` is a jsonb operator; its parameters are `$1`.
|
|
const n = toks[i + 1];
|
|
if (n?.kind === "number" && n.from === t.to) out.push({ name: `?${n.text}`, from: t.from, to: n.to });
|
|
else out.push({ name: `?${++positional}`, from: t.from, to: t.to, positional: true });
|
|
}
|
|
});
|
|
return out;
|
|
}
|
|
|
|
/** `text` with each parameter replaced by its value (empty: `NULL`). */
|
|
export function bindParams(
|
|
text: string,
|
|
params: readonly SqlParam[],
|
|
values: Readonly<Record<string, string>>,
|
|
): string {
|
|
let out = "";
|
|
let at = 0;
|
|
for (const p of params) {
|
|
out += text.slice(at, p.from) + (values[p.name]?.trim() || "NULL");
|
|
at = p.to;
|
|
}
|
|
return out + text.slice(at);
|
|
}
|