#200 functions, materialized views and procedures in code completion

This commit is contained in:
Jan Prochazka 2021-12-11 17:15:18 +01:00
parent 765ec3ed00
commit cd503efaac
2 changed files with 66 additions and 29 deletions

View File

@ -47,11 +47,15 @@ export function mountCodeCompletion({ conid, database, editor }) {
const sources = analyseQuerySources(editor.getValue(), [ const sources = analyseQuerySources(editor.getValue(), [
...dbinfo.tables.map(x => x.pureName), ...dbinfo.tables.map(x => x.pureName),
...dbinfo.views.map(x => x.pureName), ...dbinfo.views.map(x => x.pureName),
...dbinfo.matviews.map(x => x.pureName),
// ...dbinfo.functions.map(x => x.pureName),
// ...dbinfo.procedures.map(x => x.pureName),
]); ]);
const sourceObjects = sources.map(src => { const sourceObjects = sources.map(src => {
const table = dbinfo.tables.find(x => x.pureName == src.name); const table = dbinfo.tables.find(x => x.pureName == src.name);
const view = dbinfo.views.find(x => x.pureName == src.name); const view = dbinfo.views.find(x => x.pureName == src.name);
return { ...(table || view), alias: src.alias }; const matview = dbinfo.matviews.find(x => x.pureName == src.name);
return { ...(table || view || matview), alias: src.alias };
}); });
if (colMatch) { if (colMatch) {
@ -74,7 +78,7 @@ export function mountCodeCompletion({ conid, database, editor }) {
]; ];
} }
const view = dbinfo.views.find(x => x.pureName == source.name); const view = [...(dbinfo.views || []), ...(dbinfo.matviews || [])].find(x => x.pureName == source.name);
if (view) { if (view) {
list = [ list = [
...view.columns.map(x => ({ ...view.columns.map(x => ({
@ -90,36 +94,68 @@ export function mountCodeCompletion({ conid, database, editor }) {
} else { } else {
const onlyTables = const onlyTables =
lastKeyword == 'FROM' || lastKeyword == 'JOIN' || lastKeyword == 'UPDATE' || lastKeyword == 'DELETE'; lastKeyword == 'FROM' || lastKeyword == 'JOIN' || lastKeyword == 'UPDATE' || lastKeyword == 'DELETE';
list = [ const onlyProcedures = lastKeyword == 'EXEC' || lastKeyword == 'EXECUTE' || lastKeyword == 'CALL';
...(onlyTables ? [] : list), if (onlyProcedures) {
...dbinfo.tables.map(x => ({ list = dbinfo.procedures.map(x => ({
name: x.pureName, name: x.pureName,
value: x.pureName, value: x.pureName,
caption: x.pureName, caption: x.pureName,
meta: 'table', meta: 'procedure',
score: 1000, score: 1000,
})), }));
...dbinfo.views.map(x => ({ } else {
name: x.pureName, list = [
value: x.pureName, ...(onlyTables ? [] : list),
caption: x.pureName, ...dbinfo.tables.map(x => ({
meta: 'view', name: x.pureName,
score: 1000, value: x.pureName,
})), caption: x.pureName,
...(onlyTables meta: 'table',
? [] score: 1000,
: _.flatten( })),
sourceObjects.map(obj => ...dbinfo.views.map(x => ({
obj.columns.map(col => ({ name: x.pureName,
name: col.columnName, value: x.pureName,
value: obj.alias ? `${obj.alias}.${col.columnName}` : col.columnName, caption: x.pureName,
caption: obj.alias ? `${obj.alias}.${col.columnName}` : col.columnName, meta: 'view',
meta: 'column', score: 1000,
score: 1200, })),
})) ...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,
})),
...(onlyTables
? []
: _.flatten(
sourceObjects.map(obj =>
obj.columns.map(col => ({
name: col.columnName,
value: obj.alias ? `${obj.alias}.${col.columnName}` : col.columnName,
caption: obj.alias ? `${obj.alias}.${col.columnName}` : col.columnName,
meta: `column (${obj.pureName})`,
score: 1200,
}))
)
)),
];
}
} }
} }
@ -147,7 +183,7 @@ export function mountCodeCompletion({ conid, database, editor }) {
editor.execCommand('startAutocomplete'); editor.execCommand('startAutocomplete');
} }
if (e.args == ' ' && /((from)|(join)|(update))\s*$/i.test(line)) { if (e.args == ' ' && /((from)|(join)|(update)|(call)|(exec)|(execute))\s*$/i.test(line)) {
editor.execCommand('startAutocomplete'); editor.execCommand('startAutocomplete');
} }
} }

View File

@ -72,6 +72,7 @@ const postgresDriverBase = {
getNewObjectTemplates() { getNewObjectTemplates() {
return [ return [
{ label: 'New view', sql: 'CREATE VIEW myview\nAS\nSELECT * FROM table1' }, { label: 'New view', sql: 'CREATE VIEW myview\nAS\nSELECT * FROM table1' },
{ label: 'New materialized view', sql: 'CREATE MATERIALIZED VIEW myview\nAS\nSELECT * FROM table1' },
{ {
label: 'New procedure', label: 'New procedure',
sql: `CREATE PROCEDURE myproc (arg1 INT) sql: `CREATE PROCEDURE myproc (arg1 INT)