From a316c575ac4186b7be44c14590f208c96ad116d9 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko <91055067+serhii-filonenko@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:47:45 +0300 Subject: [PATCH] HCK-6246: validation error on default property extraneous quotes (#17)
Sub-bugHCK-6246 Validation error on default property: extraneous quotes for string and illegitimate error for number

--- constants/types.js | 13 +++++++++++++ .../columnDefinition/getColumnDefault.js | 14 +++++++------- .../ddlHelpers/comment/commentHelper.js | 6 ++---- forward_engineering/utils/general.js | 7 +++++++ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/constants/types.js b/constants/types.js index 1a09f8d..3950be3 100644 --- a/constants/types.js +++ b/constants/types.js @@ -53,10 +53,23 @@ const DATA_TYPES_WITH_PRECISION = [DATA_TYPE.decimal, DATA_TYPE.float, DATA_TYPE const DATA_TYPES_WITH_IDENTITY = [DATA_TYPE.integer, DATA_TYPE.smallint, DATA_TYPE.bigint, DATA_TYPE.decimal]; +const STRING_DATA_TYPES = [ + DATA_TYPE.char, + DATA_TYPE.varchar, + DATA_TYPE.nchar, + DATA_TYPE.nvarchar, + DATA_TYPE.clob, + DATA_TYPE.graphic, + DATA_TYPE.vargraphic, + DATA_TYPE.dbclob, + DATA_TYPE.blob, +]; + module.exports = { DATA_TYPE, DATA_TYPES_WITH_BYTE, DATA_TYPES_WITH_LENGTH, DATA_TYPES_WITH_PRECISION, DATA_TYPES_WITH_IDENTITY, + STRING_DATA_TYPES, }; diff --git a/forward_engineering/ddlProvider/ddlHelpers/columnDefinition/getColumnDefault.js b/forward_engineering/ddlProvider/ddlHelpers/columnDefinition/getColumnDefault.js index 55ca45e..d7bfd9c 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/columnDefinition/getColumnDefault.js +++ b/forward_engineering/ddlProvider/ddlHelpers/columnDefinition/getColumnDefault.js @@ -1,6 +1,6 @@ const { toUpper } = require('lodash'); -const { wrapInQuotes } = require('../../../utils/general'); -const { DATA_TYPES_WITH_IDENTITY } = require('../../../../constants/types'); +const { wrapInSingleQuotes } = require('../../../utils/general'); +const { DATA_TYPES_WITH_IDENTITY, STRING_DATA_TYPES } = require('../../../../constants/types'); /** * @typedef {string | number} DefaultValue @@ -36,13 +36,13 @@ const getIdentityOptions = ({ start, increment, minValue, maxValue, cycle }) => }; /** - * @param {{ defaultValue: DefaultValue }} + * @param {{ defaultValue: DefaultValue, type: string }} * @returns {DefaultValue} */ -const wrapInQuotesDefaultValue = ({ defaultValue }) => { - const doesContainSpaces = /\s+/g.test(defaultValue); +const wrapInQuotesDefaultValue = ({ defaultValue, type }) => { + const isStringDataType = STRING_DATA_TYPES.includes(toUpper(type)); - return doesContainSpaces ? wrapInQuotes({ name: defaultValue }) : defaultValue; + return isStringDataType ? wrapInSingleQuotes({ name: defaultValue }) : defaultValue; }; /** @@ -59,7 +59,7 @@ const getColumnDefault = ({ default: defaultValue, identity, type }) => { } if (defaultValue || defaultValue === 0) { - const value = wrapInQuotesDefaultValue({ defaultValue }); + const value = wrapInQuotesDefaultValue({ defaultValue, type }); return ` WITH DEFAULT ${value}`; } diff --git a/forward_engineering/ddlProvider/ddlHelpers/comment/commentHelper.js b/forward_engineering/ddlProvider/ddlHelpers/comment/commentHelper.js index 39f3433..ff39e86 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/comment/commentHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/comment/commentHelper.js @@ -1,7 +1,7 @@ const { trim } = require('lodash'); const templates = require('../../templates'); const { assignTemplates } = require('../../../utils/assignTemplates'); -const { wrapInQuotes, commentIfDeactivated } = require('../../../utils/general'); +const { wrapInQuotes, commentIfDeactivated, wrapInSingleQuotes } = require('../../../utils/general'); /** * @enum {string} @@ -12,8 +12,6 @@ const OBJECT_TYPE = { index: 'INDEX', }; -const wrapComment = comment => `'${comment}'`; - /** * @param {{ objectName: string, objectType: OBJECT_TYPE, description?: string }} * @returns {string} @@ -25,7 +23,7 @@ const getCommentStatement = ({ objectName, objectType, description }) => { return assignTemplates({ template: templates.comment, - templateData: { objectType, objectName: trim(objectName), comment: wrapComment(description) }, + templateData: { objectType, objectName: trim(objectName), comment: wrapInSingleQuotes({ name: description }) }, }); }; diff --git a/forward_engineering/utils/general.js b/forward_engineering/utils/general.js index a907db7..85af3fe 100644 --- a/forward_engineering/utils/general.js +++ b/forward_engineering/utils/general.js @@ -74,6 +74,12 @@ const commentIfDeactivated = (statement, { isActivated, isPartOfLine, inlineComm */ const wrapInQuotes = ({ name }) => `"${name}"`; +/** + * @param {{ name: string }} + * @returns {string} + */ +const wrapInSingleQuotes = ({ name }) => `'${name}'`; + /** * @param {{ name: string, schemaName?: string }} * @returns {string} @@ -117,6 +123,7 @@ module.exports = { divideIntoActivatedAndDeactivated, commentIfDeactivated, wrapInQuotes, + wrapInSingleQuotes, getNamePrefixedWithSchemaName, getColumnsList, toArray,