feat(editor): support SQL Server cross-database completion

This commit is contained in:
zipg authored and GitHub committed 2026-07-22 15:08:44 +08:00
1 parent dfab2d09ed
commit 63dd0e88b5
19 files changed
+438 -75

No files matched your search

@@ -18,6 +18,7 @@ export function sqlSemanticReferencedTables(model: SqlSemanticModel): SqlComplet
.filter((source) => source.kind !== "unknown")
.map((source) => ({
name: source.name,
database: source.metadataTarget?.database,
schema: source.qualifierParts[source.qualifierParts.length - 1],
alias: source.alias,
columns: source.columns,
@@ -180,6 +181,7 @@ export function sqlCompletionContextFromSemantic(model: SqlSemanticModel, base:
const qualifier = model.cursorIntent.qualifierParts.length > 0 ? model.cursorIntent.qualifierParts.join(".") : undefined;
const referencedTables = sqlSemanticReferencedTables(model);
const mutationTarget = semanticMutationTarget(model);
const mutationDatabase = mutationTarget?.metadataTarget?.database;
const mutationSchema = mutationTarget?.qualifierParts[mutationTarget.qualifierParts.length - 1];
const suggestTables = scope.kind === "table" || scope.kind === "schema" || scope.kind === "catalog";
const suggestColumns = scope.kind === "columns";
@@ -203,6 +205,7 @@ export function sqlCompletionContextFromSemantic(model: SqlSemanticModel, base:
selectAliases: projectionAliases.length > 0 ? projectionAliases : base.selectAliases,
referencedTables: referencedTables.length > 0 ? referencedTables : base.referencedTables,
insertTable: model.cursorIntent.kind === "insert_column" ? mutationTarget?.name : base.insertTable,
insertDatabase: model.cursorIntent.kind === "insert_column" ? mutationDatabase : base.insertDatabase,
insertSchema: model.cursorIntent.kind === "insert_column" ? mutationSchema : base.insertSchema,
updateTarget: model.cursorIntent.kind === "update_column" && mutationTarget ? { table: mutationTarget.name, schema: mutationSchema } : base.updateTarget,
deleteTarget: model.cursorIntent.kind === "delete_target" && mutationTarget ? { table: mutationTarget.name, schema: mutationSchema } : base.deleteTarget,
@@ -255,6 +255,7 @@ function tableLookupFor(tables: SqlTableReference[]): Map<string, SqlTableRefere
lookup.set(normalizeName(table.name), table);
if (table.alias) lookup.set(normalizeName(table.alias), table);
if (table.schema) lookup.set(normalizeName(`${table.schema}.${table.name}`), table);
if (table.database && table.schema) lookup.set(normalizeName(`${table.database}.${table.schema}.${table.name}`), table);
}
return lookup;
}
@@ -301,7 +302,7 @@ function columnsForTable(table: SqlTableReference, columnsByTable: Map<string, S
schema: table.schema ?? undefined,
}));
}
const keys = table.schema ? [`${table.schema}.${table.name}`, table.name] : [table.name, ...keysWithTableName(columnsByTable, table.name)];
const keys = table.schema ? [table.database ? `${table.database}.${table.schema}.${table.name}` : undefined, `${table.schema}.${table.name}`, table.name].filter((key): key is string => !!key) : [table.name, ...keysWithTableName(columnsByTable, table.name)];
for (const key of keys) {
const normalizedKey = normalizeName(key);
const columns = columnsByTable.get(key) ?? columnsByTable.get(normalizedKey);
@@ -321,12 +322,12 @@ function rangesIntersect(left: SqlSemanticDiagnosticVisibleRange, right: SqlSema
return left.from < right.to && right.from < left.to;
}
export function tableReferenceKey(table: Pick<SqlTableReference, "name" | "schema">): string {
return normalizeName(table.schema ? `${table.schema}.${table.name}` : table.name);
export function tableReferenceKey(table: Pick<SqlTableReference, "name" | "database" | "schema">): string {
return normalizeName(table.schema ? `${table.database ? `${table.database}.` : ""}${table.schema}.${table.name}` : table.name);
}
function displayTableName(table: SqlTableReference): string {
return table.schema ? `${table.schema}.${table.name}` : table.name;
return table.schema ? `${table.database ? `${table.database}.` : ""}${table.schema}.${table.name}` : table.name;
}
function isCursorAfterTableTrigger(sql: string, cursor: number): boolean {
+6 -1
View File
@@ -409,6 +409,7 @@ function parseTableSource(state: ParseState, nameIndex: number, introducer: stri
columns: cte?.columns ? mergeColumnAliases(cte.columns, alias.columns) : undefined,
columnAliases: alias.columns,
metadataTarget: {
database: qualifierParts.length >= 2 ? qualifierParts[qualifierParts.length - 2] : undefined,
schema: qualifierParts[qualifierParts.length - 1],
table: name,
},
@@ -640,7 +641,11 @@ function buildCursorIntent(tokens: readonly SqlSemanticToken[], cursor: number,
if (
trailing.qualifierParts.length > 0 &&
(previous === "from" || previous === "join" || TABLE_INTRODUCERS.has(previous) || TABLE_INTRODUCERS.has(wordBeforeReplacement) || (!!targetSource && !targetSource.alias && TABLE_INTRODUCERS.has(wordBeforePosition(tokens, targetSource.sourceSpan.start))))
(previous === "from" ||
previous === "join" ||
TABLE_INTRODUCERS.has(previous) ||
TABLE_INTRODUCERS.has(wordBeforeReplacement) ||
(!!targetSource && !targetSource.alias && trailing.replacementRange.start <= targetSource.sourceSpan.end + 1 && TABLE_INTRODUCERS.has(wordBeforePosition(tokens, targetSource.sourceSpan.start))))
) {
const role = dialect.qualifierRole(trailing.qualifierParts, "table");
return { kind: role === "catalog" ? "catalog" : "table", prefix: trailing.prefix, replacementRange: trailing.replacementRange, qualifierParts: trailing.qualifierParts, expectedObjectKinds: ["table", "view"], confidence: "medium" };
@@ -10,6 +10,7 @@ export type SqlSemanticTableReference = SqlTableReference & {
export interface SqlSemanticNavigationTarget {
name: string;
database?: string;
schema?: string;
alias?: string;
columns?: string[];
@@ -51,12 +52,16 @@ function sourceSchema(source: SqlSemanticRowSource): string | undefined {
return source.metadataTarget?.schema ?? source.qualifierParts[source.qualifierParts.length - 1];
}
function sourceDatabase(source: SqlSemanticRowSource): string | undefined {
return source.metadataTarget?.database;
}
function sourceTableName(source: SqlSemanticRowSource): string {
return source.metadataTarget?.table ?? source.name;
}
function sourceReferenceKey(source: SqlSemanticTableReference): string {
return [normalized(source.schema), normalized(source.name), normalized(source.alias), source.span.start_line, source.span.start_column].join(":");
return [normalized(source.database), normalized(source.schema), normalized(source.name), normalized(source.alias), source.span.start_line, source.span.start_column].join(":");
}
function existingScopeId(analysis: SqlReferenceAnalysis): number {
@@ -72,6 +77,7 @@ export function sqlSemanticTableReferences(model: SqlSemanticModel, scopeId = 0)
const schema = sourceSchema(source);
return {
name: sourceTableName(source),
database: sourceDatabase(source),
schema,
alias: source.alias,
span: semanticSpanToSqlTextSpan(model.sql, span),
@@ -95,11 +101,13 @@ export function mergeSqlSemanticReferenceAnalysis(analysis: SqlReferenceAnalysis
for (const table of semanticTables) {
const existing = [...merged.values()].find((candidate) => {
if (normalized(candidate.name) !== normalized(table.name)) return false;
if (candidate.database && table.database && normalized(candidate.database) !== normalized(table.database)) return false;
if (candidate.alias && table.alias && normalized(candidate.alias) !== normalized(table.alias)) return false;
if (candidate.schema && table.schema && normalized(candidate.schema) !== normalized(table.schema)) return false;
return true;
});
if (existing) {
existing.database = existing.database ?? table.database;
existing.schema = existing.schema ?? table.schema;
existing.alias = existing.alias ?? table.alias;
existing.columns = existing.columns ?? table.columns;
@@ -118,6 +126,7 @@ export function mergeSqlSemanticReferenceAnalysis(analysis: SqlReferenceAnalysis
export function sqlSemanticCompletionReferenceTables(model: SqlSemanticModel): SqlCompletionReferencedTable[] {
return sqlSemanticTableReferences(model).map((table) => ({
name: table.name,
database: table.database ?? undefined,
schema: table.schema ?? undefined,
alias: table.alias ?? undefined,
columns: table.columns,
@@ -129,15 +138,17 @@ export function resolveSqlSemanticNavigationTarget(model: SqlSemanticModel, iden
const normalizedParts = identifierParts.map(normalized).filter(Boolean);
const name = normalizedParts[normalizedParts.length - 1];
const qualifier = normalizedParts.length > 1 ? normalizedParts[normalizedParts.length - 2] : undefined;
const database = normalizedParts.length > 2 ? normalizedParts[normalizedParts.length - 3] : undefined;
const source =
model.rowSources.find((candidate) => {
const candidateName = normalized(sourceTableName(candidate));
const candidateAlias = normalized(candidate.alias);
const candidateDatabase = normalized(sourceDatabase(candidate));
const candidateSchema = normalized(sourceSchema(candidate));
if (qualifier) {
if (candidateAlias && candidateAlias === qualifier) return true;
return candidateName === name && (!candidateSchema || candidateSchema === qualifier);
if (!database && candidateAlias && candidateAlias === qualifier) return true;
return candidateName === name && (!candidateSchema || candidateSchema === qualifier) && (!database || !candidateDatabase || candidateDatabase === database);
}
return candidateName === name || candidateAlias === name;
}) ?? null;
@@ -146,6 +157,7 @@ export function resolveSqlSemanticNavigationTarget(model: SqlSemanticModel, iden
const schema = sourceSchema(source);
return {
name: sourceTableName(source),
database: sourceDatabase(source),
schema,
alias: source.alias,
columns: source.columns,