Skip to content

Commit 072c254

Browse files
committed
Suggest catalogs, database and tables one-by-one, hierarchically
So instead of showing `catalog1.schema1.table1` all at once after FROM, we'll only show catalog1, and then after a dot, we'll suggest schema1, and only when we have `SELECT * FROM catalog1.schema1.` will be suggest table1. It's a total hack, putting it all in the `createTableCandidates` function, but it works and it's the fastest way to implement this.
1 parent 2103aac commit 072c254

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

packages/server/src/complete/candidates/createTableCandidates.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,54 @@ import { ICONS } from '../CompletionItemUtils'
1010
* @returns
1111
*/
1212
function allTableNameCombinations(table: Table): string[] {
13-
const names = [table.tableName]
14-
if (table.database) names.push(table.database + '.' + table.tableName)
15-
if (table.catalog)
16-
names.push(table.catalog + '.' + table.database + '.' + table.tableName)
17-
return names
13+
if (table.catalog) {
14+
return [table.catalog + '.' + table.database + '.' + table.tableName]
15+
}
16+
if (table.database) {
17+
return [table.database + '.' + table.tableName]
18+
}
19+
return [table.tableName]
1820
}
1921

2022
export function createTableCandidates(
2123
tables: Table[],
2224
lastToken: string,
2325
onFromClause?: boolean
2426
) {
25-
return tables
26-
.flatMap((table) => allTableNameCombinations(table))
27+
const qualificationLevel = lastToken.split('.').length - 1
28+
29+
const qualifiedEntities = tables.flatMap((table) => {
30+
let qualificationNeeded = 0
31+
if (table.catalog) {
32+
qualificationNeeded++
33+
}
34+
if (table.database) {
35+
qualificationNeeded++
36+
}
37+
const qualificationLevelNeeded = qualificationNeeded - qualificationLevel
38+
switch (qualificationLevelNeeded) {
39+
case 0:
40+
return allTableNameCombinations(table)
41+
case 1:
42+
if (table.catalog && table.database) {
43+
return [table.catalog + '.' + table.database]
44+
}
45+
if (table.database) {
46+
return [table.database]
47+
}
48+
break
49+
case 2:
50+
if (table.catalog) {
51+
return [table.catalog]
52+
}
53+
break
54+
}
55+
return []
56+
})
57+
58+
const uniqueEntities = [...new Set(qualifiedEntities)]
59+
60+
return uniqueEntities
2761
.map((aTableNameVariant) => {
2862
return new Identifier(
2963
lastToken,

0 commit comments

Comments
 (0)