From ed89efba70cb3e58c8d88f54eaa91dd32d12b904 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 5 Nov 2021 12:41:26 +0000 Subject: [PATCH] Cleaning up repeated work in sql server building of schema. --- .../src/integrations/microsoftSqlServer.ts | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index f8fd638082..7071ce6f75 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -188,6 +188,10 @@ module MSSQLModule { } } + async runSQL(sql: string) { + return (await internalQuery(this.client, getSqlQuery(sql))).recordset + } + /** * Fetches the tables from the sql server database and assigns them to the datasource. * @param {*} datasourceId - datasourceId to fetch @@ -195,40 +199,33 @@ module MSSQLModule { */ async buildSchema(datasourceId: string, entities: Record) { await this.connect() - let tableNames = await internalQuery( - this.client, - getSqlQuery(this.TABLES_SQL) - ) + let tableNames = await this.runSQL(this.TABLES_SQL) if (tableNames == null || !Array.isArray(tableNames.recordset)) { throw "Unable to get list of tables in database" } tableNames = tableNames.recordset .map((record: any) => record.TABLE_NAME) .filter((name: string) => this.MASTER_TABLES.indexOf(name) === -1) + const tables: Record = {} for (let tableName of tableNames) { - const definition = await internalQuery( - this.client, - getSqlQuery(this.getDefinitionSQL(tableName)) - ) - const constraints = await internalQuery( - this.client, - getSqlQuery(this.getConstraintsSQL(tableName)) - ) - const columns = await internalQuery( - this.client, - getSqlQuery(this.getAutoColumnsSQL(tableName)) - ) - const autoColumns = columns.recordset - .filter((col: any) => col.IS_COMPUTED || col.IS_IDENTITY) - .map((col: any) => col.COLUMN_NAME) - const primaryKeys = constraints.recordset + // get the column definition (type) + const definition = await this.runSQL(this.getDefinitionSQL(tableName)) + // find primary key constraints + const constraints = await this.runSQL(this.getConstraintsSQL(tableName)) + // find the computed and identity columns (auto columns) + const columns = await this.runSQL(this.getAutoColumnsSQL(tableName)) + const primaryKeys = constraints .filter( (constraint: any) => constraint.CONSTRAINT_TYPE === "PRIMARY KEY" ) .map((constraint: any) => constraint.COLUMN_NAME) + const autoColumns = columns + .filter((col: any) => col.IS_COMPUTED || col.IS_IDENTITY) + .map((col: any) => col.COLUMN_NAME) + let schema: TableSchema = {} - for (let def of definition.recordset) { + for (let def of definition) { const name = def.COLUMN_NAME if (typeof name !== "string") { continue