Skip to content

Commit df0a482

Browse files
HCK-8697: Improve RE of partitions (#88)
* HCK-8697: added improed query draft * Created module for reusable query * Added missing line break * Applied reusable nested query * Added missing argument * Fixed formatting * Fixed formatting * Fixed formatting * HCK-8697: handle no items selection case --------- Co-authored-by: Thomas Jakemeyn <[email protected]>
1 parent 11c81f8 commit df0a482

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

reverse_engineering/databaseService/databaseService.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const { getObjectsFromDatabase, getNewConnectionClientByDb } = require('./helper
55
const getSampleDocSize = require('../helpers/getSampleDocSize');
66
const { logAuthTokenInfo } = require('../helpers/logInfo');
77
const { getConnection } = require('./helpers/connection');
8+
const {
9+
queryForRetrievingTheTablesSelectedByTheUser,
10+
} = require('../queries/queryForRetrievingTheTablesSelectedByTheUser');
811

912
const QUERY_REQUEST_TIMEOUT = 60000;
1013

@@ -253,32 +256,31 @@ const getViewsIndexes = async (connectionClient, dbName) => {
253256
);
254257
};
255258

256-
const getPartitions = async (connectionClient, dbName, logger) => {
257-
const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName);
258-
259+
const getPartitions = async ({ connectionClient, tablesInfo, dbName, logger }) => {
259260
logger.log('info', { message: `Get '${dbName}' database partitions.` }, 'Reverse Engineering');
260-
261-
return mapResponse(
262-
await currentDbConnectionClient.query`
263-
SELECT
264-
sch.name AS schemaName,
265-
tbl.name AS tableName,
261+
const currentDbConnectionClient = await getNewConnectionClientByDb(connectionClient, dbName);
262+
const tablesSelectedByTheUser = queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap: tablesInfo });
263+
const queryForRetrievingThePartitions = `
264+
WITH user_selected_tables AS (${tablesSelectedByTheUser.sql()})
265+
SELECT
266+
tbl.${tablesSelectedByTheUser.projection.schemaName} AS schemaName,
267+
tbl.${tablesSelectedByTheUser.projection.tableName} AS tableName,
266268
prt.partition_number,
267269
pf.boundary_value_on_right AS range,
268270
c.name AS name,
269271
rng.value AS value
270-
FROM sys.schemas sch
271-
INNER JOIN sys.tables tbl ON sch.schema_id = tbl.schema_id
272-
INNER JOIN sys.partitions prt ON prt.object_id = tbl.object_id
272+
FROM user_selected_tables tbl
273+
INNER JOIN sys.partitions prt ON prt.object_id = tbl.${tablesSelectedByTheUser.projection.tableId}
273274
INNER JOIN sys.indexes idx ON prt.object_id = idx.object_id AND prt.index_id = idx.index_id
274275
INNER JOIN sys.data_spaces ds ON idx.data_space_id = ds.[data_space_id]
275276
INNER JOIN sys.partition_schemes ps ON ds.data_space_id = ps.data_space_id
276277
INNER JOIN sys.partition_functions pf ON ps.function_id = pf.function_id
277278
INNER JOIN sys.index_columns ic ON ic.object_id = idx.object_id AND ic.index_id = idx.index_id AND ic.partition_ordinal >= 1
278-
INNER JOIN sys.columns c ON tbl.object_id = c.object_id AND ic.column_id = c.column_id
279+
INNER JOIN sys.columns c ON tbl.${tablesSelectedByTheUser.projection.tableId} = c.object_id AND ic.column_id = c.column_id
279280
LEFT JOIN sys.partition_range_values rng ON pf.function_id = rng.function_id AND rng.boundary_id = prt.partition_number
280-
`,
281-
);
281+
`;
282+
283+
return mapResponse(await currentDbConnectionClient.query(queryForRetrievingThePartitions));
282284
};
283285

284286
const getTableColumnsDescription = async (connectionClient, dbName, tableName, schemaName) => {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function buildPredicateForTable({ schema, table }) {
2+
return `(sch.name = '${schema}' AND tbl.name = '${table}')`;
3+
}
4+
5+
function buildPredicateForTablesInSchema({ schema, tables }) {
6+
return tables.map(table => buildPredicateForTable({ schema, table })).join('OR');
7+
}
8+
9+
function queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap }) {
10+
const projection = {
11+
tableId: 'tableId',
12+
tableName: 'tableName',
13+
schemaName: 'schemaName',
14+
};
15+
const predicate = Object.entries(schemaToTablesMap)
16+
.map(([schema, tables]) => buildPredicateForTablesInSchema({ schema, tables }))
17+
.join('OR');
18+
const whereClause = Object.entries(schemaToTablesMap).length > 0 ? `WHERE ${predicate}` : '';
19+
const sql = `
20+
SELECT
21+
tbl.object_id AS ${projection.tableId}
22+
, tbl.name AS ${projection.tableName}
23+
, sch.name AS ${projection.schemaName}
24+
FROM sys.tables tbl
25+
JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id
26+
${whereClause}`;
27+
return {
28+
projection,
29+
sql: () => sql,
30+
};
31+
}
32+
33+
module.exports = { queryForRetrievingTheTablesSelectedByTheUser };

reverse_engineering/reverseEngineeringService/reverseEngineeringService.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ const reverseCollectionsToJSON = logger => async (dbConnectionClient, tablesInfo
411411
getDatabaseUserDefinedTypes(dbConnectionClient, dbName, logger).catch(
412412
logError(logger, 'Getting user defined types'),
413413
),
414-
getPartitions(dbConnectionClient, dbName, logger),
414+
getPartitions({ connectionClient: dbConnectionClient, tablesInfo, dbName, logger }).catch(
415+
logError(logger, 'Getting partitions'),
416+
),
415417
]);
416418

417419
return await Object.entries(tablesInfo).reduce(async (jsonSchemas, [schemaName, tableNames]) => {

0 commit comments

Comments
 (0)