-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hck 8696 fine tune the other sql queries that are executed by the syn…
…apse plugin during the re process (#91) * HCK-8696: optimized indexes query * HCK-8696: optimized indexes and memory optimized tables queries * HCK-8696: refactored projection approach * HCK-8696: removed redundant property and join * HCK-8696: improved names for tables selected by the user builder classes * HCK-8696: replaced inheritance with composition * HCK-8696: removed unrelevant data query * HCK-8696: transformed redundant classes into functions --------- Co-authored-by: Thomas Jakemeyn <thomas.jakemeyn@gmail.com>
- Loading branch information
1 parent
935df71
commit faedb33
Showing
7 changed files
with
160 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
reverse_engineering/queries/queryForRetrievingTheTablesSelectedByTheUser.js
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
...ngineering/queries/selectedTablesSubQuery/QueryForRetrievingTheTablesSelectedByTheUser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); | ||
|
||
/** | ||
@typedef {{ | ||
* sql: () => string, | ||
* projection: Record<string, string> | ||
* }} Query | ||
*/ | ||
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'); | ||
} | ||
|
||
#buildProjection({ columnToAliasMap }) { | ||
return Object.entries(columnToAliasMap) | ||
.map(([column, alias]) => `${column} AS ${alias}`) | ||
.join(','); | ||
} | ||
|
||
#queryForRetrievingTheTablesSelectedByTheUser({ schemaToTablesMap, columnToAliasMap }) { | ||
const projection = this.#buildProjection({ | ||
columnToAliasMap, | ||
}); | ||
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 | ||
${projection} | ||
FROM sys.tables tbl | ||
JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id | ||
${whereClause} | ||
`; | ||
} | ||
|
||
/** | ||
* | ||
* @param {{columnToAliasMap: Record<string, string>, schemaToTablesMap: Record<string, string[]>}} param | ||
* @returns {Query} | ||
*/ | ||
getQuery({ columnToAliasMap, schemaToTablesMap }) { | ||
const query = this.#queryForRetrievingTheTablesSelectedByTheUser({ | ||
schemaToTablesMap, | ||
columnToAliasMap, | ||
}); | ||
|
||
return { | ||
projection: getProjectedPropertiesNames({ columnToAliasMap }), | ||
sql: () => query, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
QueryForRetrievingTheTablesSelectedByTheUser, | ||
}; |
25 changes: 25 additions & 0 deletions
25
.../selectedTablesSubQuery/databaseIndexesSubQueryForRetrievingTheTablesSelectedByTheUser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); | ||
const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); | ||
|
||
/** | ||
* @typedef {import("./QueryForRetrievingTheTablesSelectedByTheUser").Query} Query | ||
* @param {{schemaToTablesMap: Record<string, string[]>}} 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, | ||
}; |
6 changes: 6 additions & 0 deletions
6
reverse_engineering/queries/selectedTablesSubQuery/getProjectedPropertiesNames.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const getProjectedPropertiesNames = ({ columnToAliasMap }) => | ||
Object.fromEntries(Object.values(columnToAliasMap).map(projectedName => [projectedName, projectedName])); | ||
|
||
module.exports = { | ||
getProjectedPropertiesNames, | ||
}; |
25 changes: 25 additions & 0 deletions
25
...eries/selectedTablesSubQuery/partitionsSubQueryForRetrievingTheTablesSelectedByTheUser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { QueryForRetrievingTheTablesSelectedByTheUser } = require('./QueryForRetrievingTheTablesSelectedByTheUser'); | ||
const { getProjectedPropertiesNames } = require('./getProjectedPropertiesNames'); | ||
|
||
/** | ||
* @typedef {import("./QueryForRetrievingTheTablesSelectedByTheUser").Query} Query | ||
* @param {{schemaToTablesMap: Record<string, string[]>}} 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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters