feat: support schema-qualified table completion in SQL editor

When typing `schema.` in the SQL editor for schema-aware databases
(PostgreSQL, GaussDB, etc.), auto-complete now suggests tables from
that schema. Falls back to schema lookup when the qualifier doesn't
match any table name.

Closes #239
This commit is contained in:
t8y2 2026-05-13 13:09:22 +08:00
parent 24b39bd853
commit 48785162e3
2 changed files with 25 additions and 4 deletions

View File

@ -128,7 +128,7 @@ async function provideSqlCompletions(currentState: import("@codemirror/state").E
const fullDoc = currentState.doc.toString();
const completionContext = getSqlCompletionContext(fullDoc, position);
const shouldLoadTables = completionContext.suggestTables || !!completionContext.qualifier;
const tables = shouldLoadTables
let tables = shouldLoadTables
? await connectionStore.listCompletionTables(
props.connectionId,
props.database,
@ -137,6 +137,22 @@ async function provideSqlCompletions(currentState: import("@codemirror/state").E
)
: cachedTables;
// If qualifier didn't match any table names, try it as a schema name
let qualifierIsSchema = false;
if (completionContext.qualifier && completionContext.suggestTables && tables.length === 0) {
const schemaTables = await connectionStore.listCompletionTables(
props.connectionId,
props.database,
completionContext.prefix,
MAX_COMPLETION_TABLES,
completionContext.qualifier,
);
if (schemaTables.length > 0) {
tables = schemaTables;
qualifierIsSchema = true;
}
}
// Collect referenced tables enrich with schema from filtered table lookup
let refs = completionContext.referencedTables.map((rt) => {
// If no schema, look it up in the cached tables
@ -200,7 +216,11 @@ async function provideSqlCompletions(currentState: import("@codemirror/state").E
}
}
const items = buildSqlCompletionItemsFromContext(completionContext, {
const effectiveContext = qualifierIsSchema
? { ...completionContext, qualifier: undefined, suggestTables: true, suggestColumns: false }
: completionContext;
const items = buildSqlCompletionItemsFromContext(effectiveContext, {
tables,
columnsByTable,
});

View File

@ -995,9 +995,10 @@ export const useConnectionStore = defineStore("connection", () => {
database: string,
filter = "",
limit?: number,
schema?: string,
): Promise<SqlCompletionTable[]> {
const normalizedFilter = filter.trim().toLowerCase();
const cacheKey = `${connectionId}:${database}:${normalizedFilter}:${limit ?? ""}`;
const cacheKey = `${connectionId}:${database}:${normalizedFilter}:${limit ?? ""}:${schema ?? ""}`;
if (completionTablesCache.value[cacheKey]) {
return completionTablesCache.value[cacheKey];
}
@ -1005,7 +1006,7 @@ export const useConnectionStore = defineStore("connection", () => {
await ensureConnected(connectionId);
if (isSchemaAwareDatabase(connectionId)) {
const schemas = await api.listSchemas(connectionId, database);
const schemas = schema ? [schema] : await api.listSchemas(connectionId, database);
if (normalizedFilter || limit) {
const limitedTables: SqlCompletionTable[] = [];
for (const schema of schemas) {