Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { getViewName } = require('../../../utils/general');
const { DualityViewPropertiesType } = require('../../../enums/DualityViewPropertiesType');
const { getAnnotationsString } = require('../../../utils/getAnnotationsString');

/**
* @abstract
Expand Down Expand Up @@ -176,9 +177,10 @@ class AbstractDualityViewFeDdlCreator {

/**
* @param createViewDto {CreateDualityViewDto}
* @param prepareName {Function}
* @return {string}
* */
getCreateJsonRelationalDualityViewHeadingDdl(createViewDto) {
getCreateJsonRelationalDualityViewHeadingDdl(createViewDto, prepareName) {
const { jsonSchema, view } = createViewDto;
const template = this._ddlTemplates?.dualityView?.createJsonRelationalDualityViewHeading || '';

Expand All @@ -187,12 +189,14 @@ class AbstractDualityViewFeDdlCreator {
const editionableStatement = this._getEditionableStatement(jsonSchema);
const viewName = getViewName(view);
const ddlViewName = this._getNamePrefixedWithSchemaName(viewName, view.schemaName);
const annotations = getAnnotationsString(prepareName)(view.viewAnnotations);

const params = {
orReplaceStatement,
forceStatement,
editionableStatement,
viewName: ddlViewName,
annotations: annotations ? `\n\t${annotations}` : '',
};
return this._assignTemplates(template, params);
}
Expand All @@ -210,7 +214,7 @@ class AbstractDualityViewFeDdlCreator {
* @return {string}
* */
convertDualityViewToDdl(createViewDto) {
const heading = this.getCreateJsonRelationalDualityViewHeadingDdl(createViewDto);
const heading = this.getCreateJsonRelationalDualityViewHeadingDdl(createViewDto, this._prepareName);
const body = this.getDualityViewBodyDdl(createViewDto);
return heading + '\n' + body + '\n';
}
Expand Down
14 changes: 4 additions & 10 deletions forward_engineering/ddlProvider/ddlHelpers/tableHelper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
const _ = require('lodash');
const { getAnnotationsString } = require('../../utils/getAnnotationsString');

module.exports = ({ getColumnsList, checkAllKeysDeactivated, commentIfDeactivated, prepareName, assignTemplates }) => {
const getTableType = ({
duplicated,
external,
immutable,
sharded,
temporary,
temporaryType,
blockchain_table_clauses,
}) => {
module.exports = ({ getColumnsList, checkAllKeysDeactivated, commentIfDeactivated, prepareName }) => {
const getTableType = ({ duplicated, immutable, sharded, temporary, temporaryType, blockchain_table_clauses }) => {
const blockchain = !_.isEmpty(blockchain_table_clauses);
switch (true) {
case temporary:
Expand All @@ -36,6 +29,7 @@ module.exports = ({ getColumnsList, checkAllKeysDeactivated, commentIfDeactivate
{ key: 'partitioning', getValue: getPartitioning },
{ key: 'selectStatement', getValue: getBasicValue('AS') },
{ key: 'tableProperties', getValue: value => _.trim(value) },
{ key: 'tableAnnotations', getValue: getAnnotationsString(prepareName) },
]
.map(config => (tableData[config.key] ? wrap(config.getValue(tableData[config.key], tableData)) : ''))
.filter(Boolean)
Expand Down
19 changes: 18 additions & 1 deletion forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
getColumnsList,
prepareNameForScriptFormat,
} = require('../utils/general');
const { getAnnotationsString } = require('../utils/getAnnotationsString');
const { assignTemplates } = require('../utils/assignTemplates');
const { decorateType } = require('./ddlHelpers/columnDefinitionHelpers/decorateType');
const { getNotNullConstraints } = require('../alterScript/alterScriptHelpers/columnHelpers/nonNullConstraintHelper');
Expand Down Expand Up @@ -65,6 +66,7 @@ module.exports = (baseProvider, options, app) => {
commentIfDeactivated,
prepareName,
assignTemplates,
wrapComment,
});

const { getUserDefinedType, isNotPlainType } = require('./ddlHelpers/udtHelper')({
Expand Down Expand Up @@ -206,6 +208,7 @@ module.exports = (baseProvider, options, app) => {
subtype: jsonSchema.subtype,
defaultOnNull: jsonSchema.defaultOnNull,
generatedDefaultValue: jsonSchema.generatedDefaultValue,
columnAnnotations: jsonSchema.columnAnnotations,
};
},

Expand All @@ -220,6 +223,8 @@ module.exports = (baseProvider, options, app) => {

convertColumnDefinition(columnDefinition, template = templates.columnDefinition) {
const type = replaceTypeByVersion(columnDefinition.type, columnDefinition.dbVersion);
const annotations = getAnnotationsString(prepareName)(columnDefinition.columnAnnotations);
const finalAnnotationsClause = annotations ? ' ' + annotations : '';

return commentIfDeactivated(
assignTemplates(template, {
Expand All @@ -228,6 +233,7 @@ module.exports = (baseProvider, options, app) => {
default: getColumnDefault(columnDefinition),
encrypt: getColumnEncrypt(columnDefinition),
constraints: getColumnConstraints(columnDefinition),
annotations: finalAnnotationsClause,
}),
{
isActivated: columnDefinition.isActivated,
Expand Down Expand Up @@ -375,6 +381,7 @@ module.exports = (baseProvider, options, app) => {
'description',
'ifNotExist',
'tableProperties',
'tableAnnotations',
),
synonyms:
tableData?.schemaData?.synonyms?.filter(synonym => synonym.synonymEntityId === jsonSchema.GUID) ||
Expand Down Expand Up @@ -407,6 +414,7 @@ module.exports = (baseProvider, options, app) => {
tableProperties,
synonyms,
notNullConstraints,
tableAnnotations,
},
isActivated,
) {
Expand Down Expand Up @@ -471,6 +479,7 @@ module.exports = (baseProvider, options, app) => {
partitioning,
selectStatement,
tableProperties,
tableAnnotations,
}),
});
if (usingTryCatchWrapper) {
Expand All @@ -485,7 +494,7 @@ module.exports = (baseProvider, options, app) => {
},

hydrateIndex(indexData, tableData, schemaData) {
return { ...indexData, schemaName: schemaData.schemaName };
return { ...indexData, schemaName: schemaData.schemaName, indexAnnotations: indexData.indexAnnotations };
},

createIndex(tableName, index, dbData, isParentActivated = true) {
Expand All @@ -501,6 +510,9 @@ module.exports = (baseProvider, options, app) => {
const dbVersion = options.dbVersion || '';
const usingTryCatchWrapper = shouldUseTryCatchIfNotExistsWrapper(dbVersion);

const annotations = getAnnotationsString(prepareName)(index.indexAnnotations);
const finalAnnotationsClause = annotations ? ' ' + annotations : '';

const shouldInsertIfNotExistsStatement = index.ifNotExist && !usingTryCatchWrapper;

let statement = assignTemplates(templates.createIndex, {
Expand All @@ -510,6 +522,7 @@ module.exports = (baseProvider, options, app) => {
keys,
options: indexOptions,
tableName: getNamePrefixedWithSchemaName(tableName, index.schemaName),
annotations: finalAnnotationsClause,
});

if (index.ifNotExist && usingTryCatchWrapper) {
Expand Down Expand Up @@ -587,6 +600,7 @@ module.exports = (baseProvider, options, app) => {
dbVersion: _.get(viewData, 'schemaData.dbVersion'),
},
whereClause: detailsTab.whereClause,
viewAnnotations: detailsTab.viewAnnotations,
};
},

Expand Down Expand Up @@ -650,6 +664,8 @@ module.exports = (baseProvider, options, app) => {
const dbVersion = _.get(viewData, 'modelInfo.dbVersion', '');
const usingTryCatchWrapper = shouldUseTryCatchIfNotExistsWrapper(dbVersion);

const annotations = getAnnotationsString(prepareName)(viewData.viewAnnotations);

let createViewDdl = assignTemplates(templates.createView, {
name: viewName,
ifNotExists: !usingTryCatchWrapper && viewData.ifNotExist ? ' IF NOT EXISTS' : '',
Expand All @@ -660,6 +676,7 @@ module.exports = (baseProvider, options, app) => {
viewProperties: viewData.viewProperties ? ' \n' + tab(viewData.viewProperties) : '',
sharing: viewData.sharing && !viewData.materialized ? ` SHARING=${viewData.sharing}` : '',
selectStatement,
annotations: annotations ? `\n\t${annotations}` : '',
});
if (usingTryCatchWrapper) {
createViewDdl = wrapIfNotExists(createViewDdl, viewData.ifNotExist);
Expand Down
8 changes: 4 additions & 4 deletions forward_engineering/ddlProvider/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
createTableProps:
'${columnDefinitions}${keyConstraints}${checkConstraints}${foreignKeyConstraints}${notNullConstraints}',

columnDefinition: '${name}${type}${default}${encrypt}${constraints}',
columnDefinition: '${name}${type}${default}${encrypt}${constraints}${annotations}',

createKeyConstraint: '${constraintName}${keyType}${columns}${options}',

Expand All @@ -20,7 +20,7 @@ module.exports = {
createForeignKey:
'ALTER TABLE ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey})${onDelete};',

createIndex: `CREATE$\{indexType} INDEX$\{ifNotExists}$\{name} ON $\{tableName}$\{keys}$\{options};\n`,
createIndex: 'CREATE${indexType} INDEX${ifNotExists}${name} ON ${tableName}${keys}${options}${annotations};\n',

dropIndex: 'DROP INDEX ${name};',

Expand All @@ -29,7 +29,7 @@ module.exports = {
alterIndexRebuild: 'ALTER INDEX ${name} REBUILD ${options};',

createView:
'CREATE${orReplace}${force}${viewType}${materialized} VIEW${ifNotExists} ${name} ${sharing}${viewProperties}\n\tAS ${selectStatement}',
'CREATE${orReplace}${force}${viewType}${materialized} VIEW${ifNotExists} ${name} ${sharing}${viewProperties}${annotations}\n\tAS ${selectStatement}',

viewSelectStatement: 'SELECT ${keys}\n\tFROM ${tableName}',

Expand Down Expand Up @@ -61,7 +61,7 @@ module.exports = {

dualityView: {
createJsonRelationalDualityViewHeading:
'CREATE${orReplaceStatement}${forceStatement}${editionableStatement} JSON RELATIONAL DUALITY VIEW ${viewName} AS',
'CREATE${orReplaceStatement}${forceStatement}${editionableStatement} JSON RELATIONAL DUALITY VIEW ${viewName}${annotations} AS',

sql: {
tableTagsStatement:
Expand Down
44 changes: 44 additions & 0 deletions forward_engineering/utils/getAnnotationsString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { wrapComment } = require('./general');

/**
* @typedef {{
* annotationName?: string
* annotationValue?: string
* }} Annotation
*/

/**
* Generates annotations string.
* @param {function} prepareName
* @returns {(annotations: Annotation[]) => string} - returns Annotations string (e.g: "\nANNOTATIONS (...)") or ''.
*/
const getAnnotationsString = prepareName => annotations => {
if (!Array.isArray(annotations) || annotations.length === 0) {
return '';
}

const wrapValue = value => wrapComment(value);

const annotationsItems = annotations
.filter(annotation => annotation?.annotationName?.trim())
.map(annotation => {
const { annotationName, annotationValue } = annotation;
const name = prepareName(annotationName.trim());

let finalValue = '';

if (annotationValue !== undefined && annotationValue.trim() !== '') {
finalValue = ' ' + wrapValue(annotationValue.trim());
}

return `${name}${finalValue}`;
});

if (annotationsItems.length > 0) {
return `ANNOTATIONS (${annotationsItems.join(', ')})`;
}

return '';
};

module.exports = { getAnnotationsString };
4 changes: 2 additions & 2 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
* the agreement/contract under which the software has been supplied.
*/
module.exports = {
'*.{js,jsx,ts,tsx,cjs,mjs}': ['prettier --write', 'npm run lint'],
'*.{json,css,scss}': ['prettier --write'],
'*.{js,jsx,ts,tsx,cjs,mjs}': ['prettier --write', 'npm run lint'],
'*.{json,css,scss}': ['prettier --write'],
};
Loading