Skip to content

Commit

Permalink
HCK-8008: add comma commenting for last activated column statement (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-filonenko authored Sep 19, 2024
1 parent 4aeea32 commit 7784d51
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
8 changes: 5 additions & 3 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
53 changes: 53 additions & 0 deletions forward_engineering/utils/joinActivatedAndDeactivatedStatements.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit 7784d51

Please sign in to comment.