mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(database): quote completions of custom JDBC sources by their URL's dialect (#4765)
A custom JDBC source has driver "jdbc", so completion quoted only names that aren't plain words. On Oracle (and other case-folding servers) a mixed-case table such as BAS_MeterUnit was inserted bare and folded to BAS_METERUNIT (ORA-00942). Completion now takes the dialect from the source's URL scheme (jdbc:oracle:thin:@…, jdbc:postgresql://…), using the vendor scheme table Data Source from URL already uses, and quotes table, column and JOIN candidates the way that dialect needs. Unknown schemes keep the previous behaviour. Co-authored-by: eclix-del <56993440+eclix-del@users.noreply.github.com>
This commit is contained in:
@@ -63,20 +63,26 @@ export function shouldAutoCompleteSql(analysis: Analysis): boolean {
|
||||
|
||||
const MAX_ALL_COLUMNS = 3000;
|
||||
|
||||
/** `name`, quoted when the dialect needs it (case, characters, reserved words). */
|
||||
export function quoteIdent(name: string, driver: DbDriver): string {
|
||||
/**
|
||||
* `name`, quoted when the dialect needs it (case, characters, reserved words).
|
||||
* A `jdbc` source quotes by the dialect its URL speaks (`dialect`); without one, only
|
||||
* what is not a plain word is quoted, which is right for the dialects that fold case
|
||||
* neither way.
|
||||
*/
|
||||
export function quoteIdent(name: string, driver: DbDriver, dialect?: DbDriver | null): string {
|
||||
const d = driver === "jdbc" && dialect ? dialect : driver;
|
||||
const plain =
|
||||
driver === "postgres"
|
||||
d === "postgres"
|
||||
? /^[a-z_][a-z0-9_$]*$/
|
||||
: // Oracle and Snowflake fold unquoted names to upper case; a lower-case name must stay quoted.
|
||||
driver === "oracle" || driver === "snowflake"
|
||||
d === "oracle" || d === "snowflake"
|
||||
? /^[A-Z_][A-Z0-9_$]*$/
|
||||
: driver === "clickhouse"
|
||||
: d === "clickhouse"
|
||||
? /^[A-Za-z_][A-Za-z0-9_]*$/
|
||||
: /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
||||
if (plain.test(name) && !isReserved(name, driver)) return name;
|
||||
if (driver === "clickhouse") return `"${name.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
||||
const q = driver === "mysql" ? "`" : '"';
|
||||
if (plain.test(name) && !isReserved(name, d)) return name;
|
||||
if (d === "clickhouse") return `"${name.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
||||
const q = d === "mysql" ? "`" : '"';
|
||||
return q + name.split(q).join(q + q) + q;
|
||||
}
|
||||
|
||||
@@ -121,11 +127,20 @@ export interface CompletionInput {
|
||||
model: DbCompletion | null;
|
||||
analysis: Analysis;
|
||||
driver: DbDriver;
|
||||
/** The dialect a `jdbc` source speaks, from its URL (`jdbcDialect`); quoting follows it. */
|
||||
dialect?: DbDriver | null;
|
||||
/** Data source name, shown next to tables like IDEA does. */
|
||||
sourceName?: string;
|
||||
}
|
||||
|
||||
export function sqlCandidates({ model, analysis: a, driver, sourceName, catalog }: CompletionInput): SqlCandidate[] {
|
||||
export function sqlCandidates({
|
||||
model,
|
||||
analysis: a,
|
||||
driver,
|
||||
dialect,
|
||||
sourceName,
|
||||
catalog,
|
||||
}: CompletionInput): SqlCandidate[] {
|
||||
const eq = nameMatcher(driver);
|
||||
const ctx = a.context;
|
||||
if (ctx.kind === "none") return [];
|
||||
@@ -135,7 +150,7 @@ export function sqlCandidates({ model, analysis: a, driver, sourceName, catalog
|
||||
const kw = (k: string) => (lower ? k.toLowerCase() : k);
|
||||
const tables = model?.tables ?? [];
|
||||
const defaultSchema = model?.defaultSchema ?? "";
|
||||
const exact = (n: string) => quoteIdent(n, driver);
|
||||
const exact = (n: string) => quoteIdent(n, driver, dialect);
|
||||
// Snowflake folds unquoted names to upper case, so a name typed in lower case may stay lower case.
|
||||
const lowerQualifier = !!a.qualifier?.length && a.qualifier.every((p) => /[a-z]/.test(p) && p === p.toLowerCase());
|
||||
// What is typed decides: the name's own prefix, else the qualifier before it, else the statement's keywords.
|
||||
@@ -270,7 +285,7 @@ export function sqlCandidates({ model, analysis: a, driver, sourceName, catalog
|
||||
|
||||
case "table": {
|
||||
const taken = new Set(a.refs.flatMap((r) => [refName(r).toLowerCase(), r.name.toLowerCase()]));
|
||||
if (ctx.join) out.push(...joinCandidates(a, model, driver, taken, kw));
|
||||
if (ctx.join) out.push(...joinCandidates(a, model, driver, dialect, taken, kw));
|
||||
for (const c of a.ctes) out.push({ label: c.name, kind: "table", tail: "(CTE)", boost: 40 });
|
||||
const schemas = new Set<string>();
|
||||
for (const t of tables) {
|
||||
@@ -472,13 +487,14 @@ function joinCandidates(
|
||||
a: Analysis,
|
||||
model: DbCompletion | null,
|
||||
driver: DbDriver,
|
||||
dialect: DbDriver | null | undefined,
|
||||
taken: Set<string>,
|
||||
kw: Kw,
|
||||
): SqlCandidate[] {
|
||||
if (!model) return [];
|
||||
const eq = nameMatcher(driver);
|
||||
const out: SqlCandidate[] = [];
|
||||
const q = (n: string) => quoteIdent(n, driver);
|
||||
const q = (n: string) => quoteIdent(n, driver, dialect);
|
||||
const seen = new Set<string>();
|
||||
const local = (s: string) => eq(s, model.defaultSchema) || driver === "sqlite";
|
||||
const tableText = (schema: string, name: string) => (local(schema) ? q(name) : `${q(schema)}.${q(name)}`);
|
||||
|
||||
@@ -8,6 +8,7 @@ import { identName, inLiteral, lex } from "./lexer";
|
||||
import { DATA_TYPES, statementStarters } from "./dialect";
|
||||
import { bindParams, findParams } from "./params";
|
||||
import { SqlResolver } from "./resolve";
|
||||
import { jdbcDialect } from "../vendors";
|
||||
|
||||
const col = (name: string, dataType = "INTEGER", primaryKey = false, hasDefault = false) => ({
|
||||
name,
|
||||
@@ -280,6 +281,51 @@ describe("completion candidates", () => {
|
||||
expect(quoteIdent('a"b', "oracle")).toBe('"a""b"');
|
||||
});
|
||||
|
||||
it("quotes a custom JDBC source's names by the dialect its URL speaks", () => {
|
||||
expect(jdbcDialect("jdbc:oracle:thin:@//db.local:1521/ORCL")).toBe("oracle");
|
||||
expect(jdbcDialect("JDBC:Oracle:thin:@db:1521:ORCL")).toBe("oracle");
|
||||
expect(jdbcDialect("jdbc:postgresql://db.local/shop")).toBe("postgres");
|
||||
expect(jdbcDialect("jdbc:MariaDB://db/shop")).toBe("mysql");
|
||||
expect(jdbcDialect("jdbc:sqlserver://db;databaseName=shop")).toBe("sqlServer");
|
||||
expect(jdbcDialect("jdbc:unknown://db/shop")).toBeNull();
|
||||
expect(jdbcDialect("")).toBeNull();
|
||||
expect(jdbcDialect(null)).toBeNull();
|
||||
// Oracle folds unquoted names to upper case, so only an upper-case name stays bare.
|
||||
expect(quoteIdent("MyTable", "jdbc", "oracle")).toBe('"MyTable"');
|
||||
expect(quoteIdent("MYTABLE", "jdbc", "oracle")).toBe("MYTABLE");
|
||||
expect(quoteIdent("mytable", "jdbc", "oracle")).toBe('"mytable"');
|
||||
// Without a dialect, a plain word still stays bare: the source never said which server it is.
|
||||
expect(quoteIdent("MyTable", "jdbc")).toBe("MyTable");
|
||||
expect(quoteIdent("MyTable", "jdbc", null)).toBe("MyTable");
|
||||
const hr: DbCompletion = {
|
||||
defaultSchema: "HR",
|
||||
tables: [
|
||||
{
|
||||
schema: "HR",
|
||||
name: "MyTable",
|
||||
kind: "table",
|
||||
columns: [col("ID", "NUMBER", true), col("fullName", "VARCHAR2")],
|
||||
},
|
||||
],
|
||||
foreignKeys: [],
|
||||
};
|
||||
const tables = sqlCandidates({
|
||||
model: hr,
|
||||
analysis: at("SELECT * FROM |", "jdbc"),
|
||||
driver: "jdbc",
|
||||
dialect: jdbcDialect("jdbc:oracle:thin:@//db.local:1521/ORCL"),
|
||||
});
|
||||
expect(tables.find((c) => c.label === "MyTable")?.insert).toBe('"MyTable"');
|
||||
const columns = sqlCandidates({
|
||||
model: hr,
|
||||
analysis: at("SELECT | FROM MyTable", "jdbc"),
|
||||
driver: "jdbc",
|
||||
dialect: "oracle",
|
||||
});
|
||||
expect(columns.find((c) => c.label === "ID")?.insert).toBe("ID");
|
||||
expect(columns.find((c) => c.label === "fullName")?.insert).toBe('"fullName"');
|
||||
});
|
||||
|
||||
it("offers Snowflake vocabulary and quotes names the server folds to upper case", () => {
|
||||
expect(statementStarters("snowflake")).toEqual(
|
||||
expect.arrayContaining([
|
||||
|
||||
Reference in New Issue
Block a user