Skip to content

Commit

Permalink
Add query autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
fzhem committed Sep 13, 2024
1 parent d7e1b8e commit d159130
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Changelog

### 0.0.5

- Add query autocompletion

### 0.0.4

- Add query to fetch views in side panel
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sqltools-firebird-driver",
"displayName": "SQLTools Firebird",
"description": "SQLTools Firebird",
"version": "0.0.4",
"version": "0.0.5",
"engines": {
"vscode": "^1.72.0"
},
Expand Down
11 changes: 11 additions & 0 deletions src/ls/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,15 @@ export default class FirebirdSQL
table: parent,
}));
}

public searchItems(itemType: ContextValue, search: string, extraParams: any = {}) {
switch (itemType) {
case ContextValue.TABLE:
console.log('hereeee')
return this.queryResults(this.queries.searchTables({ search }));
case ContextValue.COLUMN:
console.log('hereeee.......')
return this.queryResults(this.queries.searchColumns({ search, ...extraParams }));
}
}
}
106 changes: 103 additions & 3 deletions src/ls/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SELECT DISTINCT TRIM(RDB$OWNER_NAME) AS "label"
, TRIM('schema') AS "detail"
, TRIM('group-by-ref-type') AS "iconId"
FROM RDB$RELATIONS
WHERE RDB$SYSTEM_FLAG = 0
WHERE (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)
`;

export const fetchTables: IBaseQueries['fetchTables'] = queryFactory`
Expand All @@ -24,7 +24,7 @@ export const fetchViews: IBaseQueries['fetchTables'] = queryFactory`
SELECT TRIM(RDB$RELATION_NAME) AS "label"
, TRIM('${ContextValue.VIEW}') AS "type"
FROM RDB$RELATIONS
WHERE RDB$SYSTEM_FLAG = 0
WHERE (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)
AND RDB$VIEW_BLR is not null
AND RDB$OWNER_NAME = '${p => p.schema}'
`;
Expand Down Expand Up @@ -110,4 +110,104 @@ SELECT TRIM(rf.RDB$FIELD_NAME) AS "label"
FROM RDB$RELATION_FIELDS rf
JOIN RDB$FIELDS f ON rf.RDB$FIELD_SOURCE = f.RDB$FIELD_NAME
WHERE rf.RDB$RELATION_NAME = '${p => p.label}'
`
`;

export const searchTables: IBaseQueries['searchTables'] = queryFactory`
SELECT TRIM(RDB$RELATION_NAME) AS "label"
, TRIM(CASE WHEN RDB$VIEW_BLR IS NULL THEN '${ContextValue.TABLE}' ELSE '${ContextValue.VIEW}' END) AS "type"
, TRIM(RDB$OWNER_NAME) AS "schema"
, CASE WHEN RDB$VIEW_BLR IS NULL THEN 0 ELSE 1 END AS "isView"
, TRIM(CASE WHEN RDB$VIEW_BLR IS NULL THEN 'table' ELSE 'view' END) AS "description"
FROM RDB$RELATIONS
WHERE (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)
${p => p.search ? `AND TRIM(LOWER(RDB$RELATION_NAME)) LIKE '%${p.search.toLowerCase()}%'` : ''}
`;

export const searchColumns: IBaseQueries['searchColumns'] = queryFactory`
SELECT TRIM(rf.RDB$FIELD_NAME) AS "label"
, TRIM('${ContextValue.COLUMN}') AS "type"
, TRIM('${p => p.label}') AS "table"
, TRIM(CASE f.RDB$FIELD_TYPE
WHEN 7 THEN 'SMALLINT'
WHEN 8 THEN 'INTEGER'
WHEN 9 THEN 'QUAD'
WHEN 10 THEN 'FLOAT'
WHEN 12 THEN 'DATE'
WHEN 13 THEN 'TIME'
WHEN 14 THEN 'CHAR'
WHEN 16 THEN 'BIGINT'
WHEN 27 THEN 'DOUBLE PRECISION'
WHEN 35 THEN 'TIMESTAMP'
WHEN 37 THEN 'VARCHAR'
WHEN 40 THEN 'CSTRING'
WHEN 261 THEN 'BLOB'
ELSE 'UNKNOWN'
END) AS "dataType"
, TRIM(CASE
WHEN f.RDB$FIELD_LENGTH > 0 THEN
CASE f.RDB$FIELD_TYPE
WHEN 7 THEN 'SMALLINT'
WHEN 8 THEN 'INTEGER'
WHEN 9 THEN 'QUAD'
WHEN 10 THEN 'FLOAT'
WHEN 12 THEN 'DATE'
WHEN 13 THEN 'TIME'
WHEN 14 THEN 'CHAR'
WHEN 16 THEN 'BIGINT'
WHEN 27 THEN 'DOUBLE PRECISION'
WHEN 35 THEN 'TIMESTAMP'
WHEN 37 THEN 'VARCHAR'
WHEN 40 THEN 'CSTRING'
WHEN 261 THEN 'BLOB'
ELSE 'UNKNOWN'
END || '(' || f.RDB$FIELD_LENGTH || ')'
ELSE
CASE f.RDB$FIELD_TYPE
WHEN 7 THEN 'SMALLINT'
WHEN 8 THEN 'INTEGER'
WHEN 9 THEN 'QUAD'
WHEN 10 THEN 'FLOAT'
WHEN 12 THEN 'DATE'
WHEN 13 THEN 'TIME'
WHEN 14 THEN 'CHAR'
WHEN 16 THEN 'BIGINT'
WHEN 27 THEN 'DOUBLE PRECISION'
WHEN 35 THEN 'TIMESTAMP'
WHEN 37 THEN 'VARCHAR'
WHEN 40 THEN 'CSTRING'
WHEN 261 THEN 'BLOB'
ELSE 'UNKNOWN'
END
END) AS "detail"
, CASE
WHEN EXISTS (
SELECT 1
FROM RDB$RELATION_CONSTRAINTS rc
JOIN RDB$INDEX_SEGMENTS sg ON rc.RDB$INDEX_NAME = sg.RDB$INDEX_NAME
WHERE rc.RDB$RELATION_NAME = rf.RDB$RELATION_NAME
AND rc.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
AND sg.RDB$FIELD_NAME = rf.RDB$FIELD_NAME
) THEN 1
ELSE 0
END AS "isPk"
, CASE
WHEN EXISTS (
SELECT 1
FROM RDB$RELATION_CONSTRAINTS rc
JOIN RDB$INDEX_SEGMENTS sg ON rc.RDB$INDEX_NAME = sg.RDB$INDEX_NAME
WHERE rc.RDB$RELATION_NAME = rf.RDB$RELATION_NAME
AND rc.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
AND sg.RDB$FIELD_NAME = rf.RDB$FIELD_NAME
) THEN 1
ELSE 0
END AS "isFk"
FROM RDB$RELATION_FIELDS rf
JOIN RDB$FIELDS f ON rf.RDB$FIELD_SOURCE = f.RDB$FIELD_NAME
WHERE 1=1
${p => p.tables.filter(t => !!t.label).length
? `AND LOWER(rf.RDB$RELATION_NAME) IN (${p.tables.filter(t => !!t.label).map(t => `'${t.label}'`.toLowerCase()).join(', ')})`
: ''
}
${p => p.search ? `AND TRIM(LOWER(rf.RDB$FIELD_NAME)) LIKE '%${p.search.toLowerCase()}%'` : ''}
ORDER BY rf.RDB$RELATION_NAME, rf.RDB$FIELD_NAME
`;

0 comments on commit d159130

Please sign in to comment.