From 7784d51c8ae844974961933f529eedaed6a7c2fd Mon Sep 17 00:00:00 2001 From: Serhii Filonenko <91055067+serhii-filonenko@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:27:50 +0300 Subject: [PATCH] HCK-8008: add comma commenting for last activated column statement (#35) --- forward_engineering/ddlProvider.js | 8 +-- .../joinActivatedAndDeactivatedStatements.js | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 forward_engineering/utils/joinActivatedAndDeactivatedStatements.js diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index 631abac..02ad90a 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -3,6 +3,7 @@ const defaultTypes = require('./configs/defaultTypes'); const types = require('./configs/types'); const getAdditionalOptions = require('./helpers/getAdditionalOptions'); const dropStatementProxy = require('./helpers/dropStatementProxy'); +const { joinActivatedAndDeactivatedStatements } = require('./utils/joinActivatedAndDeactivatedStatements'); /** * @param {BaseProvider} baseProvider @@ -280,6 +281,7 @@ module.exports = (baseProvider, options, app) => { const dividedForeignKeys = divideIntoActivatedAndDeactivated(foreignKeyConstraints, key => key.statement); const foreignKeyConstraintsString = generateConstraintsString(dividedForeignKeys, isActivated); + const columnStatements = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t\t' }); if (tableOptions.FOREIGN_TABLE && tableOptions.USING?.location) { const usingOptions = getUsingOptions(tableOptions.USING); @@ -290,7 +292,7 @@ module.exports = (baseProvider, options, app) => { usingOptions, tableIndexes, tableOptions: preparedTableOptions, - column_definitions: columns.join(',\n\t\t'), + column_definitions: columnStatements, keyConstraints: keyConstraintsString, foreignKeyConstraints: foreignKeyConstraintsString, checkConstraints: checkConstraintsStatement, @@ -321,7 +323,7 @@ module.exports = (baseProvider, options, app) => { temporary: tableOptions.TEMPORARY_VOLATILE ? ` ${tableOptions.TEMPORARY_VOLATILE}` : '', name: tableName, tableOptions: preparedTableOptions, - column_definitions: columns.join(',\n\t\t'), + column_definitions: columnStatements, keyConstraints: keyConstraintsString, foreignKeyConstraints: foreignKeyConstraintsString, checkConstraints: checkConstraintsStatement, @@ -343,7 +345,7 @@ module.exports = (baseProvider, options, app) => { tableSet: tableOptions.SET_MULTISET ? ` ${tableOptions.SET_MULTISET}` : '', temporary: tableOptions.TEMPORARY_VOLATILE ? ` ${tableOptions.TEMPORARY_VOLATILE}` : '', traceTable: tableOptions.TRACE_TABLE ? ` TRACE` : '', - column_definitions: columns.join(',\n\t\t'), + column_definitions: columnStatements, keyConstraints: keyConstraintsString, foreignKeyConstraints: foreignKeyConstraintsString, checkConstraints: checkConstraintsStatement, diff --git a/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js b/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js new file mode 100644 index 0000000..02f0470 --- /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, +};