From aebc7f7f99edc1bf51c1444c48ae9a5ca581d353 Mon Sep 17 00:00:00 2001 From: eclix-del <56993440+eclix-del@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:27:18 +0800 Subject: [PATCH] fix(database): quote completions of custom JDBC sources by their URL's dialect (#4765) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- src/features/database/console.tsx | 5 +++ src/features/database/sql/complete.ts | 40 ++++++++++++++++------- src/features/database/sql/sql.test.ts | 46 +++++++++++++++++++++++++++ src/features/database/vendors.ts | 9 ++++++ 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/src/features/database/console.tsx b/src/features/database/console.tsx index 941227adb..39d419dba 100644 --- a/src/features/database/console.tsx +++ b/src/features/database/console.tsx @@ -112,6 +112,7 @@ const TEMPLATE_CONTEXT_KEY = "openideaTemplateContexts"; import { formatElapsed } from "./resultView"; import { SQL_LANGUAGE } from "./sqlLanguage"; +import { jdbcDialect } from "./vendors"; export { SQL_LANGUAGE }; const ROW_LIMITS = ["100", "500", "1000", "5000", "50000"]; @@ -1069,6 +1070,9 @@ export function completionSource(path: string, sql: SqlText = WHOLE_FILE): Compl const current = id ? databaseRequest(id) : projectRequest(); const source = sourceById(id); const driver: DbDriver = source?.driver ?? "sqlite"; + // A custom JDBC source (`driver: "jdbc"`) says nothing about how its server folds + // identifiers; its URL does (`jdbc:oracle:thin:@…`). Quoting follows that dialect. + const dialect = driver === "jdbc" ? jdbcDialect(source?.url) : null; const model = id ? (useDatabase.getState().completion[id] ?? null) : null; if (id && !model) void loadCompletion(id); // Unqualified names resolve in the console's active schema (IDEA's schema dropdown). @@ -1091,6 +1095,7 @@ export function completionSource(path: string, sql: SqlText = WHOLE_FILE): Compl model: scoped, analysis, driver, + dialect, sourceName: source?.name ?? undefined, catalog: cataloged ? catalogOf(id) : undefined, }); diff --git a/src/features/database/sql/complete.ts b/src/features/database/sql/complete.ts index 81848ad43..d1ac22519 100644 --- a/src/features/database/sql/complete.ts +++ b/src/features/database/sql/complete.ts @@ -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(); for (const t of tables) { @@ -472,13 +487,14 @@ function joinCandidates( a: Analysis, model: DbCompletion | null, driver: DbDriver, + dialect: DbDriver | null | undefined, taken: Set, 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(); const local = (s: string) => eq(s, model.defaultSchema) || driver === "sqlite"; const tableText = (schema: string, name: string) => (local(schema) ? q(name) : `${q(schema)}.${q(name)}`); diff --git a/src/features/database/sql/sql.test.ts b/src/features/database/sql/sql.test.ts index ce2cae6f0..b1575515a 100644 --- a/src/features/database/sql/sql.test.ts +++ b/src/features/database/sql/sql.test.ts @@ -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([ diff --git a/src/features/database/vendors.ts b/src/features/database/vendors.ts index 2e91e20bd..30d4165a2 100644 --- a/src/features/database/vendors.ts +++ b/src/features/database/vendors.ts @@ -176,6 +176,15 @@ const SCHEMES: Record = { export const vendorOfScheme = (scheme: string): DbVendor | undefined => BY_ID.get(SCHEMES[scheme.toLowerCase()] ?? ""); +/** + * The dialect a custom JDBC source's URL speaks (`jdbc:oracle:thin:@//db:1521/ORCL` → `oracle`): + * its `driver: "jdbc"` says nothing about how the server folds identifiers. + */ +export function jdbcDialect(url: string | null | undefined): DbDriver | null { + const scheme = /^(?:jdbc:)?([a-z][a-z0-9]*):/i.exec((url ?? "").trim())?.[1] ?? ""; + return vendorOfScheme(scheme)?.driver ?? null; +} + const bracketed = (host: string) => (host.includes(":") && !host.startsWith("[") ? `[${host}]` : host); /** The JDBC URL of a data source's fields (the General tab's URL preview). */