Skip to content

Commit 063cf4b

Browse files
HCK-14785: RE relationships (#90)
<!--do not remove this marker, its needed to replace info when ticket title is updated --> <!--jira-description-action-hidden-marker-start--> <table> <td> <a href="https://hackolade.atlassian.net/browse/HCK-14785" title="HCK-14785" target="_blank"><img alt="Sub-bug" src="https://hackolade.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium" />HCK-14785</a> [DB2][RE from instance] relationships aren't REed from instance if schema name length is less than 8 </td></table> <br /> <!--jira-description-action-hidden-marker-end--> ## Content: * normalized schema names in DDL statements on RE. ## Technical details * The PR fixes the behavior of `CALL SYSPROC.DB2LK_GENERATE_DDL`, where the schema names are right padded by default. For examples: * schema name in the model is `x` * after applying it to the instance, trying to reverse it back as DDL will return: ```sql -- "x"."t" definition CREATE TABLE "x "."t" ( "c1" VARCHAR(20 OCTETS) ) IN "IBMDB2SAMPLEREL" ORGANIZE BY ROW; ``` * This happens because the schema name was created as a fixed-length `CHAR` value (default `8`) and is blank-padded internally to full length. * This impacts the internal resolution of references on most levels (relationships, types, columns, views, etc.)
1 parent d8c3b3a commit 063cf4b

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

shared/helpers/instanceHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const getTableDdl = async ({ connection, schemaName, tableName, objectType, logg
9191

9292
await connection.execute({ query: clearQuery, callable: true, inparam: opToken });
9393

94-
return ddlResult.map(row => queryHelper.ensureTerminator({ query: row.SQL_STMT })).join('\n');
94+
return ddlResult.map(queryHelper.postProcessDdlStatement).join('\n');
9595
} catch (error) {
9696
logger.error(error);
9797

shared/helpers/queryHelper.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ const cleanUpQuery = ({ query = '' }) => query.replaceAll(/\s+/g, ' ');
88

99
const ensureTerminator = ({ query = '' }) => (query.trimEnd().endsWith(';') ? query : `${query};`);
1010

11+
/**
12+
* finds every quoted identifier and removes *only* trailing whitespace
13+
* @param {{ query: string }} params
14+
* @returns {string}
15+
*/
16+
const normalizeDb2Identifiers = ({ query = '' }) => {
17+
return query.replaceAll(/"([^"]*)"/g, (_, name) => `"${name.trimEnd()}"`);
18+
};
19+
1120
/**
1221
* @param {{ query: string, schemaNameKeyword: string }} params
1322
* @returns {string}
@@ -109,6 +118,16 @@ const getClearTableDdlQuery = () => {
109118
return 'CALL SYSPROC.DB2LK_CLEAN_TABLE(?);';
110119
};
111120

121+
/**
122+
* @param {{ SQL_STMT: string}} row - select query ddl result
123+
* @returns {string}
124+
*/
125+
const postProcessDdlStatement = row => {
126+
let statement = queryHelper.ensureTerminator({ query: row.SQL_STMT });
127+
statement = queryHelper.normalizeDb2Identifiers({ query: statement });
128+
return statement;
129+
};
130+
112131
const queryHelper = {
113132
cleanUpQuery,
114133
getDbVersionQuery,
@@ -119,6 +138,8 @@ const queryHelper = {
119138
getSelectTableDdlQuery,
120139
getClearTableDdlQuery,
121140
ensureTerminator,
141+
normalizeDb2Identifiers,
142+
postProcessDdlStatement,
122143
};
123144

124145
module.exports = {

0 commit comments

Comments
 (0)