From 29ab0e18cd66fdacd5d9620353abf5ccce23ddfc Mon Sep 17 00:00:00 2001 From: Kononnable Date: Wed, 29 Mar 2017 23:10:59 +0200 Subject: [PATCH] enumerating all columns and indexes --- package.json | 1 + src/drivers/MssqlDriver.ts | 88 +++++++++++++++++++++++++++++++---- src/models/ColumnInfo.ts | 6 ++- src/models/IndexColumnInfo.ts | 5 ++ src/models/IndexInfo.ts | 7 ++- 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 src/models/IndexColumnInfo.ts diff --git a/package.json b/package.json index a94df66..1490288 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Generates models for TypeORM from existing databases.", "scripts": { "tsc:w": "tsc -w", + "tsc": "tsc", "typings-install": "typings install", "setup": "npm install && npm run typings-install", "start": "tsc && node ./src/index.js" diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 6cb5321..1e0b741 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -6,23 +6,91 @@ import * as MSSQL from 'mssql' export class MssqlDriver extends AbstractDriver { async GetAllTables(): Promise { let request = new MSSQL.Request(this.Connection) - let response: { TABLE_SCHEMA: 'string', TABLE_NAME: 'string' }[] + let response: { TABLE_SCHEMA: string, TABLE_NAME: string }[] = await request.query("SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'"); - let ret:EntityInfo[]=[]; - response.forEach( (val)=>{ - let ent:EntityInfo={}; - ent.EntityName=val.TABLE_NAME; + let ret: EntityInfo[] = []; + response.forEach((val) => { + let ent: EntityInfo = {}; + ent.EntityName = val.TABLE_NAME; + ent.Columns = []; + ent.Indexes = []; ret.push(ent); }) return ret; } - GetCoulmnsFromEntity(entity: EntityInfo[]) { - throw new Error('Method not implemented.'); + async GetCoulmnsFromEntity(entities: EntityInfo[]) { + let request = new MSSQL.Request(this.Connection) + let response: { TABLE_NAME: string, COLUMN_NAME: string, COLUMN_DEFAULT: string, IS_NULLABLE: string, DATA_TYPE: string, CHARACTER_MAXIMUM_LENGTH: number }[] + = await request.query("SELECT TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS"); + entities.forEach((ent) => { + response.filter((filterVal) => { + return filterVal.TABLE_NAME == ent.EntityName; + }).forEach((resp) => { + let colInfo: ColumnInfo = {}; + colInfo.name = resp.COLUMN_NAME; + colInfo.is_nullable = resp.IS_NULLABLE == 'YES' ? true : false; + colInfo.default = resp.COLUMN_DEFAULT; + colInfo.data_type = resp.DATA_TYPE;//TODO:Parse to Typeorm types + colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH; + ent.Columns.push(colInfo); + }) + }) + return entities; } - GetIndexesFromEntity(entity: EntityInfo[]) { - throw new Error('Method not implemented.'); + async GetIndexesFromEntity(entities: EntityInfo[]) { + let request = new MSSQL.Request(this.Connection) + let response: { + TableName: string, IndexName: string, ColumnName: string, is_unique: number, + is_primary_key: number, is_descending_key: number, is_included_column: number + }[] + = await request.query(`SELECT + TableName = t.name, + IndexName = ind.name, + ColumnName = col.name, + ind.is_unique, + ind.is_primary_key, + ic.is_descending_key, + ic.is_included_column +FROM + sys.indexes ind +INNER JOIN + sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id +INNER JOIN + sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id +INNER JOIN + sys.tables t ON ind.object_id = t.object_id +WHERE + t.is_ms_shipped = 0 +ORDER BY + t.name, ind.name, ind.index_id, ic.key_ordinal;`); + entities.forEach((ent) => { + response.filter((filterVal) => { + return filterVal.TableName == ent.EntityName; + }).forEach((resp) => { + let indexInfo: IndexInfo = {}; + let indexColumnInfo: IndexColumnInfo = {}; + if (ent.Indexes.filter((filterVal) => { + return filterVal.name == resp.IndexName + }).length > 0) { + indexInfo = ent.Indexes.filter((filterVal) => { + return filterVal.name == resp.IndexName + })[0]; + } else { + indexInfo.columns = []; + indexInfo.name = resp.IndexName; + indexInfo.isUnique = resp.is_unique == 1 ? true : false; + indexInfo.isPrimaryKey = resp.is_primary_key == 1 ? true : false; + ent.Indexes.push(indexInfo); + } + indexColumnInfo.name = resp.ColumnName; + indexColumnInfo.isIncludedColumn = resp.is_included_column == 1 ? true : false; + indexColumnInfo.isDescending = resp.is_descending_key == 1 ? true : false; + indexInfo.columns.push(indexColumnInfo); + }) + }) + return entities; } - GetForeignKeysFromEntity(entity: EntityInfo[]) { + GetForeignKeysFromEntity(entities: EntityInfo[]) { throw new Error('Method not implemented.'); } async DisconnectFromServer() { diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index 034f207..2865cd0 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -2,5 +2,9 @@ * ColumnInfo */ interface ColumnInfo { - + name:string, + default:string, + is_nullable:boolean, + data_type:string, + char_max_lenght:number } \ No newline at end of file diff --git a/src/models/IndexColumnInfo.ts b/src/models/IndexColumnInfo.ts new file mode 100644 index 0000000..1bd0c44 --- /dev/null +++ b/src/models/IndexColumnInfo.ts @@ -0,0 +1,5 @@ +interface IndexColumnInfo{ + name:string, + isDescending:boolean, + isIncludedColumn:boolean +} \ No newline at end of file diff --git a/src/models/IndexInfo.ts b/src/models/IndexInfo.ts index 9358d74..ea9b74a 100644 --- a/src/models/IndexInfo.ts +++ b/src/models/IndexInfo.ts @@ -2,5 +2,8 @@ * IndexInfo */ interface IndexInfo { - -} \ No newline at end of file + name:string, + columns:IndexColumnInfo[], + isUnique:boolean, + isPrimaryKey:boolean, +}