@sap/cds-compiler 6.2.2 → 6.3.0
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 +35 -0
- package/bin/cdsc.js +11 -4
- package/lib/api/options.js +1 -1
- package/lib/base/message-registry.js +36 -7
- package/lib/base/messages.js +11 -4
- package/lib/base/model.js +0 -1
- package/lib/checks/assocOutsideService.js +17 -30
- package/lib/checks/checkForTypes.js +0 -18
- package/lib/checks/checkPathsInStoredCalcElement.js +2 -1
- package/lib/checks/onConditions.js +2 -2
- package/lib/checks/queryNoDbArtifacts.js +16 -15
- package/lib/checks/types.js +1 -1
- package/lib/checks/utils.js +30 -6
- package/lib/checks/validator.js +4 -5
- package/lib/compiler/checks.js +47 -18
- package/lib/compiler/index.js +88 -6
- package/lib/compiler/resolve.js +7 -7
- package/lib/compiler/tweak-assocs.js +47 -25
- package/lib/gen/BaseParser.js +1 -1
- package/lib/gen/CdlGrammar.checksum +1 -1
- package/lib/gen/CdlParser.js +381 -378
- package/lib/gen/Dictionary.json +0 -2
- package/lib/model/csnRefs.js +9 -4
- package/lib/model/csnUtils.js +67 -2
- package/lib/optionProcessor.js +2 -3
- package/lib/parsers/AstBuildingParser.js +5 -6
- package/lib/render/toCdl.js +10 -4
- package/lib/render/utils/common.js +4 -2
- package/lib/transform/db/assertUnique.js +2 -1
- package/lib/transform/db/associations.js +37 -1
- package/lib/transform/db/assocsToQueries/transformExists.js +21 -32
- package/lib/transform/db/assocsToQueries/utils.js +1 -1
- package/lib/transform/db/cdsPersistence.js +1 -1
- package/lib/transform/db/expansion.js +37 -36
- package/lib/transform/draft/db.js +20 -20
- package/lib/transform/draft/odata.js +38 -40
- package/lib/transform/effective/associations.js +1 -1
- package/lib/transform/effective/flattening.js +40 -47
- package/lib/transform/effective/main.js +6 -4
- package/lib/transform/forOdata.js +135 -115
- package/lib/transform/forRelationalDB.js +151 -142
- package/lib/transform/localized.js +116 -109
- package/lib/transform/odata/adaptAnnotationRefs.js +21 -16
- package/lib/transform/odata/createForeignKeys.js +73 -70
- package/lib/transform/odata/flattening.js +216 -200
- package/lib/transform/odata/foreignKeyRefsInXprAnnos.js +47 -45
- package/lib/transform/odata/toFinalBaseType.js +40 -39
- package/lib/transform/odata/typesExposure.js +151 -133
- package/lib/transform/odata/utils.js +7 -6
- package/lib/transform/parseExpr.js +165 -162
- package/lib/transform/transformUtils.js +184 -551
- package/lib/transform/translateAssocsToJoins.js +510 -571
- package/lib/transform/tupleExpansion.js +495 -0
- package/lib/transform/universalCsn/universalCsnEnricher.js +1 -0
- package/package.json +1 -1
- package/lib/base/cleanSymbols.js +0 -17
- package/lib/checks/nonexpandableStructured.js +0 -39
|
@@ -2,77 +2,78 @@
|
|
|
2
2
|
|
|
3
3
|
const { isBuiltinType } = require('../../base/builtins');
|
|
4
4
|
const { setProp } = require('../../base/model');
|
|
5
|
-
const {
|
|
5
|
+
const {
|
|
6
|
+
applyTransformations, implicitAs, copyAnnotations, isDeepEqual,
|
|
7
|
+
} = require('../../model/csnUtils');
|
|
6
8
|
const { EdmTypeFacetNames } = require('../../edm/EdmPrimitiveTypeDefinitions');
|
|
7
9
|
const { adaptAnnotationsRefs } = require('./adaptAnnotationRefs');
|
|
8
10
|
|
|
9
11
|
function createForeignKeyElements(csn, options, messageFunctions, csnUtils, iterateOptions = {}) {
|
|
12
|
+
const { error } = messageFunctions;
|
|
13
|
+
|
|
14
|
+
applyTransformations(csn, { elements: createForeignKeysInCsn, params: createForeignKeysInCsn },
|
|
15
|
+
[], iterateOptions);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Process a given elements or params dictionary and create foreign key elements.
|
|
19
|
+
*
|
|
20
|
+
* @param {object} parent The thing HAVING params or elements
|
|
21
|
+
* @param {string} prop
|
|
22
|
+
* @param {object} dict The params or elements thing
|
|
23
|
+
* @param {CSN.Path} path
|
|
24
|
+
*/
|
|
25
|
+
function createForeignKeysInCsn( parent, prop, dict, path ) {
|
|
26
|
+
const orderedElements = [];
|
|
27
|
+
// First, generate the FK elements for given element
|
|
28
|
+
Object.entries(dict).forEach(([ elementName, element ]) => {
|
|
29
|
+
orderedElements.push([ elementName, element ]);
|
|
30
|
+
if (!csnUtils.isManagedAssociation(element))
|
|
31
|
+
return;
|
|
32
|
+
const elementPath = path.concat(prop, elementName);
|
|
33
|
+
const generatedForeignKeys = createForeignKeysForElement(elementPath, element, elementName, csn, options, '_');
|
|
34
|
+
|
|
35
|
+
// Second, finalize the generated FK elements
|
|
36
|
+
const refCount = generatedForeignKeys.reduce((acc, fk) => {
|
|
37
|
+
// count duplicates
|
|
38
|
+
if (acc[fk.prefix])
|
|
39
|
+
acc[fk.prefix]++;
|
|
40
|
+
else
|
|
41
|
+
acc[fk.prefix] = 1;
|
|
42
|
+
|
|
43
|
+
// check for name clash with existing elements
|
|
44
|
+
if (parent[prop][fk.prefix] && isDeepEqual(element, parent[prop][fk.prefix], true)) {
|
|
45
|
+
// error location is the colliding element
|
|
46
|
+
error('name-duplicate-element', elementPath, { '#': 'flatten-fkey-exists', name: fk.prefix, art: elementName });
|
|
47
|
+
}
|
|
48
|
+
// attach a proper $path
|
|
49
|
+
setProp(element, '$path', elementPath);
|
|
50
|
+
return acc;
|
|
51
|
+
}, Object.create(null));
|
|
10
52
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
applyTransformations(csn, { elements: createForeignKeysInCsn, params: createForeignKeysInCsn},
|
|
14
|
-
[], iterateOptions);
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Process a given elements or params dictionary and create foreign key elements.
|
|
18
|
-
*
|
|
19
|
-
* @param {object} parent The thing HAVING params or elements
|
|
20
|
-
* @param {string} prop
|
|
21
|
-
* @param {object} dict The params or elements thing
|
|
22
|
-
* @param {CSN.Path} path
|
|
23
|
-
*/
|
|
24
|
-
function createForeignKeysInCsn( parent, prop, dict, path ) {
|
|
25
|
-
const orderedElements = [];
|
|
26
|
-
// First, generate the FK elements for given element
|
|
27
|
-
Object.entries(dict).forEach(([elementName, element]) => {
|
|
28
|
-
orderedElements.push([ elementName, element ]);
|
|
29
|
-
if (!csnUtils.isManagedAssociation(element)) return;
|
|
30
|
-
const elementPath = path.concat(prop, elementName);
|
|
31
|
-
const generatedForeignKeys = createForeignKeysForElement(elementPath, element, elementName, csn, options, '_');
|
|
32
|
-
|
|
33
|
-
// Second, finalize the generated FK elements
|
|
34
|
-
const refCount = generatedForeignKeys.reduce((acc, fk) => {
|
|
35
|
-
// count duplicates
|
|
36
|
-
if (acc[fk.prefix])
|
|
37
|
-
acc[fk.prefix]++;
|
|
38
|
-
else
|
|
39
|
-
acc[fk.prefix] = 1;
|
|
40
|
-
|
|
41
|
-
// check for name clash with existing elements
|
|
42
|
-
if (parent[prop][fk.prefix] && isDeepEqual(element, parent[prop][fk.prefix], true)) {
|
|
43
|
-
// error location is the colliding element
|
|
44
|
-
error('name-duplicate-element', elementPath, { '#': 'flatten-fkey-exists', name: fk.prefix, art: elementName });
|
|
45
|
-
}
|
|
46
|
-
// attach a proper $path
|
|
47
|
-
setProp(element, '$path', elementPath);
|
|
48
|
-
return acc;
|
|
49
|
-
}, Object.create(null));
|
|
50
|
-
|
|
51
|
-
// set default for single foreign key from association (if available)
|
|
52
|
-
if (element.default?.val !== undefined && generatedForeignKeys.length === 1)
|
|
53
|
+
// set default for single foreign key from association (if available)
|
|
54
|
+
if (element.default?.val !== undefined && generatedForeignKeys.length === 1)
|
|
53
55
|
generatedForeignKeys[0].foreignKey.default = element.default;
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
});
|
|
60
|
-
if (element.keys) {
|
|
61
|
-
if (options.transformation === 'effective')
|
|
62
|
-
delete element.default;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
adaptAnnotationsRefs(generatedForeignKeys, csnUtils, messageFunctions);
|
|
66
|
-
setProp(element, '$generatedForeignKeys', generatedForeignKeys.map(gfk => {
|
|
67
|
-
return { name: gfk.prefix, origin: gfk.originalKey, source: gfk.sourceElement } } ));
|
|
68
|
-
orderedElements.push(...generatedForeignKeys.map(gfk => [ gfk.prefix, gfk.foreignKey ]));
|
|
57
|
+
// check for duplicate foreign keys
|
|
58
|
+
Object.entries(refCount).forEach(([ name, occ ]) => {
|
|
59
|
+
if (occ > 1)
|
|
60
|
+
error('name-duplicate-element', elementPath, { '#': 'flatten-fkey-gen', name, art: elementName });
|
|
69
61
|
});
|
|
62
|
+
if (element.keys) {
|
|
63
|
+
if (options.transformation === 'effective')
|
|
64
|
+
delete element.default;
|
|
65
|
+
}
|
|
70
66
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
adaptAnnotationsRefs(generatedForeignKeys, csnUtils, messageFunctions);
|
|
68
|
+
setProp(element, '$generatedForeignKeys', generatedForeignKeys.map(gfk => ({ name: gfk.prefix, origin: gfk.originalKey, source: gfk.sourceElement }) ));
|
|
69
|
+
orderedElements.push(...generatedForeignKeys.map(gfk => [ gfk.prefix, gfk.foreignKey ]));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
parent[prop] = orderedElements.reduce((elementsAccumulator, [ name, element ]) => {
|
|
73
|
+
elementsAccumulator[name] = element;
|
|
74
|
+
return elementsAccumulator;
|
|
75
|
+
}, Object.create(null));
|
|
76
|
+
}
|
|
76
77
|
|
|
77
78
|
function createForeignKeysForElement(path, element, prefix, csn, options, pathDelimiter, lvl = 0, originalKey = {} ) {
|
|
78
79
|
const special$self = !csn?.definitions?.$self && '$self';
|
|
@@ -112,14 +113,14 @@ function createForeignKeyElements(csn, options, messageFunctions, csnUtils, iter
|
|
|
112
113
|
const continuePath = getContinuePath([ 'keys', keyIndex ]);
|
|
113
114
|
const alias = key.as || implicitAs(key.ref);
|
|
114
115
|
const result = csnUtils.inspectRef(continuePath);
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
const gfks = createForeignKeysForElement(result, result.art, alias, csn, options, pathDelimiter, lvl + 1,
|
|
117
|
+
lvl === 0 ? key : originalKey);
|
|
117
118
|
if (lvl === 0) {
|
|
118
119
|
gfks.forEach(gfk => gfk.keyAnnotations.push( ...copyAnnotations(key, gfk.foreignKey)));
|
|
119
|
-
Object.keys(key).forEach( prop => {
|
|
120
|
+
Object.keys(key).forEach( (prop) => {
|
|
120
121
|
// once applied -> remove the annotations from the keys array, to keep the OData CSN size as small as possible
|
|
121
122
|
if (prop[0] === '@')
|
|
122
|
-
delete key[prop]
|
|
123
|
+
delete key[prop];
|
|
123
124
|
});
|
|
124
125
|
}
|
|
125
126
|
fks = fks.concat(gfks);
|
|
@@ -138,18 +139,20 @@ function createForeignKeyElements(csn, options, messageFunctions, csnUtils, iter
|
|
|
138
139
|
// we have reached a leaf element, create a foreign key
|
|
139
140
|
else if ((finalElement.type == null || isBuiltinType(finalElement.type)) && !finalElement.on) {
|
|
140
141
|
const newFk = Object.create(null);
|
|
141
|
-
[ 'type', 'length', 'scale', 'precision', 'srid', 'default', '@odata.Type', ...EdmTypeFacetNames.map(f => `@odata.${f}`) ].forEach((prop) => {
|
|
142
|
+
[ 'type', 'length', 'scale', 'precision', 'srid', 'default', '@odata.Type', ...EdmTypeFacetNames.map(f => `@odata.${ f }`) ].forEach((prop) => {
|
|
142
143
|
// copy props from original element to preserve derived types!
|
|
143
144
|
if (element[prop] !== undefined)
|
|
144
145
|
newFk[prop] = element[prop];
|
|
145
146
|
});
|
|
146
|
-
|
|
147
|
+
const result = {
|
|
148
|
+
prefix, foreignKey: newFk, originalKey, keyAnnotations: [], sourceElement: finalElement,
|
|
149
|
+
};
|
|
147
150
|
return [ result ];
|
|
148
151
|
}
|
|
149
152
|
|
|
150
153
|
fks.forEach((fk) => {
|
|
151
154
|
// prepend current prefix
|
|
152
|
-
fk.prefix = `${prefix}${pathDelimiter}${fk.prefix}`;
|
|
155
|
+
fk.prefix = `${ prefix }${ pathDelimiter }${ fk.prefix }`;
|
|
153
156
|
// if this is the entry association, decorate the final foreign keys with the association props/annos
|
|
154
157
|
if (lvl === 0) {
|
|
155
158
|
fk.foreignKey['@odata.foreignKey4'] = prefix;
|
|
@@ -158,7 +161,7 @@ function createForeignKeyElements(csn, options, messageFunctions, csnUtils, iter
|
|
|
158
161
|
fkPath.push(fk.prefix);
|
|
159
162
|
setProp(fk.foreignKey, '$path', fkPath);
|
|
160
163
|
|
|
161
|
-
const allowedOverwriteAnnotationNames = ['@odata.Type', ...EdmTypeFacetNames.map(f => `@odata.${f}`)];
|
|
164
|
+
const allowedOverwriteAnnotationNames = [ '@odata.Type', ...EdmTypeFacetNames.map(f => `@odata.${ f }`) ];
|
|
162
165
|
const validAnnoNames = Object.keys(element).filter(pn => pn[0] === '@' && !allowedOverwriteAnnotationNames.includes(pn));
|
|
163
166
|
copyAnnotations(element, fk.foreignKey, false, {}, validAnnoNames);
|
|
164
167
|
const overwriteAnnoNames = Object.keys(element).filter(pn => allowedOverwriteAnnotationNames.includes(pn));
|