code completion supports schemas

This commit is contained in:
Jan Prochazka 2022-02-03 17:22:35 +01:00
parent 94ec41d95f
commit 2958eb372a
3 changed files with 57 additions and 35 deletions

View File

@ -114,4 +114,5 @@ export interface DatabaseInfoObjects {
export interface DatabaseInfo extends DatabaseInfoObjects {
schemas: SchemaInfo[];
engine?: string;
defaultSchema?: string;
}

View File

@ -23,6 +23,53 @@ const COMMON_KEYWORDS = [
'go',
];
function createTableLikeList(dbinfo, schemaCondition) {
return [
...(dbinfo.schemas?.map(x => ({
name: x.schemaName,
value: x.schemaName,
caption: x.schemaName,
meta: 'schema',
score: 1000,
})) || []),
...dbinfo.tables.filter(schemaCondition).map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'table',
score: 1000,
})),
...dbinfo.views.filter(schemaCondition).map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'view',
score: 1000,
})),
...dbinfo.matviews.filter(schemaCondition).map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'matview',
score: 1000,
})),
...dbinfo.functions.filter(schemaCondition).map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'function',
score: 1000,
})),
...dbinfo.procedures.filter(schemaCondition).map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'procedure',
score: 1000,
})),
];
}
export function mountCodeCompletion({ conid, database, editor, getText }) {
setCompleters([]);
addCompleter({
@ -90,6 +137,11 @@ export function mountCodeCompletion({ conid, database, editor, getText }) {
})),
];
}
} else {
const schema = (dbinfo.schemas || []).find(x => x.schemaName == colMatch[1]);
if (schema) {
list = createTableLikeList(dbinfo, x => x.schemaName == schema.schemaName);
}
}
} else {
const onlyTables =
@ -106,41 +158,8 @@ export function mountCodeCompletion({ conid, database, editor, getText }) {
} else {
list = [
...(onlyTables ? [] : list),
...dbinfo.tables.map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'table',
score: 1000,
})),
...dbinfo.views.map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'view',
score: 1000,
})),
...dbinfo.matviews.map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'matview',
score: 1000,
})),
...dbinfo.functions.map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'function',
score: 1000,
})),
...dbinfo.procedures.map(x => ({
name: x.pureName,
value: x.pureName,
caption: x.pureName,
meta: 'procedure',
score: 1000,
})),
...createTableLikeList(dbinfo, x => !dbinfo.defaultSchema || dbinfo.defaultSchema == x.schemaName),
...(onlyTables
? []
: _.flatten(

View File

@ -74,6 +74,7 @@ class MsSqlAnalyser extends DatabaseAnalyser {
const schemaRows = await this.driver.query(this.pool, this.createQuery('getSchemas'));
const indexesRows = await this.driver.query(this.pool, this.createQuery('indexes', ['tables']));
const indexcolsRows = await this.driver.query(this.pool, this.createQuery('indexcols', ['tables']));
const defaultSchemaRows = await this.driver.query(this.pool, 'SELECT SCHEMA_NAME() as name');
const schemas = schemaRows.rows;
@ -150,6 +151,7 @@ class MsSqlAnalyser extends DatabaseAnalyser {
procedures,
functions,
schemas,
defaultSchema: defaultSchemaRows.rows[0] ? defaultSchemaRows.rows[0].name : undefined,
};
}