diff --git a/packages/types/dbinfo.d.ts b/packages/types/dbinfo.d.ts index 16a0aec58..cde4914eb 100644 --- a/packages/types/dbinfo.d.ts +++ b/packages/types/dbinfo.d.ts @@ -114,4 +114,5 @@ export interface DatabaseInfoObjects { export interface DatabaseInfo extends DatabaseInfoObjects { schemas: SchemaInfo[]; engine?: string; + defaultSchema?: string; } diff --git a/packages/web/src/query/codeCompletion.ts b/packages/web/src/query/codeCompletion.ts index cd9923a14..e59503b59 100644 --- a/packages/web/src/query/codeCompletion.ts +++ b/packages/web/src/query/codeCompletion.ts @@ -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( diff --git a/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js b/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js index ac756eda3..3e1b6f7b0 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js +++ b/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js @@ -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, }; }