diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 4d11a5c..b91af1d 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -2,6 +2,7 @@ const defaultTypes = require('../configs/defaultTypes'); const descriptors = require('../configs/descriptors'); const templates = require('./templates'); const { Sequence } = require('../types/schemaSequenceTypes'); +const { joinActivatedAndDeactivatedStatements } = require('../utils/joinActivatedAndDeactivatedStatements'); module.exports = (baseProvider, options, app) => { const _ = app.require('lodash'); @@ -216,12 +217,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, +};