Skip to content

Commit

Permalink
HCK-8010: add comma commenting for last activated column statement (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-filonenko authored Sep 19, 2024
1 parent 03b9125 commit 722a642
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const defaultTypes = require('../configs/defaultTypes');
const descriptors = require('../configs/descriptors');
const templates = require('./templates');
const { joinActivatedAndDeactivatedStatements } = require('../utils/joinActivatedAndDeactivatedStatements');

module.exports = (baseProvider, options, app) => {
const _ = app.require('lodash');
Expand Down Expand Up @@ -205,12 +206,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,
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 722a642

Please sign in to comment.