From fc44fbc39560b3fb3b52ffbdbac4fe851e874b74 Mon Sep 17 00:00:00 2001 From: yevhenii-moroziuk Date: Fri, 22 Nov 2024 14:50:23 +0200 Subject: [PATCH 1/7] HCK-7856: Include column_list to a view --- forward_engineering/helpers/viewHelper.js | 26 +++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/forward_engineering/helpers/viewHelper.js b/forward_engineering/helpers/viewHelper.js index ec85bf1..056a074 100644 --- a/forward_engineering/helpers/viewHelper.js +++ b/forward_engineering/helpers/viewHelper.js @@ -85,6 +85,26 @@ function joinColumnNames(statements) { .join('\n'); } +function getDefaultColumnList(properties) { + return Object.entries(properties) + .reduce((columnList, [name, property]) => { + columnList.push({ + name: `${prepareName(name)}`, + comment: property.description, + isActivated: property.isActivated, + }); + + return columnList; + }, []) + .map(({ name, comment, isActivated }) => { + return commentDeactivatedStatement( + `${name} ${comment ? `COMMENT '${encodeStringLiteral(comment)}'` : ''}`, + isActivated, + ); + }) + .join(',\n'); +} + module.exports = { getViewScript({ _, schema, viewData, containerData, collectionRefsDefinitionsMap }) { let script = []; @@ -116,12 +136,14 @@ module.exports = { tablePropertyStatements = ` TBLPROPERTIES (${getTablePropertiesClause(_)(tableProperties)})`; } script.push(createStatement); + const columnList = view.columnList ? ` (${view.columnList})` : ` (${getDefaultColumnList(columns)})`; if (schema.selectStatement) { - const columnList = view.columnList ? ` (${view.columnList})` : ' '; return ( createStatement + - `${columnList} ${comment ? " COMMENT '" + encodeStringLiteral(comment) + "'" : ''} ${tablePropertyStatements} AS ${schema.selectStatement};\n\n${viewUnityTagsStatements}` + `${columnList} ${comment ? `COMMENT '${encodeStringLiteral(comment)}'` : ''} ${tablePropertyStatements} AS ${schema.selectStatement};\n\n${viewUnityTagsStatements}` ); + } else { + script.push(columnList); } if (_.isEmpty(columns)) { From 3db00ea02a14eb9244c7ac34fce09689ad546ea1 Mon Sep 17 00:00:00 2001 From: yevhenii-moroziuk Date: Fri, 22 Nov 2024 17:29:48 +0200 Subject: [PATCH 2/7] HCK-7856: Update comment statement --- forward_engineering/helpers/viewHelper.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/forward_engineering/helpers/viewHelper.js b/forward_engineering/helpers/viewHelper.js index 056a074..e0b6cd2 100644 --- a/forward_engineering/helpers/viewHelper.js +++ b/forward_engineering/helpers/viewHelper.js @@ -85,6 +85,10 @@ function joinColumnNames(statements) { .join('\n'); } +function getCommentStatement(comment) { + return comment ? `COMMENT '${encodeStringLiteral(comment)}'` : ''; +} + function getDefaultColumnList(properties) { return Object.entries(properties) .reduce((columnList, [name, property]) => { @@ -97,10 +101,7 @@ function getDefaultColumnList(properties) { return columnList; }, []) .map(({ name, comment, isActivated }) => { - return commentDeactivatedStatement( - `${name} ${comment ? `COMMENT '${encodeStringLiteral(comment)}'` : ''}`, - isActivated, - ); + return commentDeactivatedStatement(`${name} ${getCommentStatement(comment)}`, isActivated); }) .join(',\n'); } @@ -140,7 +141,7 @@ module.exports = { if (schema.selectStatement) { return ( createStatement + - `${columnList} ${comment ? `COMMENT '${encodeStringLiteral(comment)}'` : ''} ${tablePropertyStatements} AS ${schema.selectStatement};\n\n${viewUnityTagsStatements}` + `${columnList} ${getCommentStatement(comment)} ${tablePropertyStatements} AS ${schema.selectStatement};\n\n${viewUnityTagsStatements}` ); } else { script.push(columnList); @@ -151,7 +152,7 @@ module.exports = { } if (comment) { - script.push(`COMMENT '${encodeStringLiteral(comment)}'`); + script.push(getCommentStatement(comment)); } if (tablePropertyStatements) { From 9618dd1987b1e59ab237bcdd3fa94372e9a3f908 Mon Sep 17 00:00:00 2001 From: yevhenii-moroziuk Date: Mon, 25 Nov 2024 11:05:47 +0200 Subject: [PATCH 3/7] HCK-7856: refactor getViewScript --- .../ddlProvider/ddlTemplates.js | 2 + forward_engineering/helpers/viewHelper.js | 90 ++++++++----------- forward_engineering/utils/assignTemplates.js | 15 ++++ 3 files changed, 55 insertions(+), 52 deletions(-) create mode 100644 forward_engineering/utils/assignTemplates.js diff --git a/forward_engineering/ddlProvider/ddlTemplates.js b/forward_engineering/ddlProvider/ddlTemplates.js index 9e0d202..83c9f40 100644 --- a/forward_engineering/ddlProvider/ddlTemplates.js +++ b/forward_engineering/ddlProvider/ddlTemplates.js @@ -1,4 +1,6 @@ module.exports = { + createView: + 'CREATE${orReplace}${global}${temporary} VIEW${ifNotExists} ${name}${columnList}${schemaBinding}${comment}${tablePropertyStatements}${query};${viewUnityTagsStatements}', dropView: 'DROP VIEW IF EXISTS ${name};', alterViewName: 'ALTER VIEW ${oldName} RENAME TO ${newName};', diff --git a/forward_engineering/helpers/viewHelper.js b/forward_engineering/helpers/viewHelper.js index e0b6cd2..8c4084b 100644 --- a/forward_engineering/helpers/viewHelper.js +++ b/forward_engineering/helpers/viewHelper.js @@ -3,6 +3,8 @@ const { prepareName, encodeStringLiteral, commentDeactivatedStatement } = require('../utils/general'); const { getTablePropertiesClause } = require('./tableHelper'); const { getViewTagsStatement } = require('./unityTagsHelper'); +const ddlTemplates = require('../ddlProvider/ddlTemplates'); +const assignTemplates = require('../utils/assignTemplates'); const getColumnNames = _ => (collectionRefsDefinitionsMap, columns) => { return _.uniq( @@ -86,11 +88,11 @@ function joinColumnNames(statements) { } function getCommentStatement(comment) { - return comment ? `COMMENT '${encodeStringLiteral(comment)}'` : ''; + return comment ? `\nCOMMENT '${encodeStringLiteral(comment)}'` : ''; } function getDefaultColumnList(properties) { - return Object.entries(properties) + const list = Object.entries(properties) .reduce((columnList, [name, property]) => { columnList.push({ name: `${prepareName(name)}`, @@ -104,11 +106,23 @@ function getDefaultColumnList(properties) { return commentDeactivatedStatement(`${name} ${getCommentStatement(comment)}`, isActivated); }) .join(',\n'); + + return list ? `\n(${list})` : ''; +} + +function getTableSelectStatement({ _, collectionRefsDefinitionsMap, columns }) { + const fromStatement = getFromStatement(_)(collectionRefsDefinitionsMap, columns); + const columnsNames = getColumnNames(_)(collectionRefsDefinitionsMap, columns); + + if (fromStatement && columnsNames?.length) { + return `\nAS SELECT ${joinColumnNames(columnsNames)}\n${fromStatement}`; + } + + return ''; } module.exports = { getViewScript({ _, schema, viewData, containerData, collectionRefsDefinitionsMap }) { - let script = []; const columns = schema.properties || {}; const view = _.first(viewData) || {}; @@ -123,9 +137,6 @@ module.exports = { const orReplace = schema.viewOrReplace; const ifNotExists = view.viewIfNotExist; const name = bucketName ? `${bucketName}.${viewName}` : `${viewName}`; - const createStatement = `CREATE ${orReplace && !ifNotExists ? 'OR REPLACE ' : ''}${isGlobal ? 'GLOBAL ' : ''}${isTemporary ? 'TEMPORARY ' : ''}VIEW${ifNotExists ? ' IF NOT EXISTS' : ''} ${name}`; - const comment = schema.description; - let tablePropertyStatements = ''; const tableProperties = schema.tableProperties && Array.isArray(schema.tableProperties) ? filterRedundantProperties(schema.tableProperties, ['transient_lastDdlTime']) @@ -133,52 +144,27 @@ module.exports = { const viewUnityTagsStatements = schema.unityViewTags && getViewTagsStatement({ viewSchema: schema, viewName: name }); - if (tableProperties.length) { - tablePropertyStatements = ` TBLPROPERTIES (${getTablePropertiesClause(_)(tableProperties)})`; - } - script.push(createStatement); - const columnList = view.columnList ? ` (${view.columnList})` : ` (${getDefaultColumnList(columns)})`; - if (schema.selectStatement) { - return ( - createStatement + - `${columnList} ${getCommentStatement(comment)} ${tablePropertyStatements} AS ${schema.selectStatement};\n\n${viewUnityTagsStatements}` - ); - } else { - script.push(columnList); - } - - if (_.isEmpty(columns)) { - return; - } - - if (comment) { - script.push(getCommentStatement(comment)); - } - - if (tablePropertyStatements) { - script.push(tablePropertyStatements); - } - - if (!_.isEmpty(columns)) { - const fromStatement = getFromStatement(_)(collectionRefsDefinitionsMap, columns); - const columnsNames = getColumnNames(_)(collectionRefsDefinitionsMap, columns); - - if (fromStatement && columnsNames?.length) { - script.push(`AS SELECT ${joinColumnNames(columnsNames)}`); - script.push(fromStatement); - } else { - return; - } - } - - if (viewUnityTagsStatements) { - script.push(';\n'); - script.push(viewUnityTagsStatements); - - return script.join('\n '); - } - - return script.join('\n ') + ';\n\n\n\n\n'; + return assignTemplates(ddlTemplates.createView, { + orReplace: orReplace && !ifNotExists ? ' OR REPLACE ' : '', + global: isGlobal ? 'GLOBAL ' : '', + temporary: isTemporary ? 'TEMPORARY ' : '', + ifNotExists: ifNotExists ? ' IF NOT EXISTS' : '', + name, + columnList: view.columnList ? `\n(${view.columnList})` : getDefaultColumnList(columns), + schemaBinding: '', + comment: getCommentStatement(schema.description), + tablePropertyStatements: tableProperties.length + ? `\nTBLPROPERTIES (${getTablePropertiesClause(_)(tableProperties)})` + : '', + query: schema.selectStatement + ? `\nAS ${schema.selectStatement}` + : getTableSelectStatement({ + _, + collectionRefsDefinitionsMap, + columns, + }), + viewUnityTagsStatements: viewUnityTagsStatements ? `\n${viewUnityTagsStatements};` : '', + }); }, }; diff --git a/forward_engineering/utils/assignTemplates.js b/forward_engineering/utils/assignTemplates.js new file mode 100644 index 0000000..4ae512e --- /dev/null +++ b/forward_engineering/utils/assignTemplates.js @@ -0,0 +1,15 @@ +const template = (modifiers = '') => new RegExp('\\$\\{(.*?)}', modifiers); +const getAllTemplates = str => str.match(template('gi')) || []; +const parseTemplate = str => (str.match(template('i')) || [])[1]; + +const assignTemplates = (str, templates) => { + return getAllTemplates(str).reduce((result, item) => { + const templateName = parseTemplate(item); + + return result.replace(item, () => { + return templates[templateName] || templates[templateName] === 0 ? templates[templateName] : ''; + }); + }, str); +}; + +module.exports = assignTemplates; From b0197c8b48b4ced4304244527add26b7276b98e1 Mon Sep 17 00:00:00 2001 From: yevhenii-moroziuk Date: Mon, 25 Nov 2024 11:26:23 +0200 Subject: [PATCH 4/7] HCK-7856: Fix the issue with wrong comment placement --- forward_engineering/helpers/viewHelper.js | 2 +- forward_engineering/utils/general.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/forward_engineering/helpers/viewHelper.js b/forward_engineering/helpers/viewHelper.js index 8c4084b..4194082 100644 --- a/forward_engineering/helpers/viewHelper.js +++ b/forward_engineering/helpers/viewHelper.js @@ -88,7 +88,7 @@ function joinColumnNames(statements) { } function getCommentStatement(comment) { - return comment ? `\nCOMMENT '${encodeStringLiteral(comment)}'` : ''; + return comment ? `COMMENT '${encodeStringLiteral(comment)}'` : ''; } function getDefaultColumnList(properties) { diff --git a/forward_engineering/utils/general.js b/forward_engineering/utils/general.js index 0695438..fc392b3 100644 --- a/forward_engineering/utils/general.js +++ b/forward_engineering/utils/general.js @@ -189,7 +189,9 @@ const wrapInBrackets = (str = '') => { const buildScript = statements => { const nonEmptyScripts = statements.filter(statement => statement); const formattedScripts = nonEmptyScripts.map(script => - sqlFormatter.format(script, { indent: ' ', linesBetweenQueries: 2 }).replace(/\{ \{ (.+?) } }/g, '{{$1}}'), + sqlFormatter + .format(script, { indent: ' ', linesBetweenQueries: 2, reservedWordCase: 'upper' }) + .replace(/\{ \{ (.+?) } }/g, '{{$1}}'), ); return formattedScripts.join('\n\n') + '\n\n'; From cf99022bee10477f7bfcf2f88af0b6e3a5aff550 Mon Sep 17 00:00:00 2001 From: yevhenii-moroziuk Date: Mon, 25 Nov 2024 12:26:12 +0200 Subject: [PATCH 5/7] HCK-7856: Restore formatter settings --- forward_engineering/utils/general.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/forward_engineering/utils/general.js b/forward_engineering/utils/general.js index fc392b3..0695438 100644 --- a/forward_engineering/utils/general.js +++ b/forward_engineering/utils/general.js @@ -189,9 +189,7 @@ const wrapInBrackets = (str = '') => { const buildScript = statements => { const nonEmptyScripts = statements.filter(statement => statement); const formattedScripts = nonEmptyScripts.map(script => - sqlFormatter - .format(script, { indent: ' ', linesBetweenQueries: 2, reservedWordCase: 'upper' }) - .replace(/\{ \{ (.+?) } }/g, '{{$1}}'), + sqlFormatter.format(script, { indent: ' ', linesBetweenQueries: 2 }).replace(/\{ \{ (.+?) } }/g, '{{$1}}'), ); return formattedScripts.join('\n\n') + '\n\n'; From c480c37ba4ac79b097b435948190217cf417b5a6 Mon Sep 17 00:00:00 2001 From: yevhenii-moroziuk Date: Mon, 25 Nov 2024 12:30:29 +0200 Subject: [PATCH 6/7] HCK-7856: Fix template --- forward_engineering/ddlProvider/ddlTemplates.js | 2 +- forward_engineering/helpers/viewHelper.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/forward_engineering/ddlProvider/ddlTemplates.js b/forward_engineering/ddlProvider/ddlTemplates.js index 83c9f40..8d3d5a2 100644 --- a/forward_engineering/ddlProvider/ddlTemplates.js +++ b/forward_engineering/ddlProvider/ddlTemplates.js @@ -1,6 +1,6 @@ module.exports = { createView: - 'CREATE${orReplace}${global}${temporary} VIEW${ifNotExists} ${name}${columnList}${schemaBinding}${comment}${tablePropertyStatements}${query};${viewUnityTagsStatements}', + 'CREATE ${orReplace}${global}${temporary} VIEW${ifNotExists} ${name}${columnList}${schemaBinding}${comment}${tablePropertyStatements}${query};${viewUnityTagsStatements}', dropView: 'DROP VIEW IF EXISTS ${name};', alterViewName: 'ALTER VIEW ${oldName} RENAME TO ${newName};', diff --git a/forward_engineering/helpers/viewHelper.js b/forward_engineering/helpers/viewHelper.js index 4194082..b6bb055 100644 --- a/forward_engineering/helpers/viewHelper.js +++ b/forward_engineering/helpers/viewHelper.js @@ -145,7 +145,7 @@ module.exports = { schema.unityViewTags && getViewTagsStatement({ viewSchema: schema, viewName: name }); return assignTemplates(ddlTemplates.createView, { - orReplace: orReplace && !ifNotExists ? ' OR REPLACE ' : '', + orReplace: orReplace && !ifNotExists ? 'OR REPLACE ' : '', global: isGlobal ? 'GLOBAL ' : '', temporary: isTemporary ? 'TEMPORARY ' : '', ifNotExists: ifNotExists ? ' IF NOT EXISTS' : '', From 845c6b834a3ee44f704747f450d0f4b5bb099371 Mon Sep 17 00:00:00 2001 From: yevhenii-moroziuk Date: Mon, 25 Nov 2024 14:36:48 +0200 Subject: [PATCH 7/7] HCK-7856: Improve formatting --- forward_engineering/helpers/viewHelper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forward_engineering/helpers/viewHelper.js b/forward_engineering/helpers/viewHelper.js index b6bb055..29588b3 100644 --- a/forward_engineering/helpers/viewHelper.js +++ b/forward_engineering/helpers/viewHelper.js @@ -107,7 +107,7 @@ function getDefaultColumnList(properties) { }) .join(',\n'); - return list ? `\n(${list})` : ''; + return list ? `\n(${list}\n)` : ''; } function getTableSelectStatement({ _, collectionRefsDefinitionsMap, columns }) {