From c5a3ee01eeffe5dea4a7cb5ccf5ae0de75749d91 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 19 Aug 2021 15:14:27 +0200 Subject: [PATCH] sql server index analyser works --- .../__tests__/table-analyse.spec.js | 2 ++ packages/types/dbinfo.d.ts | 2 ++ .../src/backend/MsSqlAnalyser.js | 26 ++++++++++++++++--- .../src/backend/sql/indexcols.js | 9 ++++--- .../src/backend/sql/indexes.js | 2 +- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/integration-tests/__tests__/table-analyse.spec.js b/integration-tests/__tests__/table-analyse.spec.js index 5814236b..3282122a 100644 --- a/integration-tests/__tests__/table-analyse.spec.js +++ b/integration-tests/__tests__/table-analyse.spec.js @@ -120,6 +120,8 @@ describe('Table analyse', () => { const t1 = structure.tables.find(x => x.pureName == 't1'); expect(t1.indexes.length).toEqual(1); + expect(t1.indexes[0].columns.length).toEqual(1); + expect(t1.indexes[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val1' })); }) ); }); diff --git a/packages/types/dbinfo.d.ts b/packages/types/dbinfo.d.ts index 191423da..173b77ad 100644 --- a/packages/types/dbinfo.d.ts +++ b/packages/types/dbinfo.d.ts @@ -6,6 +6,8 @@ export interface NamedObjectInfo { export interface ColumnReference { columnName: string; refColumnName?: string; + isIncludedColumn?: boolean; + isDescending?: boolean; } export interface ConstraintInfo extends NamedObjectInfo { diff --git a/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js b/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js index c60746c2..498c21d7 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js +++ b/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js @@ -51,6 +51,7 @@ class MsSqlAnalyser extends DatabaseAnalyser { } createQuery(resFileName, typeFields) { + if (!sql[resFileName]) throw new Error(`Missing analyse file ${resFileName}`); return super.createQuery(sql[resFileName], typeFields); } @@ -67,6 +68,8 @@ class MsSqlAnalyser extends DatabaseAnalyser { const pkColumnsRows = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables'])); const fkColumnsRows = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables'])); 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 schemas = schemaRows.rows; @@ -92,6 +95,26 @@ class MsSqlAnalyser extends DatabaseAnalyser { columns: columnsRows.rows.filter(col => col.objectId == row.objectId).map(getColumnInfo), primaryKey: DatabaseAnalyser.extractPrimaryKeys(row, pkColumnsRows.rows), foreignKeys: DatabaseAnalyser.extractForeignKeys(row, fkColumnsRows.rows), + indexes: indexesRows.rows + .filter(idx => idx.object_id == row.objectId && !idx.is_unique_constraint) + .map(idx => ({ + ..._.pick(idx, ['constraintName', 'indexType', 'isUnique']), + columns: indexcolsRows.rows.filter( + col => col.object_id == idx.object_id && col.index_id == idx.index_id + ).map(col => ({ + ..._.pick(col, ['columnName', 'isDescending', 'isIncludedColumn']), + })), + })), + uniques: indexesRows.rows + .filter(idx => idx.object_id == row.objectId && idx.is_unique_constraint) + .map(idx => ({ + ..._.pick(idx, ['constraintName']), + columns: indexcolsRows.rows.filter( + col => col.object_id == idx.object_id && col.index_id == idx.index_id + ).map(col => ({ + ..._.pick(col, ['columnName']), + })), + })), })); const views = viewsRows.rows.map(row => ({ @@ -117,9 +140,6 @@ class MsSqlAnalyser extends DatabaseAnalyser { createSql: getCreateSql(row), })); - const indexesRows = await this.driver.query(this.pool, this.createQuery('indexes', ['tables'])); - const indexcolsRows = await this.driver.query(this.pool, this.createQuery('indexesindexcols', ['tables'])); - return { tables, views, diff --git a/plugins/dbgate-plugin-mssql/src/backend/sql/indexcols.js b/plugins/dbgate-plugin-mssql/src/backend/sql/indexcols.js index e3eca23b..7dbb8a64 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/sql/indexcols.js +++ b/plugins/dbgate-plugin-mssql/src/backend/sql/indexcols.js @@ -1,7 +1,10 @@ module.exports = ` -select c.object_id, c.index_id, c.column_id, c.is_descending_key, c.is_included_column from sys.index_columns c - +select + c.object_id, c.index_id, c.column_id, + col.name as columnName, + c.is_descending_key as isDescending, c.is_included_column as isIncludedColumn +from sys.index_columns c +inner join sys.columns col on c.object_id = col.object_id and c.column_id = col.column_id where c.object_id =OBJECT_ID_CONDITION order by c.key_ordinal - `; diff --git a/plugins/dbgate-plugin-mssql/src/backend/sql/indexes.js b/plugins/dbgate-plugin-mssql/src/backend/sql/indexes.js index 0915f1d7..4ba26f1b 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/sql/indexes.js +++ b/plugins/dbgate-plugin-mssql/src/backend/sql/indexes.js @@ -1,5 +1,5 @@ module.exports = ` -select i.object_id as objectId, i.name as constraintName, i.type_desc, i.is_unique as isUnique,i.index_id, i.is_unique_constraint from sys.indexes i +select i.object_id, i.name as constraintName, i.type_desc as indexType, i.is_unique as isUnique,i.index_id, i.is_unique_constraint from sys.indexes i where i.is_primary_key=0 and i.is_hypothetical=0 and indexproperty(i.object_id, i.name, 'IsStatistics') = 0 and objectproperty(i.object_id, 'IsUserTable') = 1