@sap/cds-compiler 2.7.0 → 2.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +167 -0
- package/bin/cdsc.js +42 -25
- package/bin/cdsse.js +1 -0
- package/doc/CHANGELOG_BETA.md +10 -0
- package/lib/api/.eslintrc.json +2 -0
- package/lib/api/main.js +17 -33
- package/lib/api/options.js +25 -13
- package/lib/api/validate.js +33 -9
- package/lib/backends.js +9 -8
- package/lib/base/dictionaries.js +2 -1
- package/lib/base/keywords.js +32 -2
- package/lib/base/message-registry.js +26 -2
- package/lib/base/messages.js +25 -9
- package/lib/base/model.js +5 -3
- package/lib/base/optionProcessorHelper.js +56 -22
- package/lib/checks/onConditions.js +5 -0
- package/lib/checks/selectItems.js +4 -0
- package/lib/checks/types.js +26 -2
- package/lib/checks/unknownMagic.js +41 -0
- package/lib/checks/validator.js +7 -2
- package/lib/compiler/assert-consistency.js +18 -5
- package/lib/compiler/base.js +65 -0
- package/lib/compiler/builtins.js +30 -1
- package/lib/compiler/checks.js +5 -2
- package/lib/compiler/definer.js +145 -120
- package/lib/compiler/index.js +16 -4
- package/lib/compiler/propagator.js +5 -2
- package/lib/compiler/resolver.js +207 -47
- package/lib/compiler/shared.js +47 -200
- package/lib/compiler/utils.js +173 -0
- package/lib/edm/annotations/genericTranslation.js +183 -187
- package/lib/edm/csn2edm.js +94 -98
- package/lib/edm/edm.js +16 -20
- package/lib/edm/edmPreprocessor.js +302 -115
- package/lib/edm/edmUtils.js +31 -12
- package/lib/gen/language.checksum +1 -1
- package/lib/gen/language.interp +28 -1
- package/lib/gen/language.tokens +79 -69
- package/lib/gen/languageLexer.interp +28 -1
- package/lib/gen/languageLexer.js +879 -805
- package/lib/gen/languageLexer.tokens +71 -62
- package/lib/gen/languageParser.js +5308 -4308
- package/lib/json/from-csn.js +59 -30
- package/lib/json/to-csn.js +354 -105
- package/lib/language/antlrParser.js +11 -0
- package/lib/language/errorStrategy.js +1 -0
- package/lib/language/genericAntlrParser.js +81 -14
- package/lib/language/language.g4 +163 -31
- package/lib/main.d.ts +136 -17
- package/lib/main.js +7 -1
- package/lib/model/api.js +78 -0
- package/lib/model/csnRefs.js +115 -32
- package/lib/model/csnUtils.js +71 -33
- package/lib/model/enrichCsn.js +36 -9
- package/lib/model/revealInternalProperties.js +20 -4
- package/lib/modelCompare/compare.js +2 -1
- package/lib/optionProcessor.js +33 -16
- package/lib/render/.eslintrc.json +3 -1
- package/lib/render/DuplicateChecker.js +1 -1
- package/lib/render/toCdl.js +60 -17
- package/lib/render/toHdbcds.js +122 -74
- package/lib/render/toSql.js +57 -32
- package/lib/render/utils/common.js +6 -10
- package/lib/sql-identifier.js +6 -1
- package/lib/transform/db/constraints.js +273 -119
- package/lib/transform/db/draft.js +9 -6
- package/lib/transform/db/expansion.js +19 -7
- package/lib/transform/db/flattening.js +31 -7
- package/lib/transform/db/transformExists.js +344 -66
- package/lib/transform/db/views.js +438 -0
- package/lib/transform/forHanaNew.js +65 -436
- package/lib/transform/forOdataNew.js +21 -10
- package/lib/transform/localized.js +2 -0
- package/lib/transform/odata/attachPath.js +19 -4
- package/lib/transform/odata/generateForeignKeyElements.js +11 -10
- package/lib/transform/odata/referenceFlattener.js +44 -38
- package/lib/transform/odata/sortByAssociationDependency.js +2 -2
- package/lib/transform/odata/structuralPath.js +72 -0
- package/lib/transform/odata/structureFlattener.js +13 -10
- package/lib/transform/odata/typesExposure.js +22 -12
- package/lib/transform/transformUtilsNew.js +55 -9
- package/lib/transform/translateAssocsToJoins.js +11 -17
- package/lib/transform/universalCsnEnricher.js +67 -0
- package/lib/utils/file.js +5 -3
- package/lib/utils/term.js +65 -42
- package/lib/utils/timetrace.js +48 -26
- package/package.json +1 -1
|
@@ -16,36 +16,40 @@ const ASSOCIATION = 'cds.Association';
|
|
|
16
16
|
function createReferentialConstraints(csn, options) {
|
|
17
17
|
let validated = true;
|
|
18
18
|
let enforced = true;
|
|
19
|
-
if (options.
|
|
19
|
+
if (options.integrityNotValidated)
|
|
20
20
|
validated = false;
|
|
21
21
|
|
|
22
|
-
if (options.
|
|
22
|
+
if (options.integrityNotEnforced)
|
|
23
23
|
enforced = false;
|
|
24
24
|
|
|
25
25
|
const { inspectRef } = csnRefs(csn);
|
|
26
26
|
// prepare the functions with the compositions and associations across all entities first
|
|
27
|
-
// and execute it afterwards
|
|
27
|
+
// and execute it afterwards.
|
|
28
|
+
// compositions must be processed first, as the <up_> links for them must result in `ON DELETE CASCADE`
|
|
28
29
|
const compositions = [];
|
|
29
30
|
const associations = [];
|
|
30
31
|
forEachDefinition(csn, (artifact, artifactName) => {
|
|
31
|
-
if (!artifact.query && artifact.kind === 'entity'
|
|
32
|
+
if (!artifact.query && artifact.kind === 'entity') {
|
|
32
33
|
forAllElements(artifact, artifactName, (parent, elements, path) => {
|
|
33
|
-
// Step I: iterate compositions, enrich dependent keys
|
|
34
|
+
// Step I: iterate compositions, enrich dependent keys for <up_> association in target entity of composition
|
|
34
35
|
for (const elementName in elements) {
|
|
35
36
|
const element = elements[elementName];
|
|
36
|
-
if (element.type === COMPOSITION &&
|
|
37
|
-
compositions.push(
|
|
38
|
-
|
|
37
|
+
if (element.type === COMPOSITION && element.$selfOnCondition) {
|
|
38
|
+
compositions.push({
|
|
39
|
+
fn: () => {
|
|
40
|
+
foreignKeyConstraintForUpLinkOfComposition(element, parent, path.concat([ elementName ]));
|
|
41
|
+
},
|
|
39
42
|
});
|
|
40
43
|
}
|
|
41
44
|
}
|
|
42
45
|
// Step II: iterate associations, enrich dependent keys (in entity containing the association)
|
|
43
46
|
for (const elementName in elements) {
|
|
44
47
|
const element = elements[elementName];
|
|
45
|
-
if (element.type === ASSOCIATION ||
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
if (element.keys && isToOne(element) && element.type === ASSOCIATION || element.type === COMPOSITION && treatCompositionLikeAssociation(element)) {
|
|
49
|
+
associations.push({
|
|
50
|
+
fn: () => {
|
|
51
|
+
foreignKeyConstraintForAssociation(element, path.concat([ elementName ]));
|
|
52
|
+
},
|
|
49
53
|
});
|
|
50
54
|
}
|
|
51
55
|
}
|
|
@@ -53,39 +57,34 @@ function createReferentialConstraints(csn, options) {
|
|
|
53
57
|
}
|
|
54
58
|
});
|
|
55
59
|
// create constraints on foreign keys
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
// always process unmanaged first, up_ links must be flagged
|
|
61
|
+
// before they are processed
|
|
62
|
+
compositions.forEach(composition => composition.fn());
|
|
63
|
+
associations.forEach(association => association.fn());
|
|
64
|
+
|
|
58
65
|
// Step III: Create the final referential constraints from all dependent key <-> parent key pairs stemming from the same $sourceAssociation
|
|
59
66
|
forEachDefinition(csn, collectAndAttachReferentialConstraints);
|
|
60
67
|
|
|
61
|
-
|
|
62
68
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
69
|
+
* Retrieve the <up_> link of an `cds.Composition` used in an on-condition like `$self = <comp>.<up_>`
|
|
70
|
+
* and calculate a foreign key constraint for this association if it is constraint compliant.
|
|
71
|
+
* The constraint will have an `ON DELETE CASCADE`.
|
|
66
72
|
*
|
|
67
|
-
* @param {CSN.Element} composition
|
|
73
|
+
* @param {CSN.Element} composition which might has the `$self = <comp>.<up_>` on-condition
|
|
68
74
|
* @param {CSN.Artifact} parent artifact containing the composition
|
|
69
75
|
* @param {CSN.Path} path
|
|
70
76
|
*/
|
|
71
|
-
function
|
|
72
|
-
|
|
77
|
+
function foreignKeyConstraintForUpLinkOfComposition(composition, parent, path) {
|
|
78
|
+
const dependent = csn.definitions[path[1]];
|
|
79
|
+
if (skipConstraintGeneration(parent, dependent, composition))
|
|
73
80
|
return;
|
|
74
81
|
|
|
75
|
-
const { elements } = parent;
|
|
76
82
|
const onCondition = composition.on;
|
|
77
|
-
if (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const parentKeys = Array.from(elementsOfSourceSide(onCondition, elements));
|
|
83
|
-
const { backlinkName } = composition.$selfOnCondition || {};
|
|
84
|
-
// sanity check; do not generate constraints for on-conditions like "dependent.idOne = id AND dependent.idTwo = id"
|
|
85
|
-
// also: no constraints for compositions of many w/o backlink
|
|
86
|
-
if (dependentKeys.length === parentKeys.length && backlinkName)
|
|
87
|
-
attachConstraintsToDependentKeys(dependentKeys, parentKeys, path[path.length - 3], path, backlinkName, 'CASCADE');
|
|
88
|
-
}
|
|
83
|
+
if (composition.$selfOnCondition && composition.$selfOnCondition.up_.length === 1) {
|
|
84
|
+
const upLinkName = composition.$selfOnCondition.up_[0];
|
|
85
|
+
const up_ = csn.definitions[composition.target].elements[upLinkName];
|
|
86
|
+
if (up_.keys && isToOne(up_)) // no constraint for unmanaged / to-many up_ links
|
|
87
|
+
foreignKeyConstraintForAssociation(up_, [ 'definitions', composition.target, 'elements', upLinkName ], path[path.length - 1]);
|
|
89
88
|
}
|
|
90
89
|
else if (!onCondition && composition.keys.length > 0) {
|
|
91
90
|
throw new Error('Please debug me, an on-condition was expected here, but only found keys');
|
|
@@ -95,26 +94,27 @@ function createReferentialConstraints(csn, options) {
|
|
|
95
94
|
/**
|
|
96
95
|
* Calculate referential constraints for dependent keys in the entity where the cds.Associations is defined.
|
|
97
96
|
* The DELETE rule for a referential constraint stemming from a cds.Association will be 'RESTRICT'
|
|
97
|
+
* If the association is used as an <up_> link in a compositions on-condition, the ON DELETE rule will be `CASCADE`
|
|
98
98
|
*
|
|
99
99
|
* @param {CSN.Association} association for that a constraint should be generated
|
|
100
|
-
* @param {CSN.Elements} elements of parent entity.
|
|
101
100
|
* @param {CSN.Path} path
|
|
102
|
-
* @param {
|
|
101
|
+
* @param {CSN.PathSegment} upLinkFor the name of the composition which used this association in a `$self = <comp>.<up_>` comparison
|
|
103
102
|
*/
|
|
104
|
-
function foreignKeyConstraintForAssociation(association,
|
|
105
|
-
const
|
|
106
|
-
|
|
103
|
+
function foreignKeyConstraintForAssociation(association, path, upLinkFor = null) {
|
|
104
|
+
const parent = csn.definitions[association.target];
|
|
105
|
+
const dependent = csn.definitions[path[1]];
|
|
106
|
+
if (skipConstraintGeneration(parent, dependent, association))
|
|
107
107
|
return;
|
|
108
|
-
|
|
108
|
+
const { elements } = csn.definitions[path[1]];
|
|
109
109
|
const onCondition = association.on;
|
|
110
110
|
if (onCondition && hasConstraintCompliantOnCondition(association, elements, path)) {
|
|
111
111
|
// 1. cds.Association has constraint compliant on-condition
|
|
112
112
|
// mark each dependent key - in the entity containing the association - referenced in the on-condition
|
|
113
113
|
const dependentKeys = Array.from(elementsOfSourceSide(onCondition, elements));
|
|
114
|
-
const parentKeys = Array.from(elementsOfTargetSide(onCondition,
|
|
114
|
+
const parentKeys = Array.from(elementsOfTargetSide(onCondition, parent.elements));
|
|
115
115
|
// sanity check; do not generate constraints for on-conditions like "dependent.idOne = id AND dependent.idTwo = id"
|
|
116
116
|
if (dependentKeys.length === parentKeys.length)
|
|
117
|
-
attachConstraintsToDependentKeys(dependentKeys, parentKeys, association.target, path,
|
|
117
|
+
attachConstraintsToDependentKeys(dependentKeys, parentKeys, association.target, path[path.length - 1], upLinkFor);
|
|
118
118
|
}
|
|
119
119
|
else if (!onCondition && association.keys.length > 0) {
|
|
120
120
|
throw new Error('Please debug me, an on-condition was expected here, but only found keys');
|
|
@@ -129,14 +129,16 @@ function createReferentialConstraints(csn, options) {
|
|
|
129
129
|
* @param {Array} dependentKeys array holding dependent keys in the format [['key1', 'value1'], [...], ...]
|
|
130
130
|
* @param {Array} parentKeys array holding parent keys in the format [['key1', 'value1'], [...], ...]
|
|
131
131
|
* @param {CSN.PathSegment} parentTable the sql-table where the foreign key constraints will be pointing to
|
|
132
|
-
* @param {CSN.
|
|
133
|
-
* @param {
|
|
134
|
-
*
|
|
132
|
+
* @param {CSN.PathSegment} sourceAssociation the name of the association from which the constraint originates
|
|
133
|
+
* @param {CSN.PathSegment} upLinkFor the name of the composition which used this association in a `$self = <comp>.<up_>` comparison
|
|
134
|
+
* it is used for a comment in the constraint, which is only printed out in test-mode
|
|
135
135
|
*/
|
|
136
|
-
function attachConstraintsToDependentKeys(dependentKeys, parentKeys, parentTable,
|
|
136
|
+
function attachConstraintsToDependentKeys(dependentKeys, parentKeys, parentTable, sourceAssociation, upLinkFor = null) {
|
|
137
137
|
while (dependentKeys.length > 0) {
|
|
138
138
|
const dependentKeyValuePair = dependentKeys.pop();
|
|
139
139
|
const dependentKey = dependentKeyValuePair[1];
|
|
140
|
+
// if it already has a dependent key assigned, do not overwrite it.
|
|
141
|
+
// this is the case for <up_> associations in on-conditions of compositions
|
|
140
142
|
if (Object.prototype.hasOwnProperty.call(dependentKey, '$foreignKeyConstraint'))
|
|
141
143
|
return;
|
|
142
144
|
|
|
@@ -146,9 +148,9 @@ function createReferentialConstraints(csn, options) {
|
|
|
146
148
|
const constraint = {
|
|
147
149
|
parentKey: parentKeyName,
|
|
148
150
|
parentTable,
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
onDelete,
|
|
151
|
+
upLinkFor,
|
|
152
|
+
sourceAssociation,
|
|
153
|
+
onDelete: upLinkFor ? 'CASCADE' : 'RESTRICT',
|
|
152
154
|
validated,
|
|
153
155
|
enforced,
|
|
154
156
|
};
|
|
@@ -168,7 +170,7 @@ function createReferentialConstraints(csn, options) {
|
|
|
168
170
|
* b) for cds.Association this is the target entity
|
|
169
171
|
* 3. parent keys must be the full primary key tuple
|
|
170
172
|
*
|
|
171
|
-
* @param {CSN.Association
|
|
173
|
+
* @param {CSN.Association} element
|
|
172
174
|
* @param {CSN.Elements} siblingElements
|
|
173
175
|
* @param {CSN.Path} path the path to the element
|
|
174
176
|
* @returns {boolean} indicating whether the association / composition is a constraint candidate
|
|
@@ -189,65 +191,240 @@ function createReferentialConstraints(csn, options) {
|
|
|
189
191
|
if (onCondition.some((step, index) => typeof step === 'object' && inspectRef(path.concat([ 'on', index ])).scope === '$magic'))
|
|
190
192
|
return false;
|
|
191
193
|
|
|
192
|
-
// managed composition with target cardinality of one is treated like an association
|
|
193
|
-
const isComposition = element.type === COMPOSITION && !treatCompositionLikeAssociation(element);
|
|
194
194
|
// for cds.Associations the parent keys are in the associations target entity
|
|
195
195
|
// for cds.Composition the parent keys are in the entity, where the composition is defined
|
|
196
|
-
const parentElements =
|
|
197
|
-
const parentKeys =
|
|
196
|
+
const parentElements = csn.definitions[element.target].elements;
|
|
197
|
+
const parentKeys = elementsOfTargetSide(onCondition, parentElements);
|
|
198
|
+
|
|
199
|
+
const referencesNonPrimaryKeyField = Array.from(parentKeys.values()).some(parentKey => !parentKey.key);
|
|
200
|
+
if (referencesNonPrimaryKeyField)
|
|
201
|
+
return false;
|
|
202
|
+
|
|
198
203
|
// returns true if the parentKeys found in the on-condition are covering the full primary key tuple in the parent entity
|
|
199
204
|
return Array.from(parentKeys.entries())
|
|
200
|
-
|
|
201
|
-
.filter(([ keyName, pk ]) => pk.key && parentElements[keyName].key).length ===
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
205
|
+
// check if primary key found in on-condition is present in association target / composition source
|
|
206
|
+
.filter(([ keyName, pk ]) => pk.key && parentElements[keyName].key).length ===
|
|
207
|
+
Object.keys(parentElements)
|
|
208
|
+
// compare that with the length of the primary key tuple found in association target / composition source
|
|
209
|
+
.filter(key => parentElements[key].key &&
|
|
210
|
+
parentElements[key].type !== ASSOCIATION &&
|
|
211
|
+
parentElements[key].type !== COMPOSITION)
|
|
212
|
+
.length;
|
|
207
213
|
}
|
|
208
|
-
|
|
209
214
|
/**
|
|
210
215
|
* Skip referential constraint if the parent table (association target, or artifact where composition is defined)
|
|
211
216
|
* of the relation is:
|
|
212
217
|
* - a query
|
|
213
|
-
* - annotated with '@cds.persistence.skip:true'
|
|
214
|
-
* - annotated with '@cds.persistence.exists:true'
|
|
218
|
+
* - TODO: Revisit -- annotated with '@cds.persistence.skip:true'
|
|
219
|
+
* - TODO: Revisit -- annotated with '@cds.persistence.exists:true'
|
|
220
|
+
*
|
|
221
|
+
* The following decision table reflects the current implementation:
|
|
215
222
|
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
223
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
224
|
+
* | Global Switch: | Global Check Type: | @assert.integrity | Generate |
|
|
225
|
+
* |"assertIntegrity"| "assertIntegrityType"| | Constraint|
|
|
226
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
227
|
+
* | on | RT | false | no |
|
|
228
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
229
|
+
* | on | RT | true/not set | no |
|
|
230
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
231
|
+
* | on | RT | RT | no |
|
|
232
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
233
|
+
* | on | RT | DB | yes |
|
|
234
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
235
|
+
* | | | | |
|
|
236
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
237
|
+
* | on | DB | false | no |
|
|
238
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
239
|
+
* | on | DB | true/not set | yes |
|
|
240
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
241
|
+
* | on | DB | RT | no |
|
|
242
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
243
|
+
* | on | DB | DB | yes |
|
|
244
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
245
|
+
* | | | | |
|
|
246
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
247
|
+
* | off | don't care | don't care | no |
|
|
248
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
249
|
+
* | | | | |
|
|
250
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
251
|
+
* | individual | RT | true | no |
|
|
252
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
253
|
+
* | individual | DB | true | yes |
|
|
254
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
255
|
+
* | individual | don't care | RT | no |
|
|
256
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
257
|
+
* | individual | don't care | DB | yes |
|
|
258
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
259
|
+
* | individual | don't care | false/not set | no |
|
|
260
|
+
* +-----------------+--------------------+-------------------+----------+
|
|
220
261
|
*
|
|
221
|
-
* @param {CSN.
|
|
222
|
-
* @param
|
|
262
|
+
* @param {CSN.Definition} parent entity where the foreign key reference will point at
|
|
263
|
+
* @param {CSN.Definition} dependent entity where the constraint will be defined on
|
|
264
|
+
* @param {CSN.Association} element the composition or association
|
|
223
265
|
* @returns {boolean}
|
|
224
266
|
*/
|
|
225
|
-
function skipConstraintGeneration(parent, element) {
|
|
226
|
-
if
|
|
227
|
-
|
|
228
|
-
// in case of managed composition, the 'up_' link should not result in a constraint
|
|
229
|
-
const target = csn.definitions[element.target];
|
|
230
|
-
const { up_ } = target.elements;
|
|
231
|
-
if (up_)
|
|
232
|
-
up_.$skipReferentialConstraintForUp_ = true;
|
|
267
|
+
function skipConstraintGeneration(parent, dependent, element) {
|
|
268
|
+
// if set to 'off' don't even bother, just skip all constraints
|
|
269
|
+
if (options.assertIntegrity === false || options.assertIntegrity === 'false')
|
|
233
270
|
return true;
|
|
234
|
-
}
|
|
235
271
|
|
|
236
|
-
if (
|
|
237
|
-
|
|
272
|
+
if (parent.query)
|
|
273
|
+
return true;
|
|
274
|
+
|
|
275
|
+
// no constraint if either dependent or parent is not persisted
|
|
276
|
+
if (
|
|
277
|
+
hasAnnotationValue(parent, '@cds.persistence.skip') ||
|
|
278
|
+
hasAnnotationValue(dependent, '@cds.persistence.skip')
|
|
279
|
+
)
|
|
280
|
+
return true;
|
|
281
|
+
|
|
282
|
+
// some commonly used string literals
|
|
283
|
+
const RT = 'RT';
|
|
284
|
+
const DB = 'DB';
|
|
285
|
+
const CREATE_FOR_UP = '$createReferentialConstraintForUp_';
|
|
286
|
+
const SKIP_FOR_UP = '$skipReferentialConstraintForUp_';
|
|
287
|
+
|
|
288
|
+
// if the element itself is explicitly excluded from being checked
|
|
289
|
+
// skip the constraint for it (and its backlink)
|
|
290
|
+
if (isAssertIntegrityAnnotationSetTo(false) ||
|
|
291
|
+
isAssertIntegrityAnnotationSetTo(RT) ||
|
|
292
|
+
element[SKIP_FOR_UP]
|
|
293
|
+
) {
|
|
294
|
+
// for "auto-generated" associations like for the up_ of a composition of aspects,
|
|
295
|
+
// the annotation on the composition influences the referential constraint for the
|
|
296
|
+
// up_ association
|
|
297
|
+
if (element.$selfOnCondition && element.targetAspect)
|
|
298
|
+
assignPropOnBacklinkIfPossible(SKIP_FOR_UP, true);
|
|
299
|
+
|
|
238
300
|
return true;
|
|
239
301
|
}
|
|
240
302
|
|
|
241
|
-
if (
|
|
242
|
-
|
|
243
|
-
|
|
303
|
+
if ((!options.assertIntegrity || options.assertIntegrity === true || options.assertIntegrity === 'true') &&
|
|
304
|
+
(!options.assertIntegrityType || options.assertIntegrityType === RT))
|
|
305
|
+
return assertForIntegrityTypeRT();
|
|
306
|
+
|
|
307
|
+
if ((!options.assertIntegrity || options.assertIntegrity === true || options.assertIntegrity === 'true') &&
|
|
308
|
+
options.assertIntegrityType === DB)
|
|
309
|
+
return assertForIntegrityTypeDB();
|
|
310
|
+
|
|
311
|
+
if ((options.assertIntegrity === 'individual'))
|
|
312
|
+
return assertForIndividual();
|
|
313
|
+
|
|
314
|
+
// The default for the assertIntegrityType is 'RT', no constraints in that case
|
|
315
|
+
if ((!options.assertIntegrity || options.assertIntegrity === true) &&
|
|
316
|
+
(!options.assertIntegrityType || options.assertIntegrityType === RT))
|
|
244
317
|
return true;
|
|
245
318
|
|
|
246
|
-
|
|
247
|
-
if (!hasAnnotationValue(element, '@cds.persistency.assert.integrity', true) && options.forHana.skipDbConstraints)
|
|
319
|
+
if (!element.keys || !isToOne(element))
|
|
248
320
|
return true;
|
|
249
321
|
|
|
250
322
|
return false;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* if global checks are 'individual' we evaluate every association,
|
|
326
|
+
* we create db constraints if it is annotated with @assert.integrity: 'DB' (or true)
|
|
327
|
+
*
|
|
328
|
+
* @returns {boolean}
|
|
329
|
+
*/
|
|
330
|
+
function assertForIndividual() {
|
|
331
|
+
if (isAssertIntegrityAnnotationSetTo(DB) || element[CREATE_FOR_UP]) {
|
|
332
|
+
// if this is has a $self comparison, the up_ link should then result in a constraint
|
|
333
|
+
assignPropOnBacklinkIfPossible(CREATE_FOR_UP, true);
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
if (options.assertIntegrityType === DB && isAssertIntegrityAnnotationSetTo(true)) {
|
|
337
|
+
// if this is has a $self comparison, the up_ link should then result in a constraint
|
|
338
|
+
assignPropOnBacklinkIfPossible(CREATE_FOR_UP, true);
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// individual and no ('DB') annotation on constraint --> skip
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* if global check type is 'RT' (or not provided) only generate DB constraint if element
|
|
348
|
+
* is explicitly annotated "@assert.integrity: 'DB'"
|
|
349
|
+
*
|
|
350
|
+
* @returns {boolean}
|
|
351
|
+
*/
|
|
352
|
+
function assertForIntegrityTypeRT() {
|
|
353
|
+
// for "auto-generated" associations like for the up_ of a composition of aspects,
|
|
354
|
+
// the annotation on the composition influences the referential constraint for the
|
|
355
|
+
// up_ association
|
|
356
|
+
if (isAssertIntegrityAnnotationSetTo(DB)) {
|
|
357
|
+
if (element.targetAspect)
|
|
358
|
+
assignPropOnBacklinkIfPossible(CREATE_FOR_UP, true);
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
if (element[CREATE_FOR_UP])
|
|
362
|
+
return false;
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* if global checks are on and global integrity check type is 'DB'
|
|
368
|
+
* we create db constraints in any case except if annotated
|
|
369
|
+
* with @assert.integrity: 'RT' (or false, but that is rejected earlier)
|
|
370
|
+
*
|
|
371
|
+
* @returns {boolean}
|
|
372
|
+
*/
|
|
373
|
+
function assertForIntegrityTypeDB() {
|
|
374
|
+
if (isAssertIntegrityAnnotationSetTo(RT))
|
|
375
|
+
return true;
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* convenience to check if value of element's @assert.integrity annotation
|
|
382
|
+
* is the same as a given value
|
|
383
|
+
*
|
|
384
|
+
* @param {string|boolean} value
|
|
385
|
+
* @returns {boolean}
|
|
386
|
+
*/
|
|
387
|
+
function isAssertIntegrityAnnotationSetTo(value) {
|
|
388
|
+
return hasAnnotationValue(element, '@assert.integrity', value);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Assigns a helper key-value pair on the up_ association for a $self comparison
|
|
393
|
+
* for the current 'element', if applicable
|
|
394
|
+
*
|
|
395
|
+
* @param {string} prop
|
|
396
|
+
* @param {object} val
|
|
397
|
+
*/
|
|
398
|
+
function assignPropOnBacklinkIfPossible(prop, val) {
|
|
399
|
+
if (!element.$selfOnCondition)
|
|
400
|
+
return;
|
|
401
|
+
const target = csn.definitions[element.target];
|
|
402
|
+
const backlink = target.elements[element.$selfOnCondition.up_[0]];
|
|
403
|
+
backlink[prop] = val;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* If we have a managed composition with a target cardinality of one, we will treat it like
|
|
409
|
+
* a regular association when it comes to referential constraints.
|
|
410
|
+
* The constraint will thus be generated for the foreign key we create in the source entity.
|
|
411
|
+
*
|
|
412
|
+
* @param {CSN.Composition} composition the composition which might be treated like an association
|
|
413
|
+
* @returns {boolean} true if the composition should be treated as an association in regards to foreign key constraints
|
|
414
|
+
*/
|
|
415
|
+
function treatCompositionLikeAssociation(composition) {
|
|
416
|
+
return Boolean(isToOne(composition) && composition.keys);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* returns true if the association/composition has a max target cardinality of one
|
|
421
|
+
*
|
|
422
|
+
* @param {CSN.Association|CSN.Composition} assocOrComposition
|
|
423
|
+
* @returns {boolean}
|
|
424
|
+
*/
|
|
425
|
+
function isToOne(assocOrComposition) {
|
|
426
|
+
const { min, max } = assocOrComposition.cardinality || {};
|
|
427
|
+
return !min && !max || max === 1;
|
|
251
428
|
}
|
|
252
429
|
|
|
253
430
|
/**
|
|
@@ -260,8 +437,8 @@ function createReferentialConstraints(csn, options) {
|
|
|
260
437
|
function elementsOfTargetSide(on, targetElements) {
|
|
261
438
|
const elements = new Map();
|
|
262
439
|
on.filter(element => typeof element === 'object' &&
|
|
263
|
-
|
|
264
|
-
|
|
440
|
+
element.ref.length > 1 &&
|
|
441
|
+
targetElements[element.ref[element.ref.length - 1]])
|
|
265
442
|
.forEach((element) => {
|
|
266
443
|
elements.set(element.ref[element.ref.length - 1], targetElements[element.ref[element.ref.length - 1]]);
|
|
267
444
|
});
|
|
@@ -272,15 +449,15 @@ function createReferentialConstraints(csn, options) {
|
|
|
272
449
|
/**
|
|
273
450
|
* Finds and return elementNames and elements of source side mentioned in on-condition.
|
|
274
451
|
*
|
|
275
|
-
* @param {CSN.
|
|
452
|
+
* @param {CSN.OnCondition} on the on-condition
|
|
276
453
|
* @param {CSN.Elements} sourceElements elements of source entity where the association/composition is defined.
|
|
277
454
|
* @returns {Map} of source elements with their name as key
|
|
278
455
|
*/
|
|
279
456
|
function elementsOfSourceSide(on, sourceElements) {
|
|
280
457
|
const elements = new Map();
|
|
281
458
|
on.filter(element => typeof element === 'object' &&
|
|
282
|
-
|
|
283
|
-
|
|
459
|
+
element.ref.length === 1 &&
|
|
460
|
+
sourceElements[element.ref[0]])
|
|
284
461
|
.forEach((element) => {
|
|
285
462
|
elements.set(element.ref[0], sourceElements[element.ref[0]]);
|
|
286
463
|
});
|
|
@@ -315,8 +492,8 @@ function createReferentialConstraints(csn, options) {
|
|
|
315
492
|
// find all other $foreignKeyConstraint with same $sourceAssociation and same parentTable
|
|
316
493
|
Object.entries(artifact.elements)
|
|
317
494
|
.filter(([ , e ]) => e.$foreignKeyConstraint &&
|
|
318
|
-
|
|
319
|
-
|
|
495
|
+
e.$foreignKeyConstraint.sourceAssociation === $foreignKeyConstraint.sourceAssociation &&
|
|
496
|
+
e.$foreignKeyConstraint.parentTable === $foreignKeyConstraint.parentTable)
|
|
320
497
|
.forEach(([ foreignKeyName, foreignKey ]) => {
|
|
321
498
|
const $foreignKeyConstraintCopy = Object.assign({}, foreignKey.$foreignKeyConstraint);
|
|
322
499
|
delete foreignKey.$foreignKeyConstraint;
|
|
@@ -329,9 +506,9 @@ function createReferentialConstraints(csn, options) {
|
|
|
329
506
|
let onDeleteRemark = null;
|
|
330
507
|
// comments in sqlite files are causing the JDBC driver to throw an error on deployment
|
|
331
508
|
if (options.testMode && onDelete === 'CASCADE')
|
|
332
|
-
onDeleteRemark = `Composition "${$foreignKeyConstraint.
|
|
333
|
-
referentialConstraints[`${getResultingName(csn, 'quoted', artifactName)}_${$foreignKeyConstraint.
|
|
334
|
-
identifier: `${getResultingName(csn, options.forHana.names, artifactName)}_${$foreignKeyConstraint.
|
|
509
|
+
onDeleteRemark = `Up_ link for Composition "${$foreignKeyConstraint.upLinkFor}" implies existential dependency`;
|
|
510
|
+
referentialConstraints[`${getResultingName(csn, 'quoted', artifactName)}_${$foreignKeyConstraint.sourceAssociation}`] = {
|
|
511
|
+
identifier: `${getResultingName(csn, options.forHana.names, artifactName)}_${$foreignKeyConstraint.sourceAssociation}`,
|
|
335
512
|
foreignKey: dependentKey,
|
|
336
513
|
parentKey,
|
|
337
514
|
dependentTable: artifactName,
|
|
@@ -350,29 +527,6 @@ function createReferentialConstraints(csn, options) {
|
|
|
350
527
|
artifact.$tableConstraints.referential = referentialConstraints;
|
|
351
528
|
}
|
|
352
529
|
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* If we have a managed composition with a target cardinality of one, we will treat it like
|
|
356
|
-
* a regular association when it comes to referential constraints.
|
|
357
|
-
* The constraints will thus be generated in the entity containing the composition and not in the target entity.
|
|
358
|
-
*
|
|
359
|
-
* @param {CSN.Composition} composition the composition which might be treated like an association
|
|
360
|
-
* @returns {boolean} true if the composition should be treated as an association in regards to foreign key constraints
|
|
361
|
-
*/
|
|
362
|
-
function treatCompositionLikeAssociation(composition) {
|
|
363
|
-
return Boolean((isToOne(composition) && !composition.$selfOnCondition) || composition.keys);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* returns true if the association/composition has a max target cardinality of one
|
|
368
|
-
*
|
|
369
|
-
* @param {CSN.Element} assocOrComposition
|
|
370
|
-
* @returns {boolean}
|
|
371
|
-
*/
|
|
372
|
-
function isToOne(assocOrComposition) {
|
|
373
|
-
const { min, max } = assocOrComposition.cardinality || {};
|
|
374
|
-
return !min && !max || max === 1;
|
|
375
|
-
}
|
|
376
530
|
}
|
|
377
531
|
|
|
378
532
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
|
-
hasAnnotationValue,
|
|
5
|
-
getResultingName,
|
|
4
|
+
hasAnnotationValue, getUtils, getServiceNames, forEachDefinition,
|
|
5
|
+
getResultingName, forEachMemberRecursively,
|
|
6
6
|
} = require('../../model/csnUtils');
|
|
7
7
|
const { setProp, isDeprecatedEnabled } = require('../../base/model');
|
|
8
8
|
const { getTransformers } = require('../transformUtilsNew');
|
|
@@ -21,6 +21,7 @@ function generateDrafts(csn, options, pathDelimiter, messageFunctions) {
|
|
|
21
21
|
const draftSuffix = isDeprecatedEnabled(options, 'generatedEntityNameWithUnderscore') ? '_drafts' : '.drafts';
|
|
22
22
|
// All services of the model - needed for drafts
|
|
23
23
|
const allServices = getServiceNames(csn);
|
|
24
|
+
const draftRoots = new WeakMap();
|
|
24
25
|
const {
|
|
25
26
|
createForeignKeyElement, createAndAddDraftAdminDataProjection, createScalarElement, createAssociationElement,
|
|
26
27
|
addElement, copyAndAddElement, createAssociationPathComparison,
|
|
@@ -117,10 +118,12 @@ function generateDrafts(csn, options, pathDelimiter, messageFunctions) {
|
|
|
117
118
|
|
|
118
119
|
// extract keys for UUID inspection
|
|
119
120
|
const keys = [];
|
|
120
|
-
|
|
121
|
+
forEachMemberRecursively(artifact, (elt, name, prop, path) => {
|
|
122
|
+
if (!elt.elements && !elt.type && !elt.virtual) // only check leafs
|
|
123
|
+
error(null, path, 'Expecting element to have a type when used in a draft-enabled artifact');
|
|
121
124
|
if (elt.key && elt.key === true && !elt.virtual)
|
|
122
125
|
keys.push(elt);
|
|
123
|
-
});
|
|
126
|
+
}, [ 'definitions', artifactName ], true, { elementsOnly: true });
|
|
124
127
|
|
|
125
128
|
// In contrast to EDM, the DB entity may have more than one technical keys but should have idealy exactly one key of type cds.UUID
|
|
126
129
|
if (keys.length !== 1)
|
|
@@ -153,7 +156,7 @@ function generateDrafts(csn, options, pathDelimiter, messageFunctions) {
|
|
|
153
156
|
const persistenceName = getResultingName(csn, options.forHana.names, draftsArtifactName);
|
|
154
157
|
// Duplicate the artifact as a draft shadow entity
|
|
155
158
|
if (csn.definitions[persistenceName]) {
|
|
156
|
-
const definingDraftRoot = csn.definitions[persistenceName]
|
|
159
|
+
const definingDraftRoot = draftRoots.get(csn.definitions[persistenceName]);
|
|
157
160
|
if (!definingDraftRoot) {
|
|
158
161
|
error(null, [ 'definitions', artifactName ], { name: persistenceName },
|
|
159
162
|
'Generated entity name $(NAME) conflicts with existing entity');
|
|
@@ -174,7 +177,7 @@ function generateDrafts(csn, options, pathDelimiter, messageFunctions) {
|
|
|
174
177
|
// Add draft shadow entity to the csn
|
|
175
178
|
csn.definitions[draftsArtifactName] = draftsArtifact;
|
|
176
179
|
|
|
177
|
-
|
|
180
|
+
draftRoots.set(draftsArtifact, draftRootName);
|
|
178
181
|
if (artifact.$location)
|
|
179
182
|
setProp(draftsArtifact, '$location', artifact.$location);
|
|
180
183
|
|