From 76867db8a1724b18aa4f2b87683902627e0699e7 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko <91055067+serhii-filonenko@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:23:16 +0300 Subject: [PATCH] HCK-8001: remove trailing comma before last deactivated column (#26) * HCK-8001: remove trailing comma before last deactivated column * Revert "HCK-8001: remove trailing comma before last deactivated column" This reverts commit 219a67c16ef129a7d2db8931c3960c98bd00c7bf. * HCK-8001: remove trailing comma before last deactivated column * HCK-8001: add comma commenting for last activated column statement --- .../ddlProvider/ddlProvider.js | 4 +- .../joinActivatedAndDeactivatedStatements.js | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 forward_engineering/utils/joinActivatedAndDeactivatedStatements.js diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 24e6669..3f1360a 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -20,6 +20,7 @@ module.exports = (baseProvider, options, app) => { getViewData, } = require('../utils/general')(_); const assignTemplates = require('../utils/assignTemplates'); + const { joinActivatedAndDeactivatedStatements } = require('../utils/joinActivatedAndDeactivatedStatements'); const { generateConstraintsString, @@ -190,12 +191,13 @@ module.exports = (baseProvider, options, app) => { partitionOf && !keyConstraintsValue && !checkConstraintsValue && !foreignKeyConstraintsString; const openParenthesis = isEmptyPartitionBody ? '' : '('; const closeParenthesis = isEmptyPartitionBody ? '' : ')'; + const columnStatements = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' }); const tableStatement = assignTemplates(template, { temporary: getTableTemporaryValue(temporary, unlogged), ifNotExist: ifNotExistStr, name: tableName, - columnDefinitions: !partitionOf ? '\t' + _.join(columns, ',\n\t') : '', + columnDefinitions: !partitionOf ? '\t' + columnStatements : '', keyConstraints: keyConstraintsValue, checkConstraints: checkConstraintsValue, foreignKeyConstraints: foreignKeyConstraintsString, diff --git a/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js b/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js new file mode 100644 index 0000000..440e120 --- /dev/null +++ b/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js @@ -0,0 +1,53 @@ +/** + * @param {{ + * index: number; + * numberOfStatements: number; + * lastIndexOfActivatedStatement: number; + * delimiter: string; + * }} + * @return {string} + * */ +const getDelimiter = ({ index, numberOfStatements, lastIndexOfActivatedStatement, delimiter }) => { + const isLastStatement = index === numberOfStatements - 1; + const isLastActivatedStatement = index === lastIndexOfActivatedStatement; + + if (isLastStatement) { + return ''; + } + + if (isLastActivatedStatement) { + return ' --' + delimiter; + } + + return delimiter; +}; + +/** + * @param {{ + * statements?: string[]; + * delimiter?: string; + * indent?: string; + * }} + * @return {string} + * */ +const joinActivatedAndDeactivatedStatements = ({ statements = [], delimiter = ',', indent = '\n' }) => { + const lastIndexOfActivatedStatement = statements.findLastIndex(statement => !statement.startsWith('--')); + const numberOfStatements = statements.length; + + return statements + .map((statement, index) => { + const currentDelimiter = getDelimiter({ + index, + numberOfStatements, + lastIndexOfActivatedStatement, + delimiter, + }); + + return statement + currentDelimiter; + }) + .join(indent); +}; + +module.exports = { + joinActivatedAndDeactivatedStatements, +};