From 35b230731af3bb3203ed25275bb97e36ccf3112b Mon Sep 17 00:00:00 2001 From: Kononnable Date: Mon, 27 Mar 2017 00:11:26 +0200 Subject: [PATCH] some logic in mssqldriver problem with connecting to mssql --- src/Engine.ts | 21 +++++++-------- src/drivers/AbstractDriver.ts | 16 +++++++++++- src/drivers/MssqlDriver.ts | 48 ++++++++++++++++++++++++++++++++--- src/index.ts | 30 +++++++++++----------- src/models/EntityInfo.ts | 1 + typings.json | 1 + 6 files changed, 87 insertions(+), 30 deletions(-) diff --git a/src/Engine.ts b/src/Engine.ts index d1f4526..d2225ef 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -4,19 +4,20 @@ import { AbstractDriver } from "./drivers/AbstractDriver"; * Engine */ export class Engine { - constructor(public Options: EngineOptions, private driver:AbstractDriver) { + constructor(private driver: AbstractDriver, public Options: EngineOptions) { } - public createModelFromDatabase(): boolean { - let entities = this.getEntitiesInfo(); - if (entities.length>0) { + public async createModelFromDatabase(): Promise { + let entities = await this.getEntitiesInfo(this.Options.databaseName, this.Options.host, this.Options.port, this.Options.user, this.Options.password); + if (entities.length > 0) { this.createModelFromMetadata(entities); } else { console.error('Tables not found in selected database. Skipping creation of typeorm model.'); } return true; } - private getEntitiesInfo(): EntityInfo[] { + private getEntitiesInfo(database: string, server: string, port: number, user: string, password: string): EntityInfo[] { + this.driver.GetDataFromServer(database, server, port, user, password) return []; } private createModelFromMetadata(entities: EntityInfo[]) { @@ -24,9 +25,9 @@ export class Engine { } } export interface EngineOptions { - host:string, - port:string, - databaseName:string, - user:string, - password:string + host: string, + port: number, + databaseName: string, + user: string, + password: string } \ No newline at end of file diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index cdee1a6..32decb4 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -2,5 +2,19 @@ * AbstractDriver */ export abstract class AbstractDriver { - + async GetDataFromServer(database:string,server:string,port:number,user:string,password:string): Promise { + await this.ConnectToServer(database,server,port,user,password); + let entities = await this.GetAllTables(); + await this.GetCoulmnsFromEntity(entities); + await this.GetIndexesFromEntity(entities); + await this.GetForeignKeysFromEntity(entities); + await this.DisconnectFromServer(); + return entities; + } + abstract async ConnectToServer(database:string,server:string,port:number,user:string,password:string); + abstract async GetAllTables(): Promise + abstract async GetCoulmnsFromEntity(entity: EntityInfo[]); + abstract async GetIndexesFromEntity(entity: EntityInfo[]); + abstract async GetForeignKeysFromEntity(entity: EntityInfo[]); + abstract async DisconnectFromServer(); } \ No newline at end of file diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index ea9e85b..d527f4e 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -1,10 +1,50 @@ -import {AbstractDriver} from './abstractDriver' +import { AbstractDriver } from './abstractDriver' +import * as MSSQL from 'mssql' /** * MssqlDriver */ export class MssqlDriver extends AbstractDriver { - constructor() { - super(); - + async GetAllTables(): Promise { + let request = new MSSQL.Request(this.Connection) + let response = await request.query("SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'"); +let x=1; + throw new Error('Method not implemented.'); } + GetCoulmnsFromEntity(entity: EntityInfo[]) { + throw new Error('Method not implemented.'); + } + GetIndexesFromEntity(entity: EntityInfo[]) { + throw new Error('Method not implemented.'); + } + GetForeignKeysFromEntity(entity: EntityInfo[]) { + throw new Error('Method not implemented.'); + } + async DisconnectFromServer() { + if (this.Connection) + await this.Connection.close(); + } + + private Connection: MSSQL.Connection; + async ConnectToServer(database: string, server: string, port: number, user: string, password: string) { + let config: MSSQL.config = { + database: database, + server: server, + port: port, + user: user, + password: password, + // driver: 'msnodesqlv8' + } + this.Connection = new MSSQL.Connection(config) + try { + var con:any = this.Connection; + let x = await con.connect("mssql://sa:password@localhost/AdventureWorksDW2014") + // await this.Connection.connect( (err)=>{console.log(err);console.log('a');}) + } catch (error) { + //TODO: errors on Connection + console.error(error); + process.abort(); + } + } + + } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d202316..3f33186 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,27 +44,27 @@ var argv = Yargs var driver: AbstractDriver; -var standardPort:string; +var standardPort: number; switch (argv.e) { case 'mssql': driver = new MssqlDriver(); - standardPort='' + standardPort = 1433; break; - default: - driver = new MssqlDriver(); - standardPort='' - break; + console.error('Database engine not recognized.') + process.abort(); + throw new Error('Database engine not recognized.'); } let engine = new Engine( - { - host:argv.h, - port:argv.p || standardPort, - databaseName:argv.d, - user:argv.u, - password:argv.x - }, driver); -// if(Yargs.) -engine.createModelFromDatabase(); + driver,{ + host: argv.h, + port: parseInt(argv.p) || standardPort, + databaseName: argv.d, + user: argv.u, + password: argv.x + }); +engine.createModelFromDatabase().then( ()=>{ + process.abort(); +}) diff --git a/src/models/EntityInfo.ts b/src/models/EntityInfo.ts index f3e57ec..2704100 100644 --- a/src/models/EntityInfo.ts +++ b/src/models/EntityInfo.ts @@ -2,6 +2,7 @@ * EntityInfo */ interface EntityInfo { + EntityName:String; Columns:ColumnInfo[]; Indexes:IndexInfo[]; } \ No newline at end of file diff --git a/typings.json b/typings.json index 9ee9485..8203879 100644 --- a/typings.json +++ b/typings.json @@ -3,6 +3,7 @@ "mustache": "registry:dt/mustache#0.8.2+20160510002910" }, "dependencies": { + "mssql": "registry:dt/mssql#3.3.0+20170311011547", "yargs": "registry:npm/yargs#5.0.0+20160907000723" } }