Skip to content

Commit 458ffd7

Browse files
authored
Merge pull request #31 from isc-tleavitt/master
Fix intellisense for table names
2 parents d4fb515 + 45bff9c commit 458ffd7

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/ls/driver.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,23 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
148148
*/
149149
public async searchItems(itemType: ContextValue, search: string, extraParams: any = {}): Promise<NSDatabase.SearchableItem[]> {
150150
switch (itemType) {
151+
case ContextValue.DATABASE:
152+
// Syntatically, a schema in IRIS SQL resembles a database in other databases.
153+
// That's the simplest way to adapt IRIS SQL to the generic Hue parser vscode-sqltools uses.
154+
return this.queryResults(this.queries.searchEverything({ search, showSystem: this.showSystem }));
151155
case ContextValue.TABLE:
152156
case ContextValue.FUNCTION:
153157
case ContextValue.VIEW:
154-
return this.queryResults(this.queries.searchEverything({ search, showSystem: this.showSystem })).then(r => r.map(t => {
155-
t.isView = toBool(t.isView);
156-
return t;
157-
}));
158+
const searchParams = { search, showSystem: this.showSystem, itemType, ...extraParams };
159+
if (extraParams['database']) {
160+
searchParams['schema'] = extraParams['database'];
161+
}
162+
return this.queryResults(this.queries.searchEverything(searchParams)).then(r => r
163+
.filter(r => r.type === itemType)
164+
.map(t => {
165+
t.isView = toBool(t.isView);
166+
return t;
167+
}));
158168
case ContextValue.COLUMN:
159169
return this.queryResults(this.queries.searchColumns({ search, ...extraParams }));
160170
}

src/ls/queries.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,36 @@ WHERE 1 = 1
6161
${
6262
p => p.search
6363
? `AND (
64-
LOWER(T.name || '.' || C.name) LIKE '%${p.search.toLowerCase()}%'
65-
OR LOWER(C.name) LIKE '%${p.search.toLowerCase()}%'
64+
LOWER(TABLE_NAME || '.' || COLUMN_NAME) LIKE '%${p.search.toLowerCase()}%'
65+
OR LOWER(COLUMN_NAME) LIKE '%${p.search.toLowerCase()}%'
6666
)`
6767
: ''
6868
}
69+
${
70+
p => p.tables ? 'AND (' +
71+
p.tables.map(t => `TABLE_NAME = '${t.label}' and TABLE_SCHEMA = '${t.database}'`).join(' OR ')
72+
+ ")" : ''
73+
}
6974
ORDER BY
7075
TABLE_NAME,
7176
ORDINAL_POSITION
7277
`;
7378

79+
const treeFunctionFilter = function(p: { [key: string]: any }): string {
80+
if (p.schema || p.search) {
81+
let filter = ", '";
82+
if (p.schema) {
83+
filter += p.schema + ".";
84+
}
85+
if (p.search) {
86+
filter += p.search;
87+
}
88+
filter += "*'";
89+
return filter;
90+
}
91+
return "";
92+
}
93+
7494
const fetchRecords: IQueries['fetchRecords'] = queryFactory`
7595
SELECT TOP ${p => p.limit || 50} *
7696
FROM ${p => p.table.schema}.${p => (p.table.label || p.table)}
@@ -100,7 +120,7 @@ SELECT
100120
'0:' || SCHEMA_NAME AS sortText
101121
`
102122
}
103-
FROM ${Functions[type]}(${p => p.showSystem ? 1 : 0}${p => p.search ? `, '*${p.search}*'` : p.schema ? `, '${p.schema}.*'` : ''})
123+
FROM ${Functions[type]}(${p => p.showSystem ? 1 : 0}${p => treeFunctionFilter(p)})
104124
ORDER BY
105125
${p => p.schema || p.search && p.search.includes('.') ? ValueColumn[type] : 'SCHEMA_NAME'}
106126
`;
@@ -127,15 +147,15 @@ SELECT
127147
'0:' || SCHEMA_NAME AS sortText
128148
`
129149
}
130-
FROM ${Functions[type]}(${p.showSystem ? 1 : 0}${p.search ? `, '${p.search}*'` : p.schema ? `, '${p.schema}.*'` : ''})
150+
FROM ${Functions[type]}(${p.showSystem ? 1 : 0}${treeFunctionFilter(p)})
131151
`;
132152

133153
const searchTables: IQueries['searchTables'] = queryFactory`
134154
${p => searchHelper(p, ContextValue.TABLE)}
135155
ORDER BY sortText
136156
`;
137157

138-
const searchEverything: IQueries['searchTables'] = queryFactory`
158+
const searchEverything: IQueries['searchEverything'] = queryFactory`
139159
${p => searchHelper(p, ContextValue.TABLE)}
140160
UNION
141161
${p => searchHelper(p, ContextValue.VIEW)}

0 commit comments

Comments
 (0)