Cleaning up repeated work in sql server building of schema.

This commit is contained in:
mike12345567 2021-11-05 12:41:26 +00:00
parent 9c933b629f
commit 5ec0d803af
1 changed files with 18 additions and 21 deletions

View File

@ -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. * Fetches the tables from the sql server database and assigns them to the datasource.
* @param {*} datasourceId - datasourceId to fetch * @param {*} datasourceId - datasourceId to fetch
@ -195,40 +199,33 @@ module MSSQLModule {
*/ */
async buildSchema(datasourceId: string, entities: Record<string, Table>) { async buildSchema(datasourceId: string, entities: Record<string, Table>) {
await this.connect() await this.connect()
let tableNames = await internalQuery( let tableNames = await this.runSQL(this.TABLES_SQL)
this.client,
getSqlQuery(this.TABLES_SQL)
)
if (tableNames == null || !Array.isArray(tableNames.recordset)) { if (tableNames == null || !Array.isArray(tableNames.recordset)) {
throw "Unable to get list of tables in database" throw "Unable to get list of tables in database"
} }
tableNames = tableNames.recordset tableNames = tableNames.recordset
.map((record: any) => record.TABLE_NAME) .map((record: any) => record.TABLE_NAME)
.filter((name: string) => this.MASTER_TABLES.indexOf(name) === -1) .filter((name: string) => this.MASTER_TABLES.indexOf(name) === -1)
const tables: Record<string, Table> = {} const tables: Record<string, Table> = {}
for (let tableName of tableNames) { for (let tableName of tableNames) {
const definition = await internalQuery( // get the column definition (type)
this.client, const definition = await this.runSQL(this.getDefinitionSQL(tableName))
getSqlQuery(this.getDefinitionSQL(tableName)) // find primary key constraints
) const constraints = await this.runSQL(this.getConstraintsSQL(tableName))
const constraints = await internalQuery( // find the computed and identity columns (auto columns)
this.client, const columns = await this.runSQL(this.getAutoColumnsSQL(tableName))
getSqlQuery(this.getConstraintsSQL(tableName)) const primaryKeys = constraints
)
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
.filter( .filter(
(constraint: any) => constraint.CONSTRAINT_TYPE === "PRIMARY KEY" (constraint: any) => constraint.CONSTRAINT_TYPE === "PRIMARY KEY"
) )
.map((constraint: any) => constraint.COLUMN_NAME) .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 = {} let schema: TableSchema = {}
for (let def of definition.recordset) { for (let def of definition) {
const name = def.COLUMN_NAME const name = def.COLUMN_NAME
if (typeof name !== "string") { if (typeof name !== "string") {
continue continue