From 07a61ec54997fac5668567851d297d2f2680ccb2 Mon Sep 17 00:00:00 2001 From: Yevhenii Moroziuk Date: Tue, 30 Jul 2024 16:21:08 +0300 Subject: [PATCH] HCK-7296 (#123) * HCK-7296: Update config & clean up * HCK-7296: remove redundant code * HCK-7296: update comments logic * HCK-7296: fix sonarqube warnings * HCK-7296: fix comments * HCK-7296: Update comments formatting --- forward_engineering/api.js | 5 +- forward_engineering/config.json | 14 +++ forward_engineering/ddlProvider.js | 36 ++++-- .../helpers/alterScriptFromDeltaHelper.js | 11 +- .../alterContainerHelper.js | 6 +- .../alterScriptHelpers/alterEntityHelper.js | 119 +++++++++--------- .../alterScriptHelpers/commonScript.js | 8 +- .../createColumnDefinition.js | 52 ++++---- .../helpers/applyToInstanceHelper.js | 6 +- .../helpers/columnDefinitionHelper.js | 29 +++-- .../commentDeactivatedHelper.js | 14 ++- .../commentDropStatements.js | 9 +- forward_engineering/helpers/constants.js | 6 + .../helpers/constraintHelper.js | 4 +- forward_engineering/helpers/general.js | 12 +- .../helpers/getStageCopyOptions.js | 4 +- forward_engineering/helpers/keyHelper.js | 18 +-- forward_engineering/helpers/tableHelper.js | 4 +- forward_engineering/utils/escapeString.js | 8 ++ 19 files changed, 206 insertions(+), 159 deletions(-) rename forward_engineering/helpers/{ => commentHelpers}/commentDeactivatedHelper.js (79%) rename forward_engineering/helpers/{ => commentHelpers}/commentDropStatements.js (64%) create mode 100644 forward_engineering/utils/escapeString.js diff --git a/forward_engineering/api.js b/forward_engineering/api.js index d18860a3..c49c37ef 100644 --- a/forward_engineering/api.js +++ b/forward_engineering/api.js @@ -1,7 +1,7 @@ 'use strict'; const _ = require('lodash'); -const { commentDropStatements } = require('./helpers/commentDropStatements'); +const { commentDropStatements } = require('./helpers/commentHelpers/commentDropStatements'); const { DROP_STATEMENTS } = require('./helpers/constants'); module.exports = { @@ -18,7 +18,8 @@ module.exports = { ); } - const script = getAlterScript({ collection, _, ddlProvider, app }); + const scriptFormat = _.get(data, 'options.targetScriptOptions.keyword'); + const script = getAlterScript({ scriptFormat, collection, ddlProvider, app }); const applyDropStatements = data.options?.additionalOptions?.some( option => option.id === 'applyDropStatements' && option.value, diff --git a/forward_engineering/config.json b/forward_engineering/config.json index 23f5c1bb..094a5990 100644 --- a/forward_engineering/config.json +++ b/forward_engineering/config.json @@ -7,6 +7,20 @@ "namePrefix": "Snowflake", "resolvedDefinitions": true, "combinedContainers": true, + "options": [ + { + "name": "SnowSight", + "keyword": "snowSight", + "disableApplyScriptToInstance": false, + "supportsDeltaModel": true + }, + { + "name": "Classic UI", + "keyword": "classicUI", + "disableApplyScriptToInstance": false, + "supportsDeltaModel": true + } + ], "level": { "container": true, "entity": true, diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index ef3d2ac1..d35a2cea 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -6,7 +6,7 @@ const _ = require('lodash'); const defaultTypes = require('./configs/defaultTypes'); const types = require('./configs/types'); const templates = require('./configs/templates'); -const { LANGUAGES } = require('./helpers/constants'); +const { LANGUAGES, FORMATS } = require('./helpers/constants'); const { prepareAlterSetUnsetData, prepareContainerName, @@ -17,6 +17,7 @@ const { prepareCollectionFormatTypeOptions, prepareCollectionStageCopyOptions, } = require('./helpers/alterScriptHelpers/common'); +const { escapeString } = require('./utils/escapeString'); const DEFAULT_SNOWFLAKE_SEQUENCE_START = 1; const DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT = 1; @@ -24,6 +25,7 @@ const DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT = 1; module.exports = (baseProvider, options, app) => { const assignTemplates = app.require('@hackolade/ddl-fe-utils').assignTemplates; const { tab, hasType, clean } = app.require('@hackolade/ddl-fe-utils').general; + const scriptFormat = options?.targetScriptOptions?.keyword || FORMATS.SNOWSIGHT; const keyHelper = require('./helpers/keyHelper')(app); const { getFileFormat, getCopyOptions, addOptions, getAtOrBefore, mergeKeys } = @@ -48,7 +50,7 @@ module.exports = (baseProvider, options, app) => { require('./helpers/columnDefinitionHelper')(app); const { generateConstraint } = require('./helpers/constraintHelper')(app); - const { commentIfDeactivated } = require('./helpers/commentDeactivatedHelper'); + const { commentIfDeactivated } = require('./helpers/commentHelpers/commentDeactivatedHelper'); const { getAlterSchemaName, @@ -92,8 +94,15 @@ module.exports = (baseProvider, options, app) => { return !_.isEmpty(constraints) ? ',\n\t\t' + constraints.join(',\n\t\t') : ''; }; + function insertNewlinesAtEdges(input) { + input = input.replace(/^(\$\$|')/, match => match + '\n'); + input = input.replace(/(\$\$|')$/, match => '\n\t' + match); + + return input; + } + const getOrReplaceStatement = isEnabled => (isEnabled ? ' OR REPLACE' : ''); - const getBodyStatement = body => (body ? `\n\t$$\n${body}\n\t$$` : ''); + const getBodyStatement = body => (body ? `\n\t${insertNewlinesAtEdges(escapeString(scriptFormat, body))}` : ''); const getCommentsStatement = text => (text ? `\n\tCOMMENT = '${text}'` : ''); const getNotNullStatement = isEnabled => (isEnabled ? '\n\tNOT NULL' : ''); const getIfNotExistStatement = ifNotExist => (ifNotExist ? ' IF NOT EXISTS' : ''); @@ -119,7 +128,7 @@ module.exports = (baseProvider, options, app) => { const dataRetentionStatement = !isNaN(dataRetention) && dataRetention ? `\n\tDATA_RETENTION_TIME_IN_DAYS=${dataRetention}` : ''; const managedAccessStatement = managedAccess ? '\n\tWITH MANAGED ACCESS' : ''; - const commentStatement = comment ? `\n\tCOMMENT=$$${comment}$$` : ''; + const commentStatement = comment ? `\n\tCOMMENT=${escapeString(scriptFormat, comment)}` : ''; const currentSchemaName = getName(isCaseSensitive, schemaName); const currentDatabaseName = getName(isCaseSensitive, databaseName); const fullName = getFullName(currentDatabaseName, currentSchemaName); @@ -290,7 +299,7 @@ module.exports = (baseProvider, options, app) => { : mergeKeys(tableData.partitioningKey)) + ')' : ''; - const comment = tableData.comment ? ` COMMENT=$$${tableData.comment}$$` : ''; + const comment = tableData.comment ? ` COMMENT=${escapeString(scriptFormat, tableData.comment)}` : ''; const copyGrants = tableData.copyGrants ? ` COPY GRANTS` : ''; const dataRetentionTime = tableData.dataRetentionTime ? ` DATA_RETENTION_TIME_IN_DAYS=${tableData.dataRetentionTime}` @@ -402,13 +411,20 @@ module.exports = (baseProvider, options, app) => { type: decorateType(columnDefinition.type, columnDefinition), collation: getCollation(columnDefinition.type, columnDefinition.collation), default: !_.isUndefined(columnDefinition.default) - ? ' DEFAULT ' + getDefault(columnDefinition.type, columnDefinition.default) + ? ' DEFAULT ' + + getDefault({ + scriptFormat, + type: columnDefinition.type, + defaultValue: columnDefinition.default, + }) : '', autoincrement: getAutoIncrement(columnDefinition.type, 'AUTOINCREMENT', columnDefinition.autoincrement), identity: getAutoIncrement(columnDefinition.type, 'IDENTITY', columnDefinition.identity), not_nul: !columnDefinition.nullable ? ' NOT NULL' : '', inline_constraint: getInlineConstraint(columnDefinition), - comment: columnDefinition.comment ? ` COMMENT $$${columnDefinition.comment}$$` : '', + comment: columnDefinition.comment + ? ` COMMENT ${escapeString(scriptFormat, columnDefinition.comment)}` + : '', tag: getTagStatement({ tags: columnDefinition.columnTags, isCaseSensitive: columnDefinition.isCaseSensitive, @@ -520,7 +536,7 @@ module.exports = (baseProvider, options, app) => { name: getFullName(schemaName, viewData.name), column_list: viewColumnsToString(columnList, isActivated), copy_grants: viewData.copyGrants ? 'COPY GRANTS\n' : '', - comment: viewData.comment ? 'COMMENT=$$' + viewData.comment + '$$\n' : '', + comment: viewData.comment ? 'COMMENT=' + escapeString(scriptFormat, viewData.comment) + '\n' : '', select_statement: selectStatement, tag: tagStatement ? tagStatement + '\n' : '', }); @@ -645,7 +661,7 @@ module.exports = (baseProvider, options, app) => { start: sequence.sequenceStart || DEFAULT_SNOWFLAKE_SEQUENCE_START, increment: sequence.sequenceIncrement || DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT, comment: sequence.sequenceComments - ? ` COMMENT=$$${sequence.sequenceComments}$$` + ? ` COMMENT=${escapeString(scriptFormat, sequence.sequenceComments)}` : '', }), ) @@ -661,7 +677,7 @@ module.exports = (baseProvider, options, app) => { getFormatTypeOptions(fileFormat.fileFormat, fileFormat.formatTypeOptions), ), comment: fileFormat.fileFormatComments - ? ` COMMENT=$$${fileFormat.fileFormatComments}$$` + ? ` COMMENT=${escapeString(scriptFormat, fileFormat.fileFormatComments)}` : '', }), ) diff --git a/forward_engineering/helpers/alterScriptFromDeltaHelper.js b/forward_engineering/helpers/alterScriptFromDeltaHelper.js index 1fee631d..9bb9dc44 100644 --- a/forward_engineering/helpers/alterScriptFromDeltaHelper.js +++ b/forward_engineering/helpers/alterScriptFromDeltaHelper.js @@ -1,4 +1,3 @@ -const _ = require('lodash'); const { getAddContainerScript, getDeleteContainerScript, @@ -34,7 +33,7 @@ const getAlterContainersScripts = (collection, ddlProvider, app) => { return { addedContainerScripts, deletedContainerScripts, modifiedContainerScripts }; }; -const getAlterCollectionsScripts = (collection, ddlProvider, app) => { +const getAlterCollectionsScripts = ({ collection, ddlProvider, app, scriptFormat }) => { const getCollectionScripts = (items, compMode, getScript) => items.filter(item => item.compMod?.[compMode]).map(getScript); @@ -43,7 +42,7 @@ const getAlterCollectionsScripts = (collection, ddlProvider, app) => { const addedCollectionScripts = getCollectionScripts( getItems(collection, 'entities', 'added', 'values'), 'created', - getAddCollectionScript(ddlProvider, app), + getAddCollectionScript({ ddlProvider, app, scriptFormat }), ); const deletedCollectionScripts = getCollectionScripts( getItems(collection, 'entities', 'deleted', 'values'), @@ -59,7 +58,7 @@ const getAlterCollectionsScripts = (collection, ddlProvider, app) => { const addedColumnScripts = getColumnScripts( getItems(collection, 'entities', 'added', 'values'), - getAddColumnScript(ddlProvider, app), + getAddColumnScript({ ddlProvider, app, scriptFormat }), ); const deletedColumnScripts = getColumnScripts( getItems(collection, 'entities', 'deleted', 'values'), @@ -131,9 +130,9 @@ const getAlterTagsScripts = ({ collection, ddlProvider, app }) => { return { addedTagsScripts, deletedTagsScripts, modifiedTagsScripts }; }; -const getAlterScript = ({ collection, ddlProvider, app }) => { +const getAlterScript = ({ scriptFormat, collection, ddlProvider, app }) => { const script = { - ...getAlterCollectionsScripts(collection, ddlProvider, app), + ...getAlterCollectionsScripts({ collection, ddlProvider, app, scriptFormat }), ...getAlterContainersScripts(collection, ddlProvider, app), ...getAlterViewsScripts({ schema: collection, ddlProvider, app }), ...getAlterTagsScripts({ collection, ddlProvider, app }), diff --git a/forward_engineering/helpers/alterScriptHelpers/alterContainerHelper.js b/forward_engineering/helpers/alterScriptHelpers/alterContainerHelper.js index dd503b5d..760ecee1 100644 --- a/forward_engineering/helpers/alterScriptHelpers/alterContainerHelper.js +++ b/forward_engineering/helpers/alterScriptHelpers/alterContainerHelper.js @@ -1,4 +1,3 @@ -const _ = require('lodash'); const { prepareContainerLevelData } = require('./common'); const getAddContainerScript = (ddlProvider, app) => container => { @@ -6,12 +5,13 @@ const getAddContainerScript = (ddlProvider, app) => container => { const containerData = { ...container.role, name: getDbName(container.role) }; const containerLevelData = prepareContainerLevelData(containerData); const hydratedContainer = ddlProvider.hydrateSchema(containerData, containerLevelData); - const script = ddlProvider.createSchema(hydratedContainer); - return script; + + return ddlProvider.createSchema(hydratedContainer); }; const getDeleteContainerScript = ddlProvider => container => { const { name } = ddlProvider.hydrateForDeleteSchema({ ...container, ...container.role }); + return ddlProvider.dropSchema({ name }); }; diff --git a/forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js b/forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js index 0daca06c..ae610d93 100644 --- a/forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js +++ b/forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js @@ -1,40 +1,43 @@ const _ = require('lodash'); const { checkFieldPropertiesChanged, getNames, getBaseAndContainerNames } = require('./common'); const { createColumnDefinitionBySchema } = require('./createColumnDefinition'); -const { commentIfDeactivated } = require('../commentDeactivatedHelper'); - -const getAddCollectionScript = (ddlProvider, app) => collection => { - const { getEntityName, getName } = require('../general')(app); +const { commentIfDeactivated } = require('../commentHelpers/commentDeactivatedHelper'); + +const getAddCollectionScript = + ({ ddlProvider, app, scriptFormat }) => + collection => { + const { getEntityName, getName } = require('../general')(app); + + const { schemaName, databaseName } = getBaseAndContainerNames(collection, getName); + const jsonSchema = { + ...collection, + ...(_.omit(collection?.role, 'properties') || {}), + }; + const columnDefinitions = _.toPairs(jsonSchema.properties).map(([name, column]) => + createColumnDefinitionBySchema({ + name, + jsonSchema: column, + parentJsonSchema: jsonSchema, + ddlProvider, + scriptFormat, + }), + ); - const { schemaName, databaseName } = getBaseAndContainerNames(collection, getName); - const jsonSchema = { - ...collection, - ...(_.omit(collection?.role, 'properties') || {}), - }; - const columnDefinitions = _.toPairs(jsonSchema.properties).map(([name, column]) => - createColumnDefinitionBySchema({ - name, - jsonSchema: column, - parentJsonSchema: jsonSchema, - ddlProvider, - }), - ); - - const tableData = { - name: getEntityName(jsonSchema), - columns: columnDefinitions.map(ddlProvider.convertColumnDefinition), - foreignKeyConstraints: [], - schemaData: { schemaName, databaseName }, - columnDefinitions, + const tableData = { + name: getEntityName(jsonSchema), + columns: columnDefinitions.map(ddlProvider.convertColumnDefinition), + foreignKeyConstraints: [], + schemaData: { schemaName, databaseName }, + columnDefinitions, + }; + const hydratedTable = ddlProvider.hydrateTable({ + tableData, + entityData: [jsonSchema], + jsonSchema, + }); + + return ddlProvider.createTable(hydratedTable, jsonSchema.isActivated); }; - const hydratedTable = ddlProvider.hydrateTable({ - tableData, - entityData: [jsonSchema], - jsonSchema, - }); - - return ddlProvider.createTable(hydratedTable, jsonSchema.isActivated); -}; const getDeleteCollectionScript = app => collection => { const { getEntityName, getFullName, getName } = require('../general')(app); @@ -55,31 +58,35 @@ const getModifyCollectionScript = ddlProvider => collection => { return ddlProvider.alterTable(data); }; -const getAddColumnScript = (ddlProvider, app) => collection => { - const { getEntityName, getFullName, getName } = require('../general')(app); - - const collectionSchema = { - ...collection, - ...(_.omit(collection?.role, 'properties') || {}), +const getAddColumnScript = + ({ ddlProvider, app, scriptFormat }) => + collection => { + const { getEntityName, getFullName, getName } = require('../general')(app); + + const collectionSchema = { + ...collection, + ...(_.omit(collection?.role, 'properties') || {}), + }; + const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName); + const fullName = getFullName(databaseName, getFullName(schemaName, tableName)); + + return _.toPairs(collection.properties) + .filter(([_, jsonSchema]) => !jsonSchema.compMod) + .map(([name, jsonSchema]) => + createColumnDefinitionBySchema({ + name, + jsonSchema, + parentJsonSchema: collectionSchema, + ddlProvider, + scriptFormat, + }), + ) + .map(ddlProvider.convertColumnDefinition) + .map( + column => + `ALTER TABLE IF EXISTS ${fullName} ADD COLUMN ${commentIfDeactivated(column.statement, column)};`, + ); }; - const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName); - const fullName = getFullName(databaseName, getFullName(schemaName, tableName)); - - return _.toPairs(collection.properties) - .filter(([_, jsonSchema]) => !jsonSchema.compMod) - .map(([name, jsonSchema]) => - createColumnDefinitionBySchema({ - name, - jsonSchema, - parentJsonSchema: collectionSchema, - ddlProvider, - }), - ) - .map(ddlProvider.convertColumnDefinition) - .map( - column => `ALTER TABLE IF EXISTS ${fullName} ADD COLUMN ${commentIfDeactivated(column.statement, column)};`, - ); -}; const getDeleteColumnScript = app => collection => { const { getEntityName, getFullName, getName } = require('../general')(app); diff --git a/forward_engineering/helpers/alterScriptHelpers/commonScript.js b/forward_engineering/helpers/alterScriptHelpers/commonScript.js index 72503ec6..5b9672e4 100644 --- a/forward_engineering/helpers/alterScriptHelpers/commonScript.js +++ b/forward_engineering/helpers/alterScriptHelpers/commonScript.js @@ -1,4 +1,5 @@ const _ = require('lodash'); +const { escapeString } = require('../../utils/escapeString'); module.exports = ({ getName, getFullName, templates, assignTemplates, tab }) => { const getSchemaFullName = (database, schemaName, isCaseSensitive) => { @@ -10,6 +11,7 @@ module.exports = ({ getName, getFullName, templates, assignTemplates, tab }) => const getAlterSchemaScript = ({ database, isCaseSensitive, newName } = {}) => { const schemaFullName = getSchemaFullName(database, newName, isCaseSensitive); + return assignTemplates(templates.alterSchemaScript, { name: schemaFullName }); }; @@ -17,6 +19,7 @@ module.exports = ({ getName, getFullName, templates, assignTemplates, tab }) => const schemaFullName = getSchemaFullName(database, schemaName, isCaseSensitive); const tableName = getName(isCaseSensitive, newName); const tableFullName = getFullName(schemaFullName, tableName); + return assignTemplates(template, { name: tableFullName }); }; @@ -47,7 +50,10 @@ module.exports = ({ getName, getFullName, templates, assignTemplates, tab }) => } const setPropertyData = Object.keys(setProperty).map((key, index) => { - const value = key === 'description' ? `$$${setProperty[key]}$$` : setProperty[key]; + const propValue = setProperty[key]; + const scriptFormat = _.get(data, 'options.targetScriptOptions.keyword'); + + const value = key === 'description' ? escapeString(scriptFormat, propValue) : propValue; key = key === 'description' ? 'COMMENT' : key; const statement = `${key} = ${value}`; diff --git a/forward_engineering/helpers/alterScriptHelpers/createColumnDefinition.js b/forward_engineering/helpers/alterScriptHelpers/createColumnDefinition.js index 0c3652d9..105ce688 100644 --- a/forward_engineering/helpers/alterScriptHelpers/createColumnDefinition.js +++ b/forward_engineering/helpers/alterScriptHelpers/createColumnDefinition.js @@ -1,22 +1,18 @@ const _ = require('lodash'); -const createColumnDefinition = data => { - return Object.assign( - { - name: '', - type: '', - nullable: true, - primaryKey: false, - default: '', - length: '', - scale: '', - precision: '', - hasMaxLength: false, - expression: '', - }, - data, - ); -}; +const createColumnDefinition = data => ({ + name: '', + type: '', + nullable: true, + primaryKey: false, + default: '', + length: '', + scale: '', + precision: '', + hasMaxLength: false, + expression: '', + ...data, +}); const isNullable = (parentSchema, propertyName) => { if (!Array.isArray(parentSchema.required)) { @@ -33,9 +29,9 @@ const getDefault = jsonSchema => { return defaultValue; } else if (jsonSchema.default === null) { return 'NULL'; - } else { - return defaultValue; } + + return defaultValue; }; const getLength = jsonSchema => { @@ -43,17 +39,17 @@ const getLength = jsonSchema => { return jsonSchema.length; } else if (_.isNumber(jsonSchema.maxLength)) { return jsonSchema.maxLength; - } else { - return ''; } + + return ''; }; const getScale = jsonSchema => { if (_.isNumber(jsonSchema.scale)) { return jsonSchema.scale; - } else { - return ''; } + + return ''; }; const getPrecision = jsonSchema => { @@ -61,17 +57,17 @@ const getPrecision = jsonSchema => { return jsonSchema.precision; } else if (_.isNumber(jsonSchema.fractSecPrecision)) { return jsonSchema.fractSecPrecision; - } else { - return ''; } + + return ''; }; const hasMaxLength = jsonSchema => { if (jsonSchema.hasMaxLength) { return jsonSchema.hasMaxLength; - } else { - return ''; } + + return ''; }; const getType = jsonSchema => { @@ -82,7 +78,7 @@ const getType = jsonSchema => { return jsonSchema.mode || jsonSchema.childType || jsonSchema.type; }; -const createColumnDefinitionBySchema = ({ name, jsonSchema, parentJsonSchema, ddlProvider }) => { +const createColumnDefinitionBySchema = ({ name, jsonSchema, parentJsonSchema, ddlProvider, scriptFormat }) => { const columnDefinition = createColumnDefinition({ name: name, type: getType(jsonSchema), diff --git a/forward_engineering/helpers/applyToInstanceHelper.js b/forward_engineering/helpers/applyToInstanceHelper.js index 682c00ae..31732942 100644 --- a/forward_engineering/helpers/applyToInstanceHelper.js +++ b/forward_engineering/helpers/applyToInstanceHelper.js @@ -1,5 +1,5 @@ const async = require('async'); -const { filterDeactivatedQuery, queryIsDeactivated } = require('./commentDeactivatedHelper'); +const { filterDeactivatedQuery, queryIsDeactivated } = require('./commentHelpers/commentDeactivatedHelper'); const snowflakeHelper = require('../../reverse_engineering/helpers/snowflakeHelper'); const createQueries = (script = '') => { @@ -29,8 +29,8 @@ const applyToInstance = async (connectionInfo, logger) => { logger.log('error', error, `Failed to apply statement:\n${query}`); throw { ...error, - message: `Failed to apply statement:\n${query.substring(0, 80)}...\nReason: ${error.message}` - } + message: `Failed to apply statement:\n${query.substring(0, 80)}...\nReason: ${error.message}`, + }; } }); }; diff --git a/forward_engineering/helpers/columnDefinitionHelper.js b/forward_engineering/helpers/columnDefinitionHelper.js index ee286677..19ca7329 100644 --- a/forward_engineering/helpers/columnDefinitionHelper.js +++ b/forward_engineering/helpers/columnDefinitionHelper.js @@ -1,7 +1,9 @@ +const templates = require('../configs/templates'); +const _ = require('lodash'); +const { escapeString } = require('../utils/escapeString'); + module.exports = app => { - const templates = require('../configs/templates'); const assignTemplates = app.require('@hackolade/ddl-fe-utils').assignTemplates; - const _ = app.require('lodash'); const decorateType = (type, columnDefinition) => { type = _.toUpper(type); @@ -60,20 +62,20 @@ module.exports = app => { 'DATETIME', ].includes(_.toUpper(type)); - const escapeString = str => str.replace(/^'([\S\s]+)'$/, '$1'); + const localEscapeString = str => str.replace(/^'([\S\s]+)'$/, '$1'); - const getDefault = (type, defaultValue) => { + const getDefault = ({ type, defaultValue, scriptFormat }) => { if (isString(type)) { - return `$$${escapeString(String(defaultValue))}$$`; + return escapeString(scriptFormat, localEscapeString(String(defaultValue))); } else if (_.toUpper(type) === 'BOOLEAN') { return _.toUpper(defaultValue); - } else { - return defaultValue; } + + return defaultValue; }; - const getAutoIncrement = (type, keyword, autoincrement) => { - if (!autoincrement) { + const getAutoIncrement = (type, keyword, autoIncrement) => { + if (!autoIncrement) { return ''; } @@ -82,10 +84,10 @@ module.exports = app => { } let result = ` ${keyword}`; result += - !_.isNaN(autoincrement.step) && (autoincrement.start || autoincrement.start === 0) - ? ' START ' + autoincrement.start + !_.isNaN(autoIncrement.step) && (autoIncrement.start || autoIncrement.start === 0) + ? ' START ' + autoIncrement.start : ''; - result += !_.isNaN(autoincrement.step) && autoincrement.step ? ' INCREMENT ' + autoincrement.step : ''; + result += !_.isNaN(autoIncrement.step) && autoIncrement.step ? ' INCREMENT ' + autoIncrement.step : ''; return result; }; @@ -133,8 +135,9 @@ module.exports = app => { expression: columnDefinition.expression ? `(${columnDefinition.expression})` : `(value:${columnDefinition.name}::${columnDefinition.type})`, - comment: columnDefinition.comment ? ` COMMENT $$${columnDefinition.comment}$$` : '', + comment: columnDefinition.comment ? ` COMMENT ${escapeString(columnDefinition.comment)}` : '', }); + return { statement: externalColumnStatement, isActivated: columnDefinition.isActivated }; }; diff --git a/forward_engineering/helpers/commentDeactivatedHelper.js b/forward_engineering/helpers/commentHelpers/commentDeactivatedHelper.js similarity index 79% rename from forward_engineering/helpers/commentDeactivatedHelper.js rename to forward_engineering/helpers/commentHelpers/commentDeactivatedHelper.js index 028a8a2b..304767f2 100644 --- a/forward_engineering/helpers/commentDeactivatedHelper.js +++ b/forward_engineering/helpers/commentHelpers/commentDeactivatedHelper.js @@ -6,13 +6,19 @@ const STARTS_QUERY = ['//', '--']; const commentIfDeactivated = (statement, data, isPartOfLine) => { if (_.has(data, 'isActivated') && !data.isActivated) { if (isPartOfLine) { - return '/* ' + statement + ' */'; - } else if (statement.includes('\n')) { - return '/*\n' + statement + ' */\n'; - } else { return '// ' + statement; + } else if (statement.includes('\n')) { + return statement + .split('\n') + .filter(Boolean) + .map(line => '// ' + line) + .join('\n') + .concat('\n'); } + + return !statement ? '' : '// ' + statement; } + return statement; }; diff --git a/forward_engineering/helpers/commentDropStatements.js b/forward_engineering/helpers/commentHelpers/commentDropStatements.js similarity index 64% rename from forward_engineering/helpers/commentDropStatements.js rename to forward_engineering/helpers/commentHelpers/commentDropStatements.js index 2c0c1097..efd133a4 100644 --- a/forward_engineering/helpers/commentDropStatements.js +++ b/forward_engineering/helpers/commentHelpers/commentDropStatements.js @@ -1,14 +1,15 @@ -const { DROP_STATEMENTS } = require('./constants'); +const { DROP_STATEMENTS } = require('../constants'); const commentDropStatements = (script = '') => script .split('\n') + .filter(line => line.trim()) .map(line => { if (DROP_STATEMENTS.some(statement => line.includes(statement))) { - return `-- ${line}`; - } else { - return line; + return `// ${line}`; } + + return line; }) .join('\n'); diff --git a/forward_engineering/helpers/constants.js b/forward_engineering/helpers/constants.js index 73e87a84..0beb2f67 100644 --- a/forward_engineering/helpers/constants.js +++ b/forward_engineering/helpers/constants.js @@ -8,7 +8,13 @@ const LANGUAGES = { SQL: 'sql', }; +const FORMATS = { + SNOWSIGHT: 'snowSight', + CLASSIC_UI: 'classicUI', +}; + module.exports = { DROP_STATEMENTS, LANGUAGES, + FORMATS, }; diff --git a/forward_engineering/helpers/constraintHelper.js b/forward_engineering/helpers/constraintHelper.js index 88540199..5ebed41c 100644 --- a/forward_engineering/helpers/constraintHelper.js +++ b/forward_engineering/helpers/constraintHelper.js @@ -2,9 +2,7 @@ module.exports = app => { const { foreignKeysToString, foreignActiveKeysToString, getName } = require('./general')(app); const generateConstraint = ({ name, keys, keyType, isParentActivated, isCaseSensitive }) => { - const keysAsStrings = keys.map(key => - Object.assign({}, key, { name: `${getName(isCaseSensitive, key.name)}` }), - ); + const keysAsStrings = keys.map(key => ({ ...key, name: `${getName(isCaseSensitive, key.name)}` })); const atLeastOneActive = keysAsStrings.some(key => key.isActivated); let finalStringOfKeys = foreignActiveKeysToString(isCaseSensitive, keysAsStrings); if (atLeastOneActive && isParentActivated) { diff --git a/forward_engineering/helpers/general.js b/forward_engineering/helpers/general.js index e61571b0..ea72d92b 100644 --- a/forward_engineering/helpers/general.js +++ b/forward_engineering/helpers/general.js @@ -1,6 +1,6 @@ const _ = require('lodash'); const assignTemplates = require('../utils/assignTemplates'); -const { commentIfDeactivated } = require('./commentDeactivatedHelper'); +const { commentIfDeactivated } = require('./commentHelpers/commentDeactivatedHelper'); module.exports = app => { const { checkAllKeysActivated } = app.require('@hackolade/ddl-fe-utils').general; @@ -130,12 +130,12 @@ module.exports = app => { return commentIfDeactivated(deactivatedKeys.join(splitter), { isActivated: false }, true); } else if (deactivatedKeys.length === 0) { return processedKeys.join(splitter); - } else { - return ( - processedKeys.join(splitter) + - commentIfDeactivated(splitter + deactivatedKeys.join(splitter), { isActivated: false }, true) - ); } + + return ( + processedKeys.join(splitter) + + commentIfDeactivated(splitter + deactivatedKeys.join(splitter), { isActivated: false }, true) + ); } return keys; }; diff --git a/forward_engineering/helpers/getStageCopyOptions.js b/forward_engineering/helpers/getStageCopyOptions.js index 00125d6e..9be20382 100644 --- a/forward_engineering/helpers/getStageCopyOptions.js +++ b/forward_engineering/helpers/getStageCopyOptions.js @@ -6,9 +6,9 @@ module.exports = app => { return `SKIP_FILE_${properties.skipFileNum}`; } else if (properties.ON_ERROR === 'SKIP_FILE_%') { return `SKIP_FILE_${properties.skipFileNumPct}%`; - } else { - return properties.ON_ERROR; } + + return properties.ON_ERROR; }; const getStageCopyOptions = properties => { diff --git a/forward_engineering/helpers/keyHelper.js b/forward_engineering/helpers/keyHelper.js index 8654ecef..ddd294c7 100644 --- a/forward_engineering/helpers/keyHelper.js +++ b/forward_engineering/helpers/keyHelper.js @@ -8,25 +8,11 @@ module.exports = app => { }; const isUniqueKey = column => { - if (column.compositeUniqueKey) { - return false; - } else if (!column.unique) { - return false; - } else { - return true; - } + return !(column.compositeUniqueKey || !column.unique); }; const isPrimaryKey = column => { - if (column.compositeUniqueKey) { - return false; - } else if (column.compositePrimaryKey) { - return false; - } else if (!column.primaryKey) { - return false; - } else { - return true; - } + return !(column.compositeUniqueKey || column.compositePrimaryKey || !column.primaryKey); }; const hydrateUniqueOptions = (options, columnName, isActivated, jsonSchema) => diff --git a/forward_engineering/helpers/tableHelper.js b/forward_engineering/helpers/tableHelper.js index 32a43add..58b925eb 100644 --- a/forward_engineering/helpers/tableHelper.js +++ b/forward_engineering/helpers/tableHelper.js @@ -40,9 +40,9 @@ module.exports = app => { return `${cloneParams.atOrBefore} (offset => ${cloneParams.OFFSET})`; } else if (cloneParams.STATEMENT) { return `${cloneParams.atOrBefore} (statement => '${cloneParams.STATEMENT}')`; - } else { - return ''; } + + return ''; }; const mergeKeys = keys => keys.map(key => `"${key.name}"`).join(', '); diff --git a/forward_engineering/utils/escapeString.js b/forward_engineering/utils/escapeString.js new file mode 100644 index 00000000..4dd3c4f2 --- /dev/null +++ b/forward_engineering/utils/escapeString.js @@ -0,0 +1,8 @@ +const { FORMATS } = require('../helpers/constants'); + +const escapeString = (scriptFormat, value) => + scriptFormat === FORMATS.SNOWSIGHT ? `'${value.replace(/'/g, "''")}'` : `$$${value}$$`; + +module.exports = { + escapeString, +};