From 649b07b17148841a23c0ce9b6dea0e7d322ce4f9 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Mon, 11 Nov 2024 14:18:19 +0200 Subject: [PATCH 1/8] HCK-8696: optimized indexes query --- .../databaseService/databaseService.js | 74 +++++++++++-------- ...ForRetrievingTheTablesSelectedByTheUser.js | 33 --------- ...ForRetrievingTheTablesSelectedByTheUser.js | 32 ++++++++ ...ForRetrievingTheTablesSelectedByTheUser.js | 32 ++++++++ ...ForRetrievingTheTablesSelectedByTheUser.js | 48 ++++++++++++ .../reverseEngineeringService.js | 4 +- 6 files changed, 157 insertions(+), 66 deletions(-) delete mode 100644 reverse_engineering/queries/queryForRetrievingTheTablesSelectedByTheUser.js create mode 100644 reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js create mode 100644 reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js create mode 100644 reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js diff --git a/reverse_engineering/databaseService/databaseService.js b/reverse_engineering/databaseService/databaseService.js index 89c3bd4..5470e1c 100644 --- a/reverse_engineering/databaseService/databaseService.js +++ b/reverse_engineering/databaseService/databaseService.js @@ -7,7 +7,13 @@ const { logAuthTokenInfo } = require('../helpers/logInfo'); const { getConnection } = require('./helpers/connection'); const { queryForRetrievingTheTablesSelectedByTheUser, -} = require('../queries/queryForRetrievingTheTablesSelectedByTheUser'); +} = require('../queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser'); +const { + PartitionsQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser'); +const { + DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser'); const QUERY_REQUEST_TIMEOUT = 60000; @@ -194,38 +200,40 @@ const getViewDistributedColumns = async (connectionClient, dbName, tableName, ta ); }; -const getDatabaseIndexes = async (connectionClient, dbName, logger) => { - const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - +const getDatabaseIndexes = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database indexes.` }, 'Reverse Engineering'); + const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); + const tablesSelectedByTheUser = new DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser().getQuery({ + schemaToTablesMap: tablesInfo, + }); + const queryRetrievingTheIndexes = ` + WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) + SELECT + TableName = t.${tablesSelectedByTheUser.projection.tableName}, + IndexName = ind.name, + ic.is_descending_key, + ic.is_included_column, + ic.column_store_order_ordinal, + COL_NAME(t.${tablesSelectedByTheUser.projection.tableId}, ic.column_id) as columnName, + S.name as schemaName, + p.data_compression_desc as dataCompression, + ind.* + FROM sys.indexes ind + LEFT JOIN user_selected_tables t + ON ind.object_id = t.${tablesSelectedByTheUser.projection.tableId} + INNER JOIN sys.index_columns ic + ON ind.object_id = ic.object_id AND ind.index_id = ic.index_id + INNER JOIN sys.partitions p + ON p.object_id = t.${tablesSelectedByTheUser.projection.tableId} AND ind.index_id = p.index_id + INNER JOIN sys.objects O ON O.object_id = t.${tablesSelectedByTheUser.projection.tableId} + INNER JOIN sys.schemas S ON S.schema_id = O.schema_id + WHERE + ind.is_primary_key = 0 + AND ind.is_unique_constraint = 0 + AND t.${tablesSelectedByTheUser.projection.isMsSkipped} = 0 + `; - return mapResponse( - await currentDbConnectionClient.query` - SELECT - TableName = t.name, - IndexName = ind.name, - ic.is_descending_key, - ic.is_included_column, - ic.column_store_order_ordinal, - COL_NAME(t.object_id, ic.column_id) as columnName, - S.name as schemaName, - p.data_compression_desc as dataCompression, - ind.* - FROM sys.indexes ind - LEFT JOIN sys.tables t - ON ind.object_id = t.object_id - INNER JOIN sys.index_columns ic - ON ind.object_id = ic.object_id AND ind.index_id = ic.index_id - INNER JOIN sys.partitions p - ON p.object_id = t.object_id AND ind.index_id = p.index_id - INNER JOIN sys.objects O ON O.object_id = t.object_id - INNER JOIN sys.schemas S ON S.schema_id = O.schema_id - WHERE - ind.is_primary_key = 0 - AND ind.is_unique_constraint = 0 - AND t.is_ms_shipped = 0 - `, - ); + return mapResponse(await currentDbConnectionClient.query(queryRetrievingTheIndexes)); }; const getViewsIndexes = async (connectionClient, dbName) => { @@ -259,7 +267,9 @@ const getViewsIndexes = async (connectionClient, dbName) => { const getPartitions = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database partitions.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo }); + const tablesSelectedByTheUser = new PartitionsQueryForRetrievingTheTablesSelectedByTheUser().getQuery({ + schemaToTablesMap: tablesInfo, + }); const queryForRetrievingThePartitions = ` WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) SELECT diff --git a/reverse_engineering/queries/queryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/queryForRetrievingTheTablesSelectedByTheUser.js deleted file mode 100644 index 9e19c8d..0000000 --- a/reverse_engineering/queries/queryForRetrievingTheTablesSelectedByTheUser.js +++ /dev/null @@ -1,33 +0,0 @@ -function buildPredicateForTable({ schema, table }) { - return `(sch.name = '${schema}' AND tbl.name = '${table}')`; -} - -function buildPredicateForTablesInSchema({ schema, tables }) { - return tables.map(table => buildPredicateForTable({ schema, table })).join('OR'); -} - -function queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap }) { - const projection = { - tableId: 'tableId', - tableName: 'tableName', - schemaName: 'schemaName', - }; - const predicate = Object.entries(schemaToTablesMap) - .map(([schema, tables]) => buildPredicateForTablesInSchema({ schema, tables })) - .join('OR'); - const whereClause = Object.entries(schemaToTablesMap).length > 0 ? `WHERE ${predicate}` : ''; - const sql = ` - SELECT - tbl.object_id AS ${projection.tableId} - , tbl.name AS ${projection.tableName} - , sch.name AS ${projection.schemaName} - FROM sys.tables tbl - JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id - ${whereClause}`; - return { - projection, - sql: () => sql, - }; -} - -module.exports = { queryForRetrievingTheTablesSelectedByTheUser }; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js new file mode 100644 index 0000000..afd9398 --- /dev/null +++ b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js @@ -0,0 +1,32 @@ +const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); + +class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { + getQuery({ schemaToTablesMap }) { + const propertiesToSelect = { + tableId: 'tbl.object_id', + tableName: 'tbl.name', + isMsSkipped: 'tbl.is_ms_shipped', + }; + + const projection = { + tableId: 'tableId', + tableName: 'tableName', + isMsSkipped: 'isMsSkipped', + }; + + const query = this.queryForRetrievingTheTablesSelectedByTheUser({ + schemaToTablesMap, + propertiesToSelect, + propertyNameProjection: projection, + }); + + return { + projection, + sql: () => query, + }; + } +} + +module.exports = { + DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser, +}; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js new file mode 100644 index 0000000..f05d014 --- /dev/null +++ b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js @@ -0,0 +1,32 @@ +const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); + +class PartitionsQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { + getQuery({ schemaToTablesMap }) { + const propertiesToSelect = { + tableId: 'tbl.object_id', + tableName: 'tbl.name', + schemaName: 'sch.name', + }; + + const projection = { + tableId: 'tableId', + tableName: 'tableName', + schemaName: 'schemaName', + }; + + const query = this.queryForRetrievingTheTablesSelectedByTheUser({ + schemaToTablesMap, + propertiesToSelect, + propertyNameProjection: projection, + }); + + return { + projection, + sql: () => query, + }; + } +} + +module.exports = { + PartitionsQueryForRetrievingTheTablesSelectedByTheUser, +}; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js new file mode 100644 index 0000000..c848203 --- /dev/null +++ b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js @@ -0,0 +1,48 @@ +class QueryForRetrievingTheTablesSelectedByTheUser { + #buildPredicateForTable({ schema, table }) { + return `(sch.name = '${schema}' AND tbl.name = '${table}')`; + } + + #buildPredicateForTablesInSchema({ schema, tables }) { + return tables.map(table => this.#buildPredicateForTable({ schema, table })).join('OR'); + } + + #buildProjectionForProperty({ propertyName, projectionName }) { + return `${propertyName} AS ${projectionName}`; + } + + #buildProjectionsForPropertiesSelectedByQuery({ propertiesToSelect, propertyNameProjection }) { + const propertiesNames = Object.keys(propertiesToSelect); + + return propertiesNames + .map(propertyName => + this.#buildProjectionForProperty({ + propertyName: propertiesToSelect[propertyName], + projectionName: propertyNameProjection[propertyName], + }), + ) + .join(','); + } + + queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap, propertiesToSelect, propertyNameProjection }) { + const propertiesToSelectProjections = this.#buildProjectionsForPropertiesSelectedByQuery({ + propertiesToSelect, + propertyNameProjection, + }); + const predicate = Object.entries(schemaToTablesMap) + .map(([schema, tables]) => this.#buildPredicateForTablesInSchema({ schema, tables })) + .join('OR'); + const whereClause = Object.entries(schemaToTablesMap).length > 0 ? `WHERE ${predicate}` : ''; + return ` + SELECT + ${propertiesToSelectProjections} + FROM sys.tables tbl + JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id + ${whereClause} + `; + } +} + +module.exports = { + QueryForRetrievingTheTablesSelectedByTheUser, +}; diff --git a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js index 5f667ec..9283a50 100644 --- a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js +++ b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js @@ -404,7 +404,9 @@ const reverseCollectionsToJSON = logger => async (dbConnectionClient, tablesInfo const dbName = dbConnectionClient.config.database; progress(logger, `RE data from database "${dbName}"`, dbName); const [databaseIndexes, databaseMemoryOptimizedTables, databaseUDT, dataBasePartitions] = await Promise.all([ - getDatabaseIndexes(dbConnectionClient, dbName, logger).catch(logError(logger, 'Getting indexes')), + getDatabaseIndexes({ connectionClient: dbConnectionClient, tablesInfo, dbName, logger }).catch( + logError(logger, 'Getting indexes'), + ), getDatabaseMemoryOptimizedTables(dbConnectionClient, dbName, logger).catch( logError(logger, 'Getting memory optimized tables'), ), From ba734211c0d37b428f031ace5abce6544366daf3 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Tue, 12 Nov 2024 10:25:44 +0200 Subject: [PATCH 2/8] HCK-8696: optimized indexes and memory optimized tables queries --- .../databaseService/databaseService.js | 45 +++++++++++++------ ...ForRetrievingTheTablesSelectedByTheUser.js | 9 +++- ...ForRetrievingTheTablesSelectedByTheUser.js | 45 +++++++++++++++++++ ...ForRetrievingTheTablesSelectedByTheUser.js | 9 +++- .../reverseEngineeringService.js | 14 +++--- 5 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js diff --git a/reverse_engineering/databaseService/databaseService.js b/reverse_engineering/databaseService/databaseService.js index 5470e1c..54a5e9c 100644 --- a/reverse_engineering/databaseService/databaseService.js +++ b/reverse_engineering/databaseService/databaseService.js @@ -14,6 +14,9 @@ const { const { DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser, } = require('../queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser'); +const { + MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser'); const QUERY_REQUEST_TIMEOUT = 60000; @@ -203,9 +206,9 @@ const getViewDistributedColumns = async (connectionClient, dbName, tableName, ta const getDatabaseIndexes = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database indexes.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser().getQuery({ + const tablesSelectedByTheUser = new DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, - }); + }).getQuery(); const queryRetrievingTheIndexes = ` WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) SELECT @@ -267,12 +270,12 @@ const getViewsIndexes = async (connectionClient, dbName) => { const getPartitions = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database partitions.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new PartitionsQueryForRetrievingTheTablesSelectedByTheUser().getQuery({ + const tablesSelectedByTheUser = new PartitionsQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, - }); + }).getQuery(); const queryForRetrievingThePartitions = ` - WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) - SELECT + WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) + SELECT tbl.${tablesSelectedByTheUser.projection.schemaName} AS schemaName, tbl.${tablesSelectedByTheUser.projection.tableName} AS tableName, prt.partition_number, @@ -310,14 +313,27 @@ const getTableColumnsDescription = async (connectionClient, dbName, tableName, s `); }; -const getDatabaseMemoryOptimizedTables = async (connectionClient, dbName, logger) => { +const getDatabaseMemoryOptimizedTables = async ({ connectionClient, tablesInfo, dbName, logger }) => { try { - const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - logger.log('info', { message: `Get '${dbName}' database memory optimized indexes.` }, 'Reverse Engineering'); - - return mapResponse(currentDbConnectionClient.query` - SELECT + const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); + const tablesSelectedByTheUser = new MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser({ + schemaToTablesMap: tablesInfo, + }).getQuery(); + // const queryForRetrievingMemoryOptimizedTables = ` + // WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) + // SELECT + // T.${tablesSelectedByTheUser.projection.tableName}, + // T.${tablesSelectedByTheUser.projection.durability}, + // T.${tablesSelectedByTheUser.projection.durabilityDescription}, + // OBJECT_NAME(T.${tablesSelectedByTheUser.projection.historyTableId}) AS ${tablesSelectedByTheUser.projection.historyTable}, + // SCHEMA_NAME(O.schema_id) AS ${tablesSelectedByTheUser.projection.historySchema}, + // T.${tablesSelectedByTheUser.projection.temporalTypeDescription} + // FROM user_selected_tables T LEFT JOIN sys.objects O ON T.${tablesSelectedByTheUser.projection.historyTableId} = O.object_id + // WHERE T.${tablesSelectedByTheUser.projection.isMemoryOptimized}=1 + // ` + const t = ` + SELECT T.name, T.durability, T.durability_desc, @@ -326,7 +342,8 @@ const getDatabaseMemoryOptimizedTables = async (connectionClient, dbName, logger T.temporal_type_desc FROM sys.tables T LEFT JOIN sys.objects O ON T.history_table_id = O.object_id WHERE T.is_memory_optimized=1 - `); + `; + return mapResponse(currentDbConnectionClient.query(t)); } catch (error) { logger.log('error', { message: error.message, stack: error.stack, error }, 'Retrieve memory optimized tables'); @@ -459,7 +476,7 @@ const getTableDefaultConstraintNames = async (connectionClient, dbName, tableNam `); }; -const getDatabaseUserDefinedTypes = async (connectionClient, dbName, logger) => { +const getDatabaseUserDefinedTypes = async ({ connectionClient, dbName, logger }) => { const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); logger.log('info', { message: `Get '${dbName}' database UDTs.` }, 'Reverse Engineering'); diff --git a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js index afd9398..fc83b6c 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,7 +1,12 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { - getQuery({ schemaToTablesMap }) { + constructor({ schemaToTablesMap }) { + super(); + this.schemaToTablesMap = schemaToTablesMap; + } + + getQuery() { const propertiesToSelect = { tableId: 'tbl.object_id', tableName: 'tbl.name', @@ -15,7 +20,7 @@ class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryF }; const query = this.queryForRetrievingTheTablesSelectedByTheUser({ - schemaToTablesMap, + schemaToTablesMap: this.schemaToTablesMap, propertiesToSelect, propertyNameProjection: projection, }); diff --git a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js new file mode 100644 index 0000000..8109440 --- /dev/null +++ b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js @@ -0,0 +1,45 @@ +const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); + +class MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { + constructor({ schemaToTablesMap }) { + super(); + this.schemaToTablesMap = schemaToTablesMap; + } + + getQuery() { + const propertiesToSelect = { + tableName: 'tbl.name', + durability: 'tbl.durability', + durabilityDescription: 'tbl.durability_desc', + historyTableId: 'tbl.history_table_id', + temporalTypeDescription: 'tbl.temporal_type_desc', + isMemoryOptimized: 'tbl.is_memory_optimized', + }; + + const projection = { + tableName: 'tableName', + durability: 'durability', + durabilityDescription: 'durabilityDescription', + historyTableId: 'historyTableId', + temporalTypeDescription: 'temporalTypeDescription', + isMemoryOptimized: 'isMemoryOptimized', + historyTable: 'history_table', + historySchema: 'history_schema', + }; + + const query = this.queryForRetrievingTheTablesSelectedByTheUser({ + schemaToTablesMap: this.schemaToTablesMap, + propertiesToSelect, + propertyNameProjection: projection, + }); + + return { + projection, + sql: () => query, + }; + } +} + +module.exports = { + MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser, +}; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js index f05d014..6852690 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,7 +1,12 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); class PartitionsQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { - getQuery({ schemaToTablesMap }) { + constructor({ schemaToTablesMap }) { + super(); + this.schemaToTablesMap = schemaToTablesMap; + } + + getQuery() { const propertiesToSelect = { tableId: 'tbl.object_id', tableName: 'tbl.name', @@ -15,7 +20,7 @@ class PartitionsQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRet }; const query = this.queryForRetrievingTheTablesSelectedByTheUser({ - schemaToTablesMap, + schemaToTablesMap: this.schemaToTablesMap, propertiesToSelect, propertyNameProjection: projection, }); diff --git a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js index 9283a50..39748a8 100644 --- a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js +++ b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js @@ -336,11 +336,11 @@ const getMemoryOptimizedOptions = options => { return { memory_optimized: true, - durability: ['SCHEMA_ONLY', 'SCHEMA_AND_DATA'].includes(String(options.durability_desc).toUpperCase()) - ? String(options.durability_desc).toUpperCase() + durability: ['SCHEMA_ONLY', 'SCHEMA_AND_DATA'].includes(String(options.durabilityDescription).toUpperCase()) + ? String(options.durabilityDescription).toUpperCase() : '', - systemVersioning: options.temporal_type_desc === 'SYSTEM_VERSIONED_TEMPORAL_TABLE', - historyTable: options.history_table ? `${options.history_schema}.${options.history_table}` : '', + systemVersioning: options.temporalTypeDescription === 'SYSTEM_VERSIONED_TEMPORAL_TABLE', + historyTable: options.historyTable ? `${options.historySchema}.${options.historyTable}` : '', }; }; @@ -377,7 +377,7 @@ const getIndexing = (indexingInfo, order) => { }; const getOrder = indexingInfo => { - return indexingInfo.filter(column => column.column_store_order_ordinal).map(column => column.COLUMN_NAME); + return indexingInfo.filter(column => column.column_store_order_ordinal).map(column => column.columnName); }; const getTableRole = (distribution, indexing) => { @@ -407,10 +407,10 @@ const reverseCollectionsToJSON = logger => async (dbConnectionClient, tablesInfo getDatabaseIndexes({ connectionClient: dbConnectionClient, tablesInfo, dbName, logger }).catch( logError(logger, 'Getting indexes'), ), - getDatabaseMemoryOptimizedTables(dbConnectionClient, dbName, logger).catch( + getDatabaseMemoryOptimizedTables({ connectionClient: dbConnectionClient, tablesInfo, dbName, logger }).catch( logError(logger, 'Getting memory optimized tables'), ), - getDatabaseUserDefinedTypes(dbConnectionClient, dbName, logger).catch( + getDatabaseUserDefinedTypes({ connectionClient: dbConnectionClient, dbName, logger }).catch( logError(logger, 'Getting user defined types'), ), getPartitions({ connectionClient: dbConnectionClient, tablesInfo, dbName, logger }).catch( From c46953b59579d670f027a0aee7611698ff4a315d Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Tue, 12 Nov 2024 19:52:20 +0200 Subject: [PATCH 3/8] HCK-8696: refactored projection approach --- .../databaseService/databaseService.js | 35 +++++++------------ ...ForRetrievingTheTablesSelectedByTheUser.js | 18 ++++------ ...ForRetrievingTheTablesSelectedByTheUser.js | 29 +++++---------- ...ForRetrievingTheTablesSelectedByTheUser.js | 18 ++++------ ...ForRetrievingTheTablesSelectedByTheUser.js | 17 +++++---- .../getProjectedPropertiesNames.js | 6 ++++ 6 files changed, 47 insertions(+), 76 deletions(-) create mode 100644 reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js diff --git a/reverse_engineering/databaseService/databaseService.js b/reverse_engineering/databaseService/databaseService.js index 54a5e9c..8c4f854 100644 --- a/reverse_engineering/databaseService/databaseService.js +++ b/reverse_engineering/databaseService/databaseService.js @@ -320,30 +320,19 @@ const getDatabaseMemoryOptimizedTables = async ({ connectionClient, tablesInfo, const tablesSelectedByTheUser = new MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, }).getQuery(); - // const queryForRetrievingMemoryOptimizedTables = ` - // WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) - // SELECT - // T.${tablesSelectedByTheUser.projection.tableName}, - // T.${tablesSelectedByTheUser.projection.durability}, - // T.${tablesSelectedByTheUser.projection.durabilityDescription}, - // OBJECT_NAME(T.${tablesSelectedByTheUser.projection.historyTableId}) AS ${tablesSelectedByTheUser.projection.historyTable}, - // SCHEMA_NAME(O.schema_id) AS ${tablesSelectedByTheUser.projection.historySchema}, - // T.${tablesSelectedByTheUser.projection.temporalTypeDescription} - // FROM user_selected_tables T LEFT JOIN sys.objects O ON T.${tablesSelectedByTheUser.projection.historyTableId} = O.object_id - // WHERE T.${tablesSelectedByTheUser.projection.isMemoryOptimized}=1 - // ` - const t = ` - SELECT - T.name, - T.durability, - T.durability_desc, - OBJECT_NAME(T.history_table_id) AS history_table, - SCHEMA_NAME(O.schema_id) AS history_schema, - T.temporal_type_desc - FROM sys.tables T LEFT JOIN sys.objects O ON T.history_table_id = O.object_id - WHERE T.is_memory_optimized=1 + const queryForRetrievingMemoryOptimizedTables = ` + WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) + SELECT + T.${tablesSelectedByTheUser.projection.tableName}, + T.${tablesSelectedByTheUser.projection.durability}, + T.${tablesSelectedByTheUser.projection.durabilityDescription}, + OBJECT_NAME(T.${tablesSelectedByTheUser.projection.historyTableId}) AS historyTable, + SCHEMA_NAME(O.schema_id) AS historySchema, + T.${tablesSelectedByTheUser.projection.temporalTypeDescription} + FROM user_selected_tables T LEFT JOIN sys.objects O ON T.${tablesSelectedByTheUser.projection.historyTableId} = O.object_id + WHERE T.${tablesSelectedByTheUser.projection.isMemoryOptimized}=1 `; - return mapResponse(currentDbConnectionClient.query(t)); + return mapResponse(currentDbConnectionClient.query(queryForRetrievingMemoryOptimizedTables)); } catch (error) { logger.log('error', { message: error.message, stack: error.stack, error }, 'Retrieve memory optimized tables'); diff --git a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js index fc83b6c..2d5466c 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,4 +1,5 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); +const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { @@ -7,26 +8,19 @@ class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryF } getQuery() { - const propertiesToSelect = { - tableId: 'tbl.object_id', - tableName: 'tbl.name', - isMsSkipped: 'tbl.is_ms_shipped', - }; - const projection = { - tableId: 'tableId', - tableName: 'tableName', - isMsSkipped: 'isMsSkipped', + 'tbl.object_id': 'tableId', + 'tbl.name': 'tableName', + 'tbl.is_ms_shipped': 'isMsSkipped', }; const query = this.queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: this.schemaToTablesMap, - propertiesToSelect, - propertyNameProjection: projection, + projection, }); return { - projection, + projection: getProjectedPropertiesNames({ projection }), sql: () => query, }; } diff --git a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js index 8109440..f8db465 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,4 +1,5 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); +const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); class MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { @@ -7,34 +8,22 @@ class MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser extends } getQuery() { - const propertiesToSelect = { - tableName: 'tbl.name', - durability: 'tbl.durability', - durabilityDescription: 'tbl.durability_desc', - historyTableId: 'tbl.history_table_id', - temporalTypeDescription: 'tbl.temporal_type_desc', - isMemoryOptimized: 'tbl.is_memory_optimized', - }; - const projection = { - tableName: 'tableName', - durability: 'durability', - durabilityDescription: 'durabilityDescription', - historyTableId: 'historyTableId', - temporalTypeDescription: 'temporalTypeDescription', - isMemoryOptimized: 'isMemoryOptimized', - historyTable: 'history_table', - historySchema: 'history_schema', + 'tbl.name': 'tableName', + 'tbl.durability': 'durability', + 'tbl.durability_desc': 'durabilityDescription', + 'tbl.history_table_id': 'historyTableId', + 'tbl.temporal_type_desc': 'temporalTypeDescription', + 'tbl.is_memory_optimized': 'isMemoryOptimized', }; const query = this.queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: this.schemaToTablesMap, - propertiesToSelect, - propertyNameProjection: projection, + projection, }); return { - projection, + projection: getProjectedPropertiesNames({ projection }), sql: () => query, }; } diff --git a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js index 6852690..09bf08e 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,4 +1,5 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); +const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); class PartitionsQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { @@ -7,26 +8,19 @@ class PartitionsQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRet } getQuery() { - const propertiesToSelect = { - tableId: 'tbl.object_id', - tableName: 'tbl.name', - schemaName: 'sch.name', - }; - const projection = { - tableId: 'tableId', - tableName: 'tableName', - schemaName: 'schemaName', + 'tbl.object_id': 'tableId', + 'tbl.name': 'tableName', + 'sch.name': 'schemaName', }; const query = this.queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: this.schemaToTablesMap, - propertiesToSelect, - propertyNameProjection: projection, + projection, }); return { - projection, + projection: getProjectedPropertiesNames({ projection }), sql: () => query, }; } diff --git a/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js index c848203..d6a9194 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js @@ -11,23 +11,22 @@ class QueryForRetrievingTheTablesSelectedByTheUser { return `${propertyName} AS ${projectionName}`; } - #buildProjectionsForPropertiesSelectedByQuery({ propertiesToSelect, propertyNameProjection }) { - const propertiesNames = Object.keys(propertiesToSelect); + #buildProjectionsForPropertiesSelectedByQuery({ projection }) { + const initialPropertiesNames = Object.keys(projection); - return propertiesNames - .map(propertyName => + return initialPropertiesNames + .map(initialPropertyName => this.#buildProjectionForProperty({ - propertyName: propertiesToSelect[propertyName], - projectionName: propertyNameProjection[propertyName], + propertyName: initialPropertyName, + projectionName: projection[initialPropertyName], }), ) .join(','); } - queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap, propertiesToSelect, propertyNameProjection }) { + queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap, projection }) { const propertiesToSelectProjections = this.#buildProjectionsForPropertiesSelectedByQuery({ - propertiesToSelect, - propertyNameProjection, + projection, }); const predicate = Object.entries(schemaToTablesMap) .map(([schema, tables]) => this.#buildPredicateForTablesInSchema({ schema, tables })) diff --git a/reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js b/reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js new file mode 100644 index 0000000..310522b --- /dev/null +++ b/reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js @@ -0,0 +1,6 @@ +const getProjectedPropertiesNames = ({ projection }) => + Object.fromEntries(Object.values(projection).map(projectedName => [projectedName, projectedName])); + +module.exports = { + getProjectedPropertiesNames, +}; From 6da38ef2c79235725bdb2e9722dec2050d7c91d2 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Thu, 14 Nov 2024 15:50:15 +0200 Subject: [PATCH 4/8] HCK-8696: removed redundant property and join --- reverse_engineering/databaseService/databaseService.js | 5 +---- ...aseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/reverse_engineering/databaseService/databaseService.js b/reverse_engineering/databaseService/databaseService.js index 8c4f854..ab99349 100644 --- a/reverse_engineering/databaseService/databaseService.js +++ b/reverse_engineering/databaseService/databaseService.js @@ -219,21 +219,18 @@ const getDatabaseIndexes = async ({ connectionClient, tablesInfo, dbName, logger ic.column_store_order_ordinal, COL_NAME(t.${tablesSelectedByTheUser.projection.tableId}, ic.column_id) as columnName, S.name as schemaName, - p.data_compression_desc as dataCompression, ind.* FROM sys.indexes ind LEFT JOIN user_selected_tables t ON ind.object_id = t.${tablesSelectedByTheUser.projection.tableId} INNER JOIN sys.index_columns ic ON ind.object_id = ic.object_id AND ind.index_id = ic.index_id - INNER JOIN sys.partitions p - ON p.object_id = t.${tablesSelectedByTheUser.projection.tableId} AND ind.index_id = p.index_id INNER JOIN sys.objects O ON O.object_id = t.${tablesSelectedByTheUser.projection.tableId} INNER JOIN sys.schemas S ON S.schema_id = O.schema_id WHERE ind.is_primary_key = 0 AND ind.is_unique_constraint = 0 - AND t.${tablesSelectedByTheUser.projection.isMsSkipped} = 0 + AND t.${tablesSelectedByTheUser.projection.isMsShipped} = 0 `; return mapResponse(await currentDbConnectionClient.query(queryRetrievingTheIndexes)); diff --git a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js index 2d5466c..e9369e6 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js @@ -11,7 +11,7 @@ class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryF const projection = { 'tbl.object_id': 'tableId', 'tbl.name': 'tableName', - 'tbl.is_ms_shipped': 'isMsSkipped', + 'tbl.is_ms_shipped': 'isMsShipped', }; const query = this.queryForRetrievingTheTablesSelectedByTheUser({ From 97551f2453f11e0fd11d886a60760fcc4c372fb2 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Tue, 19 Nov 2024 13:03:23 +0200 Subject: [PATCH 5/8] HCK-8696: improved names for tables selected by the user builder classes --- .../databaseService/databaseService.js | 18 +++++++++--------- ...ForRetrievingTheTablesSelectedByTheUser.js} | 4 ++-- ...ForRetrievingTheTablesSelectedByTheUser.js} | 4 ++-- ...ForRetrievingTheTablesSelectedByTheUser.js} | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) rename reverse_engineering/queries/selectedTablesSubQuery/{DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js => DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js} (78%) rename reverse_engineering/queries/selectedTablesSubQuery/{MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js => MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js} (81%) rename reverse_engineering/queries/selectedTablesSubQuery/{PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js => PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js} (79%) diff --git a/reverse_engineering/databaseService/databaseService.js b/reverse_engineering/databaseService/databaseService.js index ab99349..8dc74e2 100644 --- a/reverse_engineering/databaseService/databaseService.js +++ b/reverse_engineering/databaseService/databaseService.js @@ -9,14 +9,14 @@ const { queryForRetrievingTheTablesSelectedByTheUser, } = require('../queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser'); const { - PartitionsQueryForRetrievingTheTablesSelectedByTheUser, -} = require('../queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser'); + PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser'); const { - DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser, -} = require('../queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser'); + DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser'); const { - MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser, -} = require('../queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser'); + MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser'); const QUERY_REQUEST_TIMEOUT = 60000; @@ -206,7 +206,7 @@ const getViewDistributedColumns = async (connectionClient, dbName, tableName, ta const getDatabaseIndexes = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database indexes.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser({ + const tablesSelectedByTheUser = new DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, }).getQuery(); const queryRetrievingTheIndexes = ` @@ -267,7 +267,7 @@ const getViewsIndexes = async (connectionClient, dbName) => { const getPartitions = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database partitions.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new PartitionsQueryForRetrievingTheTablesSelectedByTheUser({ + const tablesSelectedByTheUser = new PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, }).getQuery(); const queryForRetrievingThePartitions = ` @@ -314,7 +314,7 @@ const getDatabaseMemoryOptimizedTables = async ({ connectionClient, tablesInfo, try { logger.log('info', { message: `Get '${dbName}' database memory optimized indexes.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser({ + const tablesSelectedByTheUser = new MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, }).getQuery(); const queryForRetrievingMemoryOptimizedTables = ` diff --git a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js similarity index 78% rename from reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js rename to reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js index e9369e6..24b3e05 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,7 +1,7 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); -class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { +class DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { super(); this.schemaToTablesMap = schemaToTablesMap; @@ -27,5 +27,5 @@ class DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser extends QueryF } module.exports = { - DatabaseIndexesQueryForRetrievingTheTablesSelectedByTheUser, + DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser, }; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js similarity index 81% rename from reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js rename to reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js index f8db465..63b88a8 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,7 +1,7 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); -class MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { +class MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { super(); this.schemaToTablesMap = schemaToTablesMap; @@ -30,5 +30,5 @@ class MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser extends } module.exports = { - MemoryOptimizedTablesQueryForRetrievingTheTablesSelectedByTheUser, + MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser, }; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js similarity index 79% rename from reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js rename to reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js index 09bf08e..8b0a3fa 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,7 +1,7 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); -class PartitionsQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { +class PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { super(); this.schemaToTablesMap = schemaToTablesMap; @@ -27,5 +27,5 @@ class PartitionsQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRet } module.exports = { - PartitionsQueryForRetrievingTheTablesSelectedByTheUser, + PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser, }; From c6d7d18c69f62b72dd8b750f1f124483d5babc40 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Tue, 19 Nov 2024 19:32:19 +0200 Subject: [PATCH 6/8] HCK-8696: replaced inheritance with composition --- ...ForRetrievingTheTablesSelectedByTheUser.js | 6 +++--- ...ForRetrievingTheTablesSelectedByTheUser.js | 6 +++--- ...ForRetrievingTheTablesSelectedByTheUser.js | 6 +++--- ...ForRetrievingTheTablesSelectedByTheUser.js | 19 ++++--------------- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js index 24b3e05..24f3ed8 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,10 +1,10 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); -class DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { +class DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { - super(); this.schemaToTablesMap = schemaToTablesMap; + this.query = new QueryForRetrievingTheTablesSelectedByTheUser(); } getQuery() { @@ -14,7 +14,7 @@ class DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser extends Que 'tbl.is_ms_shipped': 'isMsShipped', }; - const query = this.queryForRetrievingTheTablesSelectedByTheUser({ + const query = this.query.queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: this.schemaToTablesMap, projection, }); diff --git a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js index 63b88a8..0a980e8 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,10 +1,10 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); -class MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { +class MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { - super(); this.schemaToTablesMap = schemaToTablesMap; + this.query = new QueryForRetrievingTheTablesSelectedByTheUser(); } getQuery() { @@ -17,7 +17,7 @@ class MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser exten 'tbl.is_memory_optimized': 'isMemoryOptimized', }; - const query = this.queryForRetrievingTheTablesSelectedByTheUser({ + const query = this.query.queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: this.schemaToTablesMap, projection, }); diff --git a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js index 8b0a3fa..3aafbe4 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,10 +1,10 @@ const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); -class PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser extends QueryForRetrievingTheTablesSelectedByTheUser { +class PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser { constructor({ schemaToTablesMap }) { - super(); this.schemaToTablesMap = schemaToTablesMap; + this.query = new QueryForRetrievingTheTablesSelectedByTheUser(); } getQuery() { @@ -14,7 +14,7 @@ class PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser extends QueryFor 'sch.name': 'schemaName', }; - const query = this.queryForRetrievingTheTablesSelectedByTheUser({ + const query = this.query.queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: this.schemaToTablesMap, projection, }); diff --git a/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js index d6a9194..a11738b 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js @@ -7,25 +7,14 @@ class QueryForRetrievingTheTablesSelectedByTheUser { return tables.map(table => this.#buildPredicateForTable({ schema, table })).join('OR'); } - #buildProjectionForProperty({ propertyName, projectionName }) { - return `${propertyName} AS ${projectionName}`; - } - - #buildProjectionsForPropertiesSelectedByQuery({ projection }) { - const initialPropertiesNames = Object.keys(projection); - - return initialPropertiesNames - .map(initialPropertyName => - this.#buildProjectionForProperty({ - propertyName: initialPropertyName, - projectionName: projection[initialPropertyName], - }), - ) + #buildProjection({ columnToAliasMap }) { + return Object.entries(columnToAliasMap) + .map(([column, alias]) => `${column} AS ${alias}`) .join(','); } queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap, projection }) { - const propertiesToSelectProjections = this.#buildProjectionsForPropertiesSelectedByQuery({ + const propertiesToSelectProjections = this.#buildProjection({ projection, }); const predicate = Object.entries(schemaToTablesMap) From dd4eb56501442fdf811a6318e33ad1093302c695 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Thu, 21 Nov 2024 12:12:14 +0200 Subject: [PATCH 7/8] HCK-8696: removed unrelevant data query --- .../databaseService/databaseService.js | 31 ----------------- ...ForRetrievingTheTablesSelectedByTheUser.js | 34 ------------------- .../reverseEngineeringService.js | 7 +--- 3 files changed, 1 insertion(+), 71 deletions(-) delete mode 100644 reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js diff --git a/reverse_engineering/databaseService/databaseService.js b/reverse_engineering/databaseService/databaseService.js index 8dc74e2..c0c5eb4 100644 --- a/reverse_engineering/databaseService/databaseService.js +++ b/reverse_engineering/databaseService/databaseService.js @@ -14,9 +14,6 @@ const { const { DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser, } = require('../queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser'); -const { - MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser, -} = require('../queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser'); const QUERY_REQUEST_TIMEOUT = 60000; @@ -310,33 +307,6 @@ const getTableColumnsDescription = async (connectionClient, dbName, tableName, s `); }; -const getDatabaseMemoryOptimizedTables = async ({ connectionClient, tablesInfo, dbName, logger }) => { - try { - logger.log('info', { message: `Get '${dbName}' database memory optimized indexes.` }, 'Reverse Engineering'); - const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser({ - schemaToTablesMap: tablesInfo, - }).getQuery(); - const queryForRetrievingMemoryOptimizedTables = ` - WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) - SELECT - T.${tablesSelectedByTheUser.projection.tableName}, - T.${tablesSelectedByTheUser.projection.durability}, - T.${tablesSelectedByTheUser.projection.durabilityDescription}, - OBJECT_NAME(T.${tablesSelectedByTheUser.projection.historyTableId}) AS historyTable, - SCHEMA_NAME(O.schema_id) AS historySchema, - T.${tablesSelectedByTheUser.projection.temporalTypeDescription} - FROM user_selected_tables T LEFT JOIN sys.objects O ON T.${tablesSelectedByTheUser.projection.historyTableId} = O.object_id - WHERE T.${tablesSelectedByTheUser.projection.isMemoryOptimized}=1 - `; - return mapResponse(currentDbConnectionClient.query(queryForRetrievingMemoryOptimizedTables)); - } catch (error) { - logger.log('error', { message: error.message, stack: error.stack, error }, 'Retrieve memory optimized tables'); - - return []; - } -}; - const getViewColumns = async (connectionClient, dbName, viewName, schemaName) => { const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); const objectId = `${schemaName}.${viewName}`; @@ -493,7 +463,6 @@ module.exports = { getTableForeignKeys, getDatabaseIndexes, getTableColumnsDescription, - getDatabaseMemoryOptimizedTables, getViewTableInfo, getTableKeyConstraints, getTableMaskedColumns, diff --git a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js deleted file mode 100644 index 0a980e8..0000000 --- a/reverse_engineering/queries/selectedTablesSubQuery/MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser.js +++ /dev/null @@ -1,34 +0,0 @@ -const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); -const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); - -class MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser { - constructor({ schemaToTablesMap }) { - this.schemaToTablesMap = schemaToTablesMap; - this.query = new QueryForRetrievingTheTablesSelectedByTheUser(); - } - - getQuery() { - const projection = { - 'tbl.name': 'tableName', - 'tbl.durability': 'durability', - 'tbl.durability_desc': 'durabilityDescription', - 'tbl.history_table_id': 'historyTableId', - 'tbl.temporal_type_desc': 'temporalTypeDescription', - 'tbl.is_memory_optimized': 'isMemoryOptimized', - }; - - const query = this.query.queryForRetrievingTheTablesSelectedByTheUser({ - schemaToTablesMap: this.schemaToTablesMap, - projection, - }); - - return { - projection: getProjectedPropertiesNames({ projection }), - sql: () => query, - }; - } -} - -module.exports = { - MemoryOptimizedTablesSubQueryForRetrievingTheTablesSelectedByTheUser, -}; diff --git a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js index 0d1d7c1..2c43222 100644 --- a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js +++ b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js @@ -4,7 +4,6 @@ const { getTableForeignKeys, getDatabaseIndexes, getTableColumnsDescription, - getDatabaseMemoryOptimizedTables, getViewTableInfo, getViewColumns, getTableKeyConstraints, @@ -403,13 +402,10 @@ const getPersistence = tableName => { const reverseCollectionsToJSON = logger => async (dbConnectionClient, tablesInfo, reverseEngineeringOptions) => { const dbName = dbConnectionClient.config.database; progress(logger, `RE data from database "${dbName}"`, dbName); - const [databaseIndexes, databaseMemoryOptimizedTables, databaseUDT, dataBasePartitions] = await Promise.all([ + const [databaseIndexes, databaseUDT, dataBasePartitions] = await Promise.all([ getDatabaseIndexes({ connectionClient: dbConnectionClient, tablesInfo, dbName, logger }).catch( logError(logger, 'Getting indexes'), ), - getDatabaseMemoryOptimizedTables({ connectionClient: dbConnectionClient, tablesInfo, dbName, logger }).catch( - logError(logger, 'Getting memory optimized tables'), - ), getDatabaseUserDefinedTypes({ connectionClient: dbConnectionClient, dbName, logger }).catch( logError(logger, 'Getting user defined types'), ), @@ -517,7 +513,6 @@ const reverseCollectionsToJSON = logger => async (dbConnectionClient, tablesInfo dbName: schemaName, entityLevel: { Indxs: reverseTableIndexes(tableIndexes), - ...getMemoryOptimizedOptions(databaseMemoryOptimizedTables.find(item => item.name === tableName)), ...defineFieldsCompositeKeyConstraints(fieldsKeyConstraints), indexingOrderColumn: order.map(column => ({ name: column })), tableRole: getTableRole(distribution, indexing), From e80a266f1fbf91a5b64957bde75ce83d775ced13 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Thu, 21 Nov 2024 13:05:26 +0200 Subject: [PATCH 8/8] HCK-8696: transformed redundant classes into functions --- .../databaseService/databaseService.js | 16 ++++----- ...ForRetrievingTheTablesSelectedByTheUser.js | 31 ----------------- ...ForRetrievingTheTablesSelectedByTheUser.js | 31 ----------------- ...ForRetrievingTheTablesSelectedByTheUser.js | 33 ++++++++++++++++--- ...ForRetrievingTheTablesSelectedByTheUser.js | 25 ++++++++++++++ .../getProjectedPropertiesNames.js | 4 +-- ...ForRetrievingTheTablesSelectedByTheUser.js | 25 ++++++++++++++ 7 files changed, 89 insertions(+), 76 deletions(-) delete mode 100644 reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js delete mode 100644 reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js create mode 100644 reverse_engineering/queries/selectedTablesSubQuery/databaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js create mode 100644 reverse_engineering/queries/selectedTablesSubQuery/partitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js diff --git a/reverse_engineering/databaseService/databaseService.js b/reverse_engineering/databaseService/databaseService.js index c0c5eb4..1625a25 100644 --- a/reverse_engineering/databaseService/databaseService.js +++ b/reverse_engineering/databaseService/databaseService.js @@ -9,11 +9,11 @@ const { queryForRetrievingTheTablesSelectedByTheUser, } = require('../queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser'); const { - PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser, -} = require('../queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser'); + getPartitionsSubQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/partitionsSubQueryForRetrievingTheTablesSelectedByTheUser'); const { - DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser, -} = require('../queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser'); + getDatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser, +} = require('../queries/selectedTablesSubQuery/databaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser'); const QUERY_REQUEST_TIMEOUT = 60000; @@ -203,9 +203,9 @@ const getViewDistributedColumns = async (connectionClient, dbName, tableName, ta const getDatabaseIndexes = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database indexes.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser({ + const tablesSelectedByTheUser = getDatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, - }).getQuery(); + }); const queryRetrievingTheIndexes = ` WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) SELECT @@ -264,9 +264,9 @@ const getViewsIndexes = async (connectionClient, dbName) => { const getPartitions = async ({ connectionClient, tablesInfo, dbName, logger }) => { logger.log('info', { message: `Get '${dbName}' database partitions.` }, 'Reverse Engineering'); const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName); - const tablesSelectedByTheUser = new PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser({ + const tablesSelectedByTheUser = getPartitionsSubQueryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo, - }).getQuery(); + }); const queryForRetrievingThePartitions = ` WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()}) SELECT diff --git a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js deleted file mode 100644 index 24f3ed8..0000000 --- a/reverse_engineering/queries/selectedTablesSubQuery/DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js +++ /dev/null @@ -1,31 +0,0 @@ -const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); -const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); - -class DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser { - constructor({ schemaToTablesMap }) { - this.schemaToTablesMap = schemaToTablesMap; - this.query = new QueryForRetrievingTheTablesSelectedByTheUser(); - } - - getQuery() { - const projection = { - 'tbl.object_id': 'tableId', - 'tbl.name': 'tableName', - 'tbl.is_ms_shipped': 'isMsShipped', - }; - - const query = this.query.queryForRetrievingTheTablesSelectedByTheUser({ - schemaToTablesMap: this.schemaToTablesMap, - projection, - }); - - return { - projection: getProjectedPropertiesNames({ projection }), - sql: () => query, - }; - } -} - -module.exports = { - DatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser, -}; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js deleted file mode 100644 index 3aafbe4..0000000 --- a/reverse_engineering/queries/selectedTablesSubQuery/PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js +++ /dev/null @@ -1,31 +0,0 @@ -const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); -const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); - -class PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser { - constructor({ schemaToTablesMap }) { - this.schemaToTablesMap = schemaToTablesMap; - this.query = new QueryForRetrievingTheTablesSelectedByTheUser(); - } - - getQuery() { - const projection = { - 'tbl.object_id': 'tableId', - 'tbl.name': 'tableName', - 'sch.name': 'schemaName', - }; - - const query = this.query.queryForRetrievingTheTablesSelectedByTheUser({ - schemaToTablesMap: this.schemaToTablesMap, - projection, - }); - - return { - projection: getProjectedPropertiesNames({ projection }), - sql: () => query, - }; - } -} - -module.exports = { - PartitionsSubQueryForRetrievingTheTablesSelectedByTheUser, -}; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js index a11738b..306c3ab 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js @@ -1,3 +1,11 @@ +const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); + +/** + @typedef {{ +* sql: () => string, +* projection: Record +* }} Query + */ class QueryForRetrievingTheTablesSelectedByTheUser { #buildPredicateForTable({ schema, table }) { return `(sch.name = '${schema}' AND tbl.name = '${table}')`; @@ -13,9 +21,9 @@ class QueryForRetrievingTheTablesSelectedByTheUser { .join(','); } - queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap, projection }) { - const propertiesToSelectProjections = this.#buildProjection({ - projection, + #queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap, columnToAliasMap }) { + const projection = this.#buildProjection({ + columnToAliasMap, }); const predicate = Object.entries(schemaToTablesMap) .map(([schema, tables]) => this.#buildPredicateForTablesInSchema({ schema, tables })) @@ -23,12 +31,29 @@ class QueryForRetrievingTheTablesSelectedByTheUser { const whereClause = Object.entries(schemaToTablesMap).length > 0 ? `WHERE ${predicate}` : ''; return ` SELECT - ${propertiesToSelectProjections} + ${projection} FROM sys.tables tbl JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id ${whereClause} `; } + + /** + * + * @param {{columnToAliasMap: Record, schemaToTablesMap: Record}} param + * @returns {Query} + */ + getQuery({ columnToAliasMap, schemaToTablesMap }) { + const query = this.#queryForRetrievingTheTablesSelectedByTheUser({ + schemaToTablesMap, + columnToAliasMap, + }); + + return { + projection: getProjectedPropertiesNames({ columnToAliasMap }), + sql: () => query, + }; + } } module.exports = { diff --git a/reverse_engineering/queries/selectedTablesSubQuery/databaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/databaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js new file mode 100644 index 0000000..3543971 --- /dev/null +++ b/reverse_engineering/queries/selectedTablesSubQuery/databaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -0,0 +1,25 @@ +const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); +const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); + +/** + * @typedef {import("./QueryForRetrievingTheTablesSelectedByTheUser").Query} Query + * @param {{schemaToTablesMap: Record}} param + * @returns {Query} + */ +const getDatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser = ({ schemaToTablesMap }) => { + const selectedTablesQuery = new QueryForRetrievingTheTablesSelectedByTheUser(); + const columnToAliasMap = { + 'tbl.object_id': 'tableId', + 'tbl.name': 'tableName', + 'tbl.is_ms_shipped': 'isMsShipped', + }; + + return selectedTablesQuery.getQuery({ + schemaToTablesMap, + columnToAliasMap, + }); +}; + +module.exports = { + getDatabaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser, +}; diff --git a/reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js b/reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js index 310522b..3251dc0 100644 --- a/reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js +++ b/reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js @@ -1,5 +1,5 @@ -const getProjectedPropertiesNames = ({ projection }) => - Object.fromEntries(Object.values(projection).map(projectedName => [projectedName, projectedName])); +const getProjectedPropertiesNames = ({ columnToAliasMap }) => + Object.fromEntries(Object.values(columnToAliasMap).map(projectedName => [projectedName, projectedName])); module.exports = { getProjectedPropertiesNames, diff --git a/reverse_engineering/queries/selectedTablesSubQuery/partitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js b/reverse_engineering/queries/selectedTablesSubQuery/partitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js new file mode 100644 index 0000000..1a13084 --- /dev/null +++ b/reverse_engineering/queries/selectedTablesSubQuery/partitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js @@ -0,0 +1,25 @@ +const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); +const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); + +/** + * @typedef {import("./QueryForRetrievingTheTablesSelectedByTheUser").Query} Query + * @param {{schemaToTablesMap: Record}} param + * @returns {Query} + */ +const getPartitionsSubQueryForRetrievingTheTablesSelectedByTheUser = ({ schemaToTablesMap }) => { + const selectedTablesQuery = new QueryForRetrievingTheTablesSelectedByTheUser(); + const columnToAliasMap = { + 'tbl.object_id': 'tableId', + 'tbl.name': 'tableName', + 'sch.name': 'schemaName', + }; + + return selectedTablesQuery.getQuery({ + schemaToTablesMap, + columnToAliasMap, + }); +}; + +module.exports = { + getPartitionsSubQueryForRetrievingTheTablesSelectedByTheUser, +};