From a3edec7119fb9584e7ee1b98d0b22beba023d8ee Mon Sep 17 00:00:00 2001 From: kononnable Date: Sun, 6 Sep 2020 23:19:38 +0200 Subject: [PATCH] move table filtering logic --- src/IConnectionOptions.ts | 4 +-- src/drivers/AbstractDriver.ts | 38 ++++++++++++++--------- src/drivers/MssqlDriver.ts | 17 ++-------- src/drivers/MysqlDriver.ts | 17 ++-------- src/drivers/OracleDriver.ts | 17 ++-------- src/drivers/PostgresDriver.ts | 17 ++-------- src/drivers/SqliteDriver.ts | 14 ++------- src/index.ts | 10 +++--- test/integration/runTestsFromPath.test.ts | 4 +-- test/utils/GeneralTestUtils.ts | 12 +++---- 10 files changed, 48 insertions(+), 102 deletions(-) diff --git a/src/IConnectionOptions.ts b/src/IConnectionOptions.ts index 37b0b8a..f7dec18 100644 --- a/src/IConnectionOptions.ts +++ b/src/IConnectionOptions.ts @@ -17,7 +17,7 @@ export default interface IConnectionOptions { schemaName: string; ssl: boolean; skipTables: string[]; - tables: string[]; + onlyTables: string[]; } export function getDefaultConnectionOptions(): IConnectionOptions { @@ -31,7 +31,7 @@ export function getDefaultConnectionOptions(): IConnectionOptions { schemaName: "", ssl: false, skipTables: [], - tables: [], + onlyTables: [], }; return connectionOptions; } diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index c2eec21..10b7d38 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -69,9 +69,7 @@ export default abstract class AbstractDriver { public abstract GetAllTablesQuery: ( schema: string, - dbNames: string, - notIntTables: string[], - inTables: string[] + dbNames: string ) => Promise< { TABLE_SCHEMA: string; @@ -189,9 +187,7 @@ export default abstract class AbstractDriver { ); dbModel = await this.GetAllTables( sqlEscapedSchema, - connectionOptions.databaseName, - connectionOptions.skipTables, - connectionOptions.tables + connectionOptions.databaseName ); await this.GetCoulmnsFromEntity( dbModel, @@ -212,23 +208,35 @@ export default abstract class AbstractDriver { ); await this.DisconnectFromServer(); dbModel = AbstractDriver.FindManyToManyRelations(dbModel); + dbModel = AbstractDriver.FilterGeneratedTables( + dbModel, + connectionOptions.skipTables, + connectionOptions.onlyTables + ); return dbModel; } + static FilterGeneratedTables( + dbModel: Entity[], + skipTables: string[], + onlyTables: string[] + ): Entity[] { + return dbModel + .filter((table) => !skipTables.includes(table.sqlName)) + .filter( + (table) => + onlyTables.length === 0 || + onlyTables.includes(table.sqlName) + ); + } + public abstract async ConnectToServer(connectionOptons: IConnectionOptions); public async GetAllTables( schema: string, - dbNames: string, - notInTables: string[], - inTables: string[] + dbNames: string ): Promise { - const response = await this.GetAllTablesQuery( - schema, - dbNames, - notInTables, - inTables - ); + const response = await this.GetAllTablesQuery(schema, dbNames); const ret: Entity[] = [] as Entity[]; response.forEach((val) => { ret.push({ diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 054eead..01dcb66 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -37,21 +37,8 @@ export default class MssqlDriver extends AbstractDriver { } } - public GetAllTablesQuery = async ( - schema: string, - dbNames: string, - notInTables: string[], - inTables: string[] - ) => { + public GetAllTablesQuery = async (schema: string, dbNames: string) => { const request = new this.MSSQL.Request(this.Connection); - const tableCondition = - notInTables.length > 0 - ? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')` - : ""; - const inTableCondition = - inTables.length > 0 - ? ` AND TABLE_NAME IN ('${inTables.join("','")}')` - : ""; const response: { TABLE_SCHEMA: string; TABLE_NAME: string; @@ -61,7 +48,7 @@ export default class MssqlDriver extends AbstractDriver { `SELECT TABLE_SCHEMA,TABLE_NAME, table_catalog as "DB_NAME" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG in (${MssqlDriver.escapeCommaSeparatedList( dbNames - )}) ${tableCondition} ${inTableCondition}` + )})` ) ).recordset; return response; diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index fbb259d..921c310 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -39,20 +39,7 @@ export default class MysqlDriver extends AbstractDriver { } } - public GetAllTablesQuery = async ( - schema: string, - dbNames: string, - notInTables: string[], - inTables: string[] - ) => { - const tableCondition = - notInTables.length > 0 - ? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')` - : ""; - const inTableCondition = - inTables.length > 0 - ? ` AND TABLE_NAME IN ('${inTables.join("','")}')` - : ""; + public GetAllTablesQuery = async (schema: string, dbNames: string) => { const response = this.ExecQuery<{ TABLE_SCHEMA: string; TABLE_NAME: string; @@ -62,7 +49,7 @@ export default class MysqlDriver extends AbstractDriver { WHERE table_type='BASE TABLE' AND table_schema IN (${MysqlDriver.escapeCommaSeparatedList( dbNames - )}) ${tableCondition} ${inTableCondition}`); + )})`); return response; }; diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index b283c13..9636349 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -38,27 +38,14 @@ export default class OracleDriver extends AbstractDriver { } } - public GetAllTablesQuery = async ( - schema: string, - dbNames: string, - notInTables: string[], - inTables: string[] - ) => { - const tableCondition = - notInTables.length > 0 - ? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')` - : ""; - const inTableCondition = - inTables.length > 0 - ? ` AND TABLE_NAME IN ('${inTables.join("','")}')` - : ""; + public GetAllTablesQuery = async (schema: string, dbNames: string) => { const response = ( await this.Connection.execute<{ TABLE_SCHEMA: string; TABLE_NAME: string; DB_NAME: string; }>( - `SELECT NULL AS TABLE_SCHEMA, TABLE_NAME, NULL AS DB_NAME FROM all_tables WHERE owner = (select user from dual) ${tableCondition} ${inTableCondition}` + `SELECT NULL AS TABLE_SCHEMA, TABLE_NAME, NULL AS DB_NAME FROM all_tables WHERE owner = (select user from dual)` ) ).rows!; return response; diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index a5884cc..1178e09 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -37,27 +37,14 @@ export default class PostgresDriver extends AbstractDriver { } } - public GetAllTablesQuery = async ( - schema: string, - dbNames: string, - notInTables: string[], - inTables: string[] - ) => { - const tableCondition = - notInTables.length > 0 - ? ` AND NOT table_name IN ('${notInTables.join("','")}')` - : ""; - const inTableCondition = - inTables.length > 0 - ? ` AND table_name IN ('${inTables.join("','")}')` - : ""; + public GetAllTablesQuery = async (schema: string, dbNames: string) => { const response: { TABLE_SCHEMA: string; TABLE_NAME: string; DB_NAME: string; }[] = ( await this.Connection.query( - `SELECT table_schema as "TABLE_SCHEMA",table_name as "TABLE_NAME", table_catalog as "DB_NAME" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND table_schema in (${schema}) ${tableCondition} ${inTableCondition}` + `SELECT table_schema as "TABLE_SCHEMA",table_name as "TABLE_NAME", table_catalog as "DB_NAME" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND table_schema in (${schema})` ) ).rows; return response; diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index 1c8ed19..abe1fa2 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -46,22 +46,12 @@ export default class SqliteDriver extends AbstractDriver { public async GetAllTables( schema: string, - dbNames: string, - notInTables: string[], - inTables: string[] + dbNames: string ): Promise { const ret: Entity[] = [] as Entity[]; - const tableCondition = - notInTables.length > 0 - ? ` AND NOT tbl_name IN ('${notInTables.join("','")}')` - : ""; - const inTableCondition = - inTables.length > 0 - ? ` AND tbl_name IN ('${inTables.join("','")}')` - : ""; // eslint-disable-next-line camelcase const rows = await this.ExecQuery<{ tbl_name: string; sql: string }>( - `SELECT tbl_name, sql FROM "sqlite_master" WHERE "type" = 'table' AND name NOT LIKE 'sqlite_%' ${tableCondition} ${inTableCondition}` + `SELECT tbl_name, sql FROM "sqlite_master" WHERE "type" = 'table' AND name NOT LIKE 'sqlite_%'` ); rows.forEach((val) => { if (val.sql.includes("AUTOINCREMENT")) { diff --git a/src/index.ts b/src/index.ts index 165d8c5..1cedf6b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -272,7 +272,7 @@ function checkYargsParameters(options: options): options { }, tables: { string: true, - default: options.connectionOptions.tables.join(","), + default: options.connectionOptions.onlyTables.join(","), describe: "Generate specific tables. You can pass multiple values separated by comma", }, @@ -316,7 +316,7 @@ function checkYargsParameters(options: options): options { tables = []; } options.connectionOptions.skipTables = skipTables; - options.connectionOptions.tables = tables; + options.connectionOptions.onlyTables = tables; options.generationOptions.activeRecord = argv.a; options.generationOptions.generateConstructor = argv.generateConstructor; options.generationOptions.convertCaseEntity = argv.ce as IGenerationOptions["convertCaseEntity"]; @@ -466,7 +466,7 @@ async function useInquirer(options: options): Promise { const optionsMapper = { "All of them": () => { options.connectionOptions.skipTables = []; - options.connectionOptions.tables = []; + options.connectionOptions.onlyTables = []; }, "Ignore specific tables": async () => { const { tableNames } = await inquirer.prompt({ @@ -479,12 +479,12 @@ async function useInquirer(options: options): Promise { }, "Select specific tables": async () => { const { tableNames } = await inquirer.prompt({ - default: options.connectionOptions.tables.join(","), + default: options.connectionOptions.onlyTables.join(","), message: "Table names(separated by comma)", name: "tableNames", type: "input", }); - options.connectionOptions.tables = tableNames.split(","); + options.connectionOptions.onlyTables = tableNames.split(","); }, }; diff --git a/test/integration/runTestsFromPath.test.ts b/test/integration/runTestsFromPath.test.ts index 66813b8..c7f048d 100644 --- a/test/integration/runTestsFromPath.test.ts +++ b/test/integration/runTestsFromPath.test.ts @@ -351,7 +351,7 @@ async function prepareTestRuns( schemaName: "ignored", ssl: yn(process.env.MYSQL_SSL, { default: false }), skipTables: [], - tables: [], + onlyTables: [], }; break; case "mariadb": @@ -365,7 +365,7 @@ async function prepareTestRuns( schemaName: "ignored", ssl: yn(process.env.MARIADB_SSL, { default: false }), skipTables: [], - tables: [] + onlyTables: [] }; break; diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index 55163a9..763225d 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -32,7 +32,7 @@ export async function createMSSQLModels( schemaName: "dbo,sch1,sch2", ssl: yn(process.env.MSSQL_SSL, { default: false }), skipTables: [], - tables: [] + onlyTables: [] }; await driver.ConnectToServer(connectionOptions); connectionOptions.databaseName = String(process.env.MSSQL_Database); @@ -85,7 +85,7 @@ export async function createPostgresModels( schemaName: "public,sch1,sch2", ssl: yn(process.env.POSTGRES_SSL, { default: false }), skipTables: ["spatial_ref_sys"], - tables: [] + onlyTables: [] }; await driver.ConnectToServer(connectionOptions); connectionOptions.databaseName = String(process.env.POSTGRES_Database); @@ -137,7 +137,7 @@ export async function createSQLiteModels( schemaName: "", ssl: false, skipTables: [], - tables: [] + onlyTables: [] }; const connOpt: ConnectionOptions = { @@ -173,7 +173,7 @@ export async function createMysqlModels( schemaName: "ignored", ssl: yn(process.env.MYSQL_SSL, { default: false }), skipTables: [], - tables: [] + onlyTables: [] }; await driver.ConnectToServer(connectionOptions); @@ -217,7 +217,7 @@ export async function createMariaDBModels( schemaName: "ignored", ssl: yn(process.env.MARIADB_SSL, { default: false }), skipTables: [], - tables: [] + onlyTables: [] }; await driver.ConnectToServer(connectionOptions); @@ -263,7 +263,7 @@ export async function createOracleDBModels( schemaName: String(process.env.ORACLE_Username), ssl: yn(process.env.ORACLE_SSL, { default: false }), skipTables: [], - tables: [] + onlyTables: [] }; await driver.ConnectToServer(connectionOptions); connectionOptions.user = String(process.env.ORACLE_Username);