-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/implement generated as (#152)
* HCK-9065: update adapters to handle new GENERATED AS property structure (#151) * HCK-9065: add 'identity' to 'generated as' adapter * HCK-9065: update polyglot adapters * HCK-9063: Add support for GENERATED ALWAYS AS an expression (#150) * HCK-9063: add field level config for generated as property * HCK-9063: add generated as clause in script * HCK-9063: improve config dependency * HCK-9063: fix generated on null property
- Loading branch information
1 parent
b2ed55a
commit dcae3a8
Showing
7 changed files
with
878 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright © 2016-2018 by IntegrIT S.A. dba Hackolade. All rights reserved. | ||
* | ||
* The copyright to the computer software herein is the property of IntegrIT S.A. | ||
* The software may be used and/or copied only with the written permission of | ||
* IntegrIT S.A. or in accordance with the terms and conditions stipulated in | ||
* the agreement/contract under which the software has been supplied. | ||
* | ||
* { | ||
* "add": { | ||
* "entity": [<names of new property>], | ||
* "container": [<names of new property>], | ||
* "model": [<names of new property>], | ||
* "view": [<names of new property>], | ||
* "field": { | ||
* "<type>": [<names of new property>] | ||
* } | ||
* }, | ||
* "delete": { | ||
* "entity": [<names of new property>], | ||
* "container": [<names of new property>], | ||
* "model": [<names of new property>], | ||
* "view": [<names of new property>], | ||
* "field": { | ||
* "<type>": [<names of new property>] | ||
* } | ||
* }, | ||
* "modify": { | ||
* "entity": [ | ||
* { | ||
* "from": { <properties that identify record> }, | ||
* "to": { <properties that need to be changed> } | ||
* } | ||
* ], | ||
* "container": [], | ||
* "model": [], | ||
* "view": [], | ||
* "field": [] | ||
* }, | ||
* } | ||
*/ | ||
{ | ||
"add": {}, | ||
"modify": { | ||
"field": [ | ||
[ | ||
"assignProperties", | ||
{ | ||
"key": "identity", | ||
"valueType": "object" | ||
}, | ||
{ | ||
"generatedDefaultValue": { | ||
"asIdentity": true, | ||
"generatedType": "" | ||
} | ||
} | ||
], | ||
[ | ||
"movePropertyByPath", | ||
{ | ||
"from": "identity", | ||
"to": "generatedDefaultValue.identity" | ||
} | ||
], | ||
[ | ||
"movePropertyByPath", | ||
{ | ||
"from": "generatedDefaultValue.identity.generated", | ||
"to": "generatedDefaultValue.generatedType" | ||
} | ||
], | ||
[ | ||
"movePropertyByPath", | ||
{ | ||
"from": "generatedDefaultValue.identity.generatedOnNull", | ||
"to": "generatedDefaultValue.generatedOnNull" | ||
} | ||
], | ||
[ | ||
"addMissingIdByPath", | ||
{ | ||
"from": "generatedDefaultValue", | ||
"path": "generatedDefaultValue.GUID" | ||
} | ||
] | ||
] | ||
}, | ||
"delete": { | ||
"number": [ | ||
{ | ||
"path": "generatedDefaultValue.identity.generated" | ||
}, | ||
{ | ||
"path": "generatedDefaultValue.identity.generatedOnNull" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
forward_engineering/ddlProvider/ddlHelpers/columnDefinitionHelpers/getColumnDefault.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const { isEmpty, isNumber, trim } = require('lodash'); | ||
|
||
/** | ||
* @enum | ||
*/ | ||
const GENERATED_TYPE = { | ||
always: 'ALWAYS', | ||
byDefault: 'BY DEFAULT', | ||
}; | ||
|
||
/** | ||
* @typedef {{ | ||
* identityStart?: number; | ||
* identityIncrement?: number; | ||
* numberToCache?: number; | ||
* }} Identity | ||
* | ||
* @typedef {{ | ||
* generatedType: GENERATED_TYPE | ''; | ||
* asIdentity?: boolean; | ||
* generatedOnNull?: boolean; | ||
* expression?: string; | ||
* identity?: Identity; | ||
* }} GeneratedDefaultValue | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {{ type: string; }} | ||
* @returns {boolean} | ||
*/ | ||
const canHaveIdentity = ({ type }) => { | ||
const typesAllowedToHaveAutoIncrement = ['number']; | ||
return typesAllowedToHaveAutoIncrement.includes(type.toLowerCase()); | ||
}; | ||
|
||
/** | ||
* @param {{ onNull: boolean; }} | ||
* @returns {string} | ||
*/ | ||
const getOnNullClause = ({ onNull }) => (onNull ? ' ON NULL' : ''); | ||
|
||
/** | ||
* @param {{ generatedType: string; generatedOnNull?: boolean; }} | ||
* @returns {string} | ||
*/ | ||
const getGeneratedClause = ({ generatedType, generatedOnNull }) => { | ||
const onNull = getOnNullClause({ onNull: generatedOnNull }); | ||
|
||
switch (generatedType) { | ||
case GENERATED_TYPE.byDefault: { | ||
return ` GENERATED BY DEFAULT${onNull}`; | ||
} | ||
case GENERATED_TYPE.always: { | ||
return ' GENERATED ALWAYS'; | ||
} | ||
default: { | ||
return ''; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* @param {{ identityStart?: number; identityIncrement?: number; numberToCache?: number; }} | ||
* @returns {string} | ||
*/ | ||
const getIdentityOptions = ({ identityStart, identityIncrement, numberToCache }) => { | ||
const startWith = isNumber(identityStart) ? ` START WITH ${identityStart}` : ''; | ||
const incrementBy = isNumber(identityIncrement) ? ` INCREMENT BY ${identityIncrement}` : ''; | ||
const cache = isNumber(numberToCache) ? ` CACHE ${numberToCache}` : ' NOCACHE'; | ||
|
||
return trim(`${startWith}${incrementBy}${cache}`); | ||
}; | ||
|
||
/** | ||
* @param {{ type: string; default: any; defaultOnNull?: boolean; generatedDefaultValue?: GeneratedDefaultValue }} | ||
* @returns {string} | ||
*/ | ||
const getColumnDefault = ({ type, default: defaultValue, defaultOnNull, generatedDefaultValue = {} }) => { | ||
const { generatedType, generatedOnNull, asIdentity, identity, expression } = generatedDefaultValue; | ||
const generatedClause = getGeneratedClause({ generatedType, generatedOnNull }); | ||
const expressionValue = trim(expression); | ||
|
||
if (generatedType && asIdentity && canHaveIdentity({ type }) && !isEmpty(identity)) { | ||
const identityOptions = getIdentityOptions(identity); | ||
|
||
return `${generatedClause} AS IDENTITY (${identityOptions})`; | ||
} | ||
|
||
if (expressionValue) { | ||
return `${generatedClause} AS (${expressionValue})`; | ||
} | ||
|
||
if (defaultValue || defaultValue === 0) { | ||
const onNull = getOnNullClause({ onNull: defaultOnNull }); | ||
|
||
return ` DEFAULT${onNull} ${defaultValue}`; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
module.exports = { | ||
getColumnDefault, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.