Skip to content

Commit

Permalink
Add schema autocomplete suggestions and remove schema from table comp…
Browse files Browse the repository at this point in the history
…letion (#7)
  • Loading branch information
smaspe authored Jan 12, 2021
1 parent d112554 commit b2ac010
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/ls/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ export default class YourDriverClass extends AbstractDriver<DriverLib, DriverOpt
*/
public async searchItems(itemType: ContextValue, search: string, _extraParams: any = {}): Promise<NSDatabase.SearchableItem[]> {
switch (itemType) {
case ContextValue.DATABASE:
return this.cachedQuery(this.queries.searchSchemas({ search }));
case ContextValue.TABLE:
case ContextValue.VIEW:
return this.cachedQuery(this.queries.searchTables({ search, schema: _extraParams.database }));
if (_extraParams.database) {
return this.cachedQuery(this.queries.searchTables({ search, schema: _extraParams.database }));
}
case ContextValue.COLUMN:
if (_extraParams.tables && _extraParams.tables.length > 0) {
return this.cachedQuery(this.queries.searchColumns({ search, tables: _extraParams.tables || [] }));
Expand Down
3 changes: 2 additions & 1 deletion src/ls/keywords.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NSDatabase } from '@sqltools/types';
import { CompletionItem } from 'vscode-languageserver-types';

// From the EXA_SQL_KEYWORDS table
const keywordsArr = [
Expand Down Expand Up @@ -816,7 +817,7 @@ const keywordsArr = [
const firstKeywords = ['SELECT', 'CREATE', 'UPDATE', 'DELETE', 'WITH'];

const keywordsCompletion: { [w: string]: NSDatabase.IStaticCompletion } = keywordsArr.reduce((agg, word) => {
agg[`"${word}"`] = {
agg[`"${word}"`] = <CompletionItem>{
label: word,
detail: word,
filterText: word,
Expand Down
20 changes: 17 additions & 3 deletions src/ls/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBaseQueries, ContextValue } from '@sqltools/types';
import { IBaseQueries, ContextValue, QueryBuilder, NSDatabase } from '@sqltools/types';
import queryFactory from '@sqltools/base-driver/dist/lib/factory';


Expand Down Expand Up @@ -84,27 +84,40 @@ order by
`;

// Search is used for autocomplete. In the context of Tables, Views are also relevant.
const searchSchemas: QueryBuilder<{ search: string }, NSDatabase.ISchema> = queryFactory`
${SNAPSHOT_EXEC} select
"SCHEMA_NAME" as 'label',
'${ContextValue.SCHEMA}' as 'type',
true as 'isView'
from
"EXA_ALL_SCHEMAS"
where
lower("SCHEMA_NAME") like '%${p => p.search.toLowerCase()}%'
`

const searchTables: IBaseQueries['searchTables'] = queryFactory`
${SNAPSHOT_EXEC} select
"VIEW_SCHEMA" || '.' || "VIEW_NAME" as 'label',
"VIEW_NAME" as 'label',
"VIEW_SCHEMA" as 'schema',
'${ContextValue.VIEW}' as 'type',
true as 'isView'
from
"EXA_ALL_VIEWS"
where
"VIEW_SCHEMA" = '${p => p.schema}' and
lower("VIEW_NAME") like '%${p => p.search.toLowerCase()}%'
union all
select
"TABLE_SCHEMA" || '.' || "TABLE_NAME" as 'label',
"TABLE_NAME" as 'label',
"TABLE_SCHEMA" as 'schema',
'${ContextValue.TABLE}' as 'type',
false as 'isView'
from
"EXA_ALL_TABLES"
where
"TABLE_SCHEMA" = '${p => p.schema}' and
lower("TABLE_NAME") like '%${p => p.search.toLowerCase()}%'
`;
Expand Down Expand Up @@ -137,6 +150,7 @@ export default {
fetchTables,
fetchViews,
searchTables,
searchSchemas,
searchColumns,
// Unused but required by the interface
describeTable, // Undef
Expand Down

0 comments on commit b2ac010

Please sign in to comment.