import { Sequelize } from 'sequelize'; import { ColumnSymbol } from './symboltable/columnsymbol'; import { TableSymbol } from './symboltable/tablesymbol'; import { ViewSymbol } from './symboltable/viewsymbol'; import { PrimaryKeySymbol } from './symboltable/primarykeysymbol'; import { ReferenceSymbol } from './symboltable/referencesymbol'; import { BaseDriver } from './basedriver' export class MySQLDriver implements BaseDriver { sequelize: Sequelize; database: string; constructor(sequelize: any) { this.sequelize = sequelize; this.database = (this.sequelize).config.database; // this kills your sweet kitty! } public async readColumns(): Promise { let result = await this.getColumns(); return await result; } public async readTables(): Promise { let data = await this.getViewOrTableList(false); let result: ViewSymbol[] = []; for (let i = 0; i < data.length; i++) { let item: ViewSymbol = { name: data[i] } result.push(item); } return await result; } public async readViews(): Promise { let data = await this.getViewOrTableList(true); let result: ViewSymbol[] = []; for (let i = 0; i < data.length; i++) { let item: ViewSymbol = { name: data[i] } result.push(item); } return await result; } public async readPrimaryKeys(): Promise { let result = await this.getPrimaryKeys(); return await result; } public async readReferences(): Promise { let result = await this.getReferences(); return await result; } protected async getViewOrTableList(isView: boolean): Promise { let tableType = "base table"; if (isView) { tableType = "view"; } let sql = "select table_name " + "from information_schema.tables " + "where table_type = '" + tableType + "' and table_schema= '" + this.database + "' " + "order by table_name "; let data = await this.sequelize.query(sql); let result: string[] = []; let arr = (data)[0]; for (let i = 0; i < arr.length; i++) { result.push(arr[i].table_name); } return await result; } protected async getColumns(): Promise { let sql = "select table_name, column_name, data_type, is_nullable, " + "column_default, character_maximum_length, numeric_precision, " + "datetime_precision, ordinal_position " + "from information_schema.columns " + "where table_schema = '" + this.database + "' " + "order by table_name, ordinal_position"; let data = await this.sequelize.query(sql); let result: ColumnSymbol[] = []; let arr = (data)[0]; for (let i = 0; i < arr.length; i++) { let item: ColumnSymbol = { name: arr[i].column_name, tableName: arr[i].table_name, dataType: arr[i].data_type, nullable: arr[i].is_nullable === 'YES', defaultValue: arr[i].column_default, length: arr[i].character_maximum_length, numericPrecision: arr[i].numeric_precision, datetimePrecision: arr[i].datetime_precision } result.push(item); }; return await result; } protected async getPrimaryKeys(): Promise { let sql = "select t.table_name, k.column_name " + "from information_schema.table_constraints t " + "left join information_schema.key_column_usage k " + "using(constraint_name,table_schema,table_name) " + "where t.constraint_type='primary key' " + "and t.table_schema=database() " + "order by table_name, column_name"; let data = await this.sequelize.query(sql); let result: PrimaryKeySymbol[] = []; let arr = (data)[0]; for (let i = 0; i < arr.length; i++) { let item: PrimaryKeySymbol = { tableName: arr[i].table_name, columnName: arr[i].column_name, } result.push(item); } return await result; } protected async getReferences(): Promise { let sql: string = "select table_name, column_name, referenced_table_name as referenced_table, referenced_column_name as referenced_column " + "from information_schema.key_column_usage " + "where constraint_schema = '" + this.database + "' " + "and referenced_table_name is not null;"; let data = await this.sequelize.query(sql); let result: ReferenceSymbol[] = []; let arr = (data)[0]; for (let i = 0; i < arr.length; i++) { let item: ReferenceSymbol = { tableName: arr[i].table_name, columnName: arr[i].column_name, referencedTableName: arr[i].referenced_table, referencedColumnName: arr[i].referenced_column, } result.push(item); } return await result; } }