fix(editor): support qualified table navigation
This commit is contained in:
3 files changed
+149
-53
No files matched your search
@@ -1,5 +1,33 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { matchTable } from "@/lib/sqlNavigation";
|
||||
import { extractIdentifierAt, matchTable, splitQualifiedIdentifier } from "@/lib/sqlNavigation";
|
||||
|
||||
describe("extractIdentifierAt", () => {
|
||||
it("extracts unquoted qualified identifiers", () => {
|
||||
const sql = "select * from MAAC00.Accounts";
|
||||
|
||||
expect(extractIdentifierAt(sql, sql.indexOf("Accounts"))).toBe("MAAC00.Accounts");
|
||||
});
|
||||
|
||||
it("extracts backtick-quoted qualified identifiers", () => {
|
||||
const sql = "select * from `MAAC00`.Accounts";
|
||||
|
||||
expect(extractIdentifierAt(sql, sql.indexOf("Accounts"))).toBe("MAAC00.Accounts");
|
||||
expect(extractIdentifierAt(sql, sql.indexOf("MAAC00"))).toBe("MAAC00.Accounts");
|
||||
});
|
||||
|
||||
it("extracts double-quoted qualified identifiers", () => {
|
||||
const sql = 'select * from "MAAC00"."Accounts"';
|
||||
|
||||
expect(extractIdentifierAt(sql, sql.indexOf("Accounts"))).toBe("MAAC00.Accounts");
|
||||
});
|
||||
});
|
||||
|
||||
describe("splitQualifiedIdentifier", () => {
|
||||
it("splits quoted and multi-part identifiers", () => {
|
||||
expect(splitQualifiedIdentifier('catalog."MAAC00".Accounts')).toEqual(["catalog", "MAAC00", "Accounts"]);
|
||||
expect(splitQualifiedIdentifier("`MAAC00`.Accounts")).toEqual(["MAAC00", "Accounts"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("matchTable", () => {
|
||||
it("matches schema-qualified table identifiers", () => {
|
||||
@@ -8,6 +36,18 @@ describe("matchTable", () => {
|
||||
expect(matchTable("maac00.accounts", [table])).toBe(table);
|
||||
});
|
||||
|
||||
it("matches catalog.schema.table identifiers against schema-scoped tables", () => {
|
||||
const table = { schema: "MAAC00", name: "Accounts" };
|
||||
|
||||
expect(matchTable("catalog.maac00.accounts", [table])).toBe(table);
|
||||
});
|
||||
|
||||
it("matches quoted schema-qualified table identifiers", () => {
|
||||
const table = { schema: "MAAC00", name: "Accounts" };
|
||||
|
||||
expect(matchTable("`MAAC00`.Accounts", [table])).toBe(table);
|
||||
});
|
||||
|
||||
it("does not treat non-schema qualifiers as table matches", () => {
|
||||
expect(matchTable("u.users", [{ schema: "public", name: "users" }])).toBeNull();
|
||||
});
|
||||
|
||||
@@ -121,42 +121,92 @@ const SQL_KEYWORDS_SET = new Set([
|
||||
"list_transform",
|
||||
]);
|
||||
|
||||
type IdentifierPart = { value: string; start: number; end: number };
|
||||
|
||||
function isIdentifierChar(char: string | undefined): boolean {
|
||||
return !!char && /^[A-Za-z0-9_$]$/.test(char);
|
||||
}
|
||||
|
||||
function readQuotedPart(text: string, start: number): IdentifierPart | null {
|
||||
const open = text[start];
|
||||
const close = open === "[" ? "]" : open;
|
||||
if (open !== "`" && open !== '"' && open !== "[") return null;
|
||||
|
||||
let value = "";
|
||||
for (let i = start + 1; i < text.length; i += 1) {
|
||||
const char = text[i];
|
||||
if (char === close) {
|
||||
if (text[i + 1] === close) {
|
||||
value += close;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
return { value, start, end: i + 1 };
|
||||
}
|
||||
value += char;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function readUnquotedPart(text: string, start: number): IdentifierPart | null {
|
||||
if (!isIdentifierChar(text[start])) return null;
|
||||
let end = start + 1;
|
||||
while (end < text.length && isIdentifierChar(text[end])) end += 1;
|
||||
return { value: text.slice(start, end), start, end };
|
||||
}
|
||||
|
||||
function readIdentifierPart(text: string, start: number): IdentifierPart | null {
|
||||
return readQuotedPart(text, start) ?? readUnquotedPart(text, start);
|
||||
}
|
||||
|
||||
function parseQualifiedIdentifier(text: string, start: number): { parts: IdentifierPart[]; start: number; end: number } | null {
|
||||
const first = readIdentifierPart(text, start);
|
||||
if (!first) return null;
|
||||
|
||||
const parts = [first];
|
||||
let end = first.end;
|
||||
while (text[end] === ".") {
|
||||
const next = readIdentifierPart(text, end + 1);
|
||||
if (!next) break;
|
||||
parts.push(next);
|
||||
end = next.end;
|
||||
}
|
||||
|
||||
return { parts, start, end };
|
||||
}
|
||||
|
||||
function identifierSearchBounds(doc: string, pos: number): { start: number; end: number } {
|
||||
let start = pos;
|
||||
while (start > 0 && doc[start - 1] !== "\n" && doc[start - 1] !== "\r") start -= 1;
|
||||
|
||||
let end = pos;
|
||||
while (end < doc.length && doc[end] !== "\n" && doc[end] !== "\r") end += 1;
|
||||
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
/** Extract identifier at position `pos` in the document. */
|
||||
export function extractIdentifierAt(doc: string, pos: number): string | null {
|
||||
if (pos < 0 || pos > doc.length) return null;
|
||||
|
||||
const char = doc[pos];
|
||||
const idChar = (c: string) => /^[A-Za-z0-9_]$/.test(c);
|
||||
const clickPos = pos === doc.length ? pos - 1 : pos;
|
||||
if (clickPos < 0) return null;
|
||||
|
||||
// Backtick-quoted: `identifier`
|
||||
if (char === "`") {
|
||||
let start = pos;
|
||||
while (start > 0 && doc[start - 1] === "`") start--;
|
||||
const end = doc.indexOf("`", start + 1);
|
||||
if (end < 0) return null;
|
||||
return doc.slice(start + 1, end);
|
||||
const bounds = identifierSearchBounds(doc, clickPos);
|
||||
let index = bounds.start;
|
||||
while (index < bounds.end) {
|
||||
const parsed = parseQualifiedIdentifier(doc, index);
|
||||
if (parsed) {
|
||||
if (clickPos >= parsed.start && clickPos < parsed.end) {
|
||||
return parsed.parts.map((part) => part.value).join(".");
|
||||
}
|
||||
index = Math.max(parsed.end, index + 1);
|
||||
continue;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
|
||||
// Double-quoted: "identifier"
|
||||
if (char === '"') {
|
||||
let start = pos;
|
||||
while (start > 0 && doc[start - 1] === '"') start--;
|
||||
const end = doc.indexOf('"', start + 1);
|
||||
if (end < 0) return null;
|
||||
return doc.slice(start + 1, end);
|
||||
}
|
||||
|
||||
// Unquoted identifier (may be qualified like schema.table)
|
||||
if (!idChar(char) && char !== ".") return null;
|
||||
|
||||
let start = pos;
|
||||
while (start > 0 && (idChar(doc[start - 1]) || doc[start - 1] === ".")) start--;
|
||||
let end = pos;
|
||||
while (end < doc.length && (idChar(doc[end]) || doc[end] === ".")) end++;
|
||||
|
||||
const result = doc.slice(start, end);
|
||||
if (!/[A-Za-z0-9_]/.test(result)) return null;
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Check whether the identifier is a SQL keyword (not a table/column name). */
|
||||
@@ -164,19 +214,27 @@ export function isSqlKeyword(identifier: string): boolean {
|
||||
return SQL_KEYWORDS_SET.has(identifier.toLowerCase());
|
||||
}
|
||||
|
||||
export function splitQualifiedIdentifier(identifier: string): string[] {
|
||||
const trimmed = identifier.trim();
|
||||
if (!trimmed) return [];
|
||||
|
||||
const parsed = parseQualifiedIdentifier(trimmed, 0);
|
||||
if (!parsed || parsed.end !== trimmed.length) return [trimmed];
|
||||
return parsed.parts.map((part) => part.value);
|
||||
}
|
||||
|
||||
/** Match identifier against known table names (case-insensitive). Supports qualified identifiers like schema.table. */
|
||||
export function matchTable(identifier: string, tables: Array<{ name: string; schema?: string }>): { name: string; schema?: string } | null {
|
||||
const lower = identifier.toLowerCase();
|
||||
const parts = splitQualifiedIdentifier(identifier);
|
||||
const normalizedIdentifier = parts.length > 0 ? parts.join(".").toLowerCase() : identifier.toLowerCase();
|
||||
|
||||
// Direct match (simple table name)
|
||||
const direct = tables.find((t) => t.name.toLowerCase() === lower);
|
||||
const direct = tables.find((t) => t.name.toLowerCase() === normalizedIdentifier);
|
||||
if (direct) return direct;
|
||||
|
||||
// Qualified match: schema.table
|
||||
const dotIndex = identifier.indexOf(".");
|
||||
if (dotIndex > 0) {
|
||||
const qualifier = identifier.substring(0, dotIndex).toLowerCase();
|
||||
const name = identifier.substring(dotIndex + 1).toLowerCase();
|
||||
if (parts.length >= 2) {
|
||||
// Use the final two parts so catalog.schema.table still resolves against schema-scoped metadata.
|
||||
const qualifier = parts[parts.length - 2].toLowerCase();
|
||||
const name = parts[parts.length - 1].toLowerCase();
|
||||
const qualified = tables.find((t) => t.name.toLowerCase() === name && t.schema?.toLowerCase() === qualifier);
|
||||
if (qualified) return qualified;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user