Skip to content

Commit

Permalink
HCK-7990: Fix splitting of column statements when deactivating the la…
Browse files Browse the repository at this point in the history
…st one (#31)

<!--do not remove this marker, its needed to replace info when ticket
title is updated -->
<!--jira-description-action-hidden-marker-start-->

<table>
<td>
<a href="https://hackolade.atlassian.net/browse/HCK-7990"
title="HCK-7990" target="_blank"><img alt="Sub-bug"
src="https://hackolade.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium"
/>HCK-7990</a> [DB2]: DDL script is not valid when the last attribute is
commented in Table
  </td></table>
  <br />
 

<!--jira-description-action-hidden-marker-end-->
  • Loading branch information
serhii-filonenko authored Sep 19, 2024
1 parent 52ef9b8 commit 320148a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
3 changes: 3 additions & 0 deletions constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const TABLE_TYPE = {
view: 'VIEW',
};

const INLINE_COMMENT = '--';

module.exports = {
ERROR_MESSAGE,
TABLE_TYPE,
INLINE_COMMENT,
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const {
divideIntoActivatedAndDeactivated,
} = require('../../../utils/general');
const { getOptionsString } = require('../constraint/getOptionsString');
const { joinActivatedAndDeactivatedStatements } = require('../../../utils/joinActivatedAndDeactivatedStatements');
const { INLINE_COMMENT } = require('../../../../constants/constants');

/**
* @typedef {{ activatedItems: string[], deactivatedItems: string[] }} DividedConstraints
Expand Down Expand Up @@ -117,7 +119,8 @@ const getTableProps = ({ columns, foreignKeyConstraints, keyConstraints, checkCo
dividedConstraints: { activatedItems: checkConstraints, deactivatedItems: [] },
isParentActivated: isActivated,
});
const columnsString = joinStatements({ statements: columns });
const columnsString = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' });

const tableProps = assignTemplates({
template: templates.createTableProps,
templateData: {
Expand Down
25 changes: 6 additions & 19 deletions forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,19 @@ const { getIndexOptions } = require('./ddlHelpers/index/getIndexOptions.js');
const { getTableType } = require('./ddlHelpers/table/getTableType.js');
const { getName } = require('./ddlHelpers/jsonSchema/jsonSchemaHelper.js');
const { hydrateAuxiliaryTableData } = require('./ddlHelpers/table/hydrateAuxiliaryTableData.js');
const { joinActivatedAndDeactivatedStatements } = require('../utils/joinActivatedAndDeactivatedStatements');

/**
* @param {{ columns: object[] }}
* @returns {string}
*/
const getViewColumnsAsString = ({ columns }) => {
const statements = columns.map(({ statement, isActivated }) =>
commentIfDeactivated(statement, { isActivated, isPartOfLine: false }),
);
const lastNonCommentIndex = statements.findLastIndex(statement => !statement.startsWith('--'));
const indent = '\n\t\t';
const statements = columns.map(({ statement, isActivated }) => {
return commentIfDeactivated(statement, { isActivated, isPartOfLine: false });
});

if (lastNonCommentIndex === -1) {
return statements.join('\n');
}

return statements
.map((st, index) => {
const isNotLast = index !== statements.length - 1;

if (lastNonCommentIndex === index && isNotLast) {
return `${st} -- ,`;
}

return `${st}${isNotLast ? ',' : ''}`;
})
.join('\n\t\t');
return indent + joinActivatedAndDeactivatedStatements({ statements, delimiter: ',', indent });
};

module.exports = (baseProvider, options, app) => {
Expand Down
3 changes: 2 additions & 1 deletion forward_engineering/utils/general.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { toLower } = require('lodash');
const { INLINE_COMMENT } = require('../../constants/constants');

/**
* @param {{ text: string, tab: string }}
Expand Down Expand Up @@ -53,7 +54,7 @@ const divideIntoActivatedAndDeactivated = ({ items, mapFunction }) => {
* @param {{ isActivated: boolean, isPartOfLine: boolean, inlineComment: string }}
* @returns {string}
*/
const commentIfDeactivated = (statement, { isActivated, isPartOfLine, inlineComment = '--' }) => {
const commentIfDeactivated = (statement, { isActivated, isPartOfLine, inlineComment = INLINE_COMMENT }) => {
if (isActivated !== false) {
return statement;
}
Expand Down
55 changes: 55 additions & 0 deletions forward_engineering/utils/joinActivatedAndDeactivatedStatements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { INLINE_COMMENT } = require('../../constants/constants');

/**
* @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(INLINE_COMMENT));
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 320148a

Please sign in to comment.