diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index 40e4d6c..6d2919d 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -3,6 +3,7 @@ const types = require('./configs/types'); const templates = require('./configs/templates'); const getAdditionalOptions = require('./helpers/getAdditionalOptions'); const dropStatementProxy = require('./helpers/dropStatementProxy'); +const { joinActivatedAndDeactivatedStatements } = require('./utils/joinActivatedAndDeactivatedStatements'); module.exports = (baseProvider, options, app) => { const _ = app.require('lodash'); @@ -272,10 +273,11 @@ module.exports = (baseProvider, options, app) => { const dividedForeignKeys = divideIntoActivatedAndDeactivated(foreignKeyConstraints, key => key.statement); const foreignKeyConstraintsString = generateConstraintsString(dividedForeignKeys, isActivated); const ignoreReplace = selectStatement ? (selectIgnore ? ' IGNORE' : selectReplace ? ' REPLACE' : '') : ''; + const columnStatements = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' }); const tableStatement = assignTemplates(templates.createTable, { name: tableName, - column_definitions: columns.join(',\n\t'), + column_definitions: columnStatements, selectStatement: selectStatement ? ` AS ${selectStatement}` : '', temporary: temporaryTable, ifNotExist: ifNotExistTable, 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, +};