@sap/cds-compiler 2.5.0 → 2.10.4

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.
Files changed (92) hide show
  1. package/CHANGELOG.md +191 -9
  2. package/bin/cdsc.js +2 -2
  3. package/doc/CHANGELOG_BETA.md +33 -3
  4. package/lib/api/main.js +29 -101
  5. package/lib/api/options.js +15 -11
  6. package/lib/api/validate.js +12 -8
  7. package/lib/backends.js +0 -81
  8. package/lib/base/keywords.js +32 -2
  9. package/lib/base/message-registry.js +63 -9
  10. package/lib/base/messages.js +63 -21
  11. package/lib/base/model.js +2 -3
  12. package/lib/checks/defaultValues.js +27 -2
  13. package/lib/checks/elements.js +1 -6
  14. package/lib/checks/foreignKeys.js +0 -6
  15. package/lib/checks/managedWithoutKeys.js +17 -0
  16. package/lib/checks/nonexpandableStructured.js +38 -0
  17. package/lib/checks/onConditions.js +9 -45
  18. package/lib/checks/queryNoDbArtifacts.js +25 -7
  19. package/lib/checks/selectItems.js +25 -2
  20. package/lib/checks/types.js +26 -2
  21. package/lib/checks/unknownMagic.js +38 -0
  22. package/lib/checks/utils.js +61 -0
  23. package/lib/checks/validator.js +60 -7
  24. package/lib/compiler/assert-consistency.js +16 -7
  25. package/lib/compiler/builtins.js +2 -0
  26. package/lib/compiler/checks.js +6 -4
  27. package/lib/compiler/definer.js +99 -42
  28. package/lib/compiler/index.js +73 -27
  29. package/lib/compiler/resolver.js +288 -157
  30. package/lib/compiler/shared.js +31 -11
  31. package/lib/edm/annotations/genericTranslation.js +182 -186
  32. package/lib/edm/csn2edm.js +103 -108
  33. package/lib/edm/edm.js +18 -21
  34. package/lib/edm/edmPreprocessor.js +361 -114
  35. package/lib/edm/edmUtils.js +103 -33
  36. package/lib/gen/Dictionary.json +22 -0
  37. package/lib/gen/language.checksum +1 -1
  38. package/lib/gen/language.interp +12 -1
  39. package/lib/gen/language.tokens +57 -53
  40. package/lib/gen/languageLexer.interp +10 -1
  41. package/lib/gen/languageLexer.js +770 -744
  42. package/lib/gen/languageLexer.tokens +49 -46
  43. package/lib/gen/languageParser.js +4713 -4279
  44. package/lib/json/from-csn.js +103 -45
  45. package/lib/json/to-csn.js +296 -117
  46. package/lib/language/antlrParser.js +4 -3
  47. package/lib/language/errorStrategy.js +1 -0
  48. package/lib/language/genericAntlrParser.js +21 -12
  49. package/lib/language/language.g4 +99 -31
  50. package/lib/main.d.ts +81 -3
  51. package/lib/main.js +30 -7
  52. package/lib/model/api.js +78 -0
  53. package/lib/model/csnRefs.js +329 -142
  54. package/lib/model/csnUtils.js +235 -58
  55. package/lib/model/enrichCsn.js +18 -1
  56. package/lib/model/revealInternalProperties.js +2 -1
  57. package/lib/modelCompare/compare.js +37 -20
  58. package/lib/optionProcessor.js +9 -3
  59. package/lib/render/.eslintrc.json +4 -1
  60. package/lib/render/DuplicateChecker.js +8 -5
  61. package/lib/render/toCdl.js +112 -33
  62. package/lib/render/toHdbcds.js +134 -64
  63. package/lib/render/toSql.js +91 -38
  64. package/lib/render/utils/common.js +8 -13
  65. package/lib/render/utils/sql.js +3 -3
  66. package/lib/sql-identifier.js +6 -1
  67. package/lib/transform/db/assertUnique.js +5 -6
  68. package/lib/transform/db/constraints.js +29 -13
  69. package/lib/transform/db/draft.js +8 -6
  70. package/lib/transform/db/expansion.js +582 -0
  71. package/lib/transform/db/flattening.js +325 -0
  72. package/lib/transform/db/groupByOrderBy.js +2 -2
  73. package/lib/transform/db/transformExists.js +284 -63
  74. package/lib/transform/forHanaNew.js +98 -381
  75. package/lib/transform/forOdataNew.js +21 -22
  76. package/lib/transform/localized.js +37 -10
  77. package/lib/transform/odata/attachPath.js +19 -4
  78. package/lib/transform/odata/generateForeignKeyElements.js +11 -10
  79. package/lib/transform/odata/referenceFlattener.js +60 -39
  80. package/lib/transform/odata/sortByAssociationDependency.js +2 -2
  81. package/lib/transform/odata/structuralPath.js +72 -0
  82. package/lib/transform/odata/structureFlattener.js +19 -18
  83. package/lib/transform/odata/typesExposure.js +22 -12
  84. package/lib/transform/transformUtilsNew.js +134 -78
  85. package/lib/transform/translateAssocsToJoins.js +17 -14
  86. package/lib/transform/universalCsnEnricher.js +67 -0
  87. package/lib/utils/file.js +0 -11
  88. package/lib/utils/moduleResolve.js +6 -8
  89. package/package.json +1 -1
  90. package/lib/json/walker.js +0 -26
  91. package/lib/transform/sqlite +0 -0
  92. package/lib/utils/string.js +0 -17
@@ -8,8 +8,6 @@
8
8
 
9
9
  'use strict';
10
10
 
11
- const walker = require('../json/walker');
12
-
13
11
  /**
14
12
  * database name - uppercase if not quoted
15
13
  *
@@ -87,13 +85,13 @@ class DuplicateChecker {
87
85
  * @param {CSN.Options} options Options used for the compilation
88
86
  */
89
87
  check(error, options = null) {
90
- walker.forEach(this.seenArtifacts, (artifactName, artifacts) => {
88
+ forEach(this.seenArtifacts, (artifactName, artifacts) => {
91
89
  if (artifacts.length > 1) {
92
90
  artifacts.slice(1).forEach((artifact) => { // report all colliding artifacts, except the first one
93
91
  const collidesWith = this.seenArtifacts[artifactName].find( art => art !== artifact );
94
92
  let namingMode;
95
93
  if (options)
96
- namingMode = options.toHana ? options.toHana.names : options.toSql.names;
94
+ namingMode = options.sqlMapping;
97
95
  else
98
96
  namingMode = 'plain';
99
97
 
@@ -106,7 +104,7 @@ class DuplicateChecker {
106
104
  });
107
105
  }
108
106
  artifacts.forEach((artifact) => {
109
- walker.forEach(artifact.elements, (elementName, elements) => {
107
+ forEach(artifact.elements, (elementName, elements) => {
110
108
  if (elements.length > 1) {
111
109
  elements.forEach((element) => { // report all colliding elements
112
110
  error(null,
@@ -123,4 +121,9 @@ class DuplicateChecker {
123
121
  }
124
122
  }
125
123
 
124
+ function forEach(obj, callback) {
125
+ for (const key in obj)
126
+ callback(key, obj[key]);
127
+ }
128
+
126
129
  module.exports = DuplicateChecker;
@@ -8,11 +8,13 @@ const {
8
8
  isBuiltinType, generatedByCompilerVersion, getNormalizedQuery,
9
9
  } = require('../model/csnUtils');
10
10
  const keywords = require('../base/keywords');
11
- const { renderFunc, processExprArray, findElement } = require('./utils/common');
11
+ const { renderFunc, beautifyExprArray, findElement } = require('./utils/common');
12
12
  const { checkCSNVersion } = require('../json/csnVersion');
13
13
  const timetrace = require('../utils/timetrace');
14
14
  const { csnRefs } = require('../model/csnRefs');
15
15
  const { forEachDefinition } = require('../model/csnUtils');
16
+ const enrichUniversalCsn = require('../transform/universalCsnEnricher');
17
+ const { isBetaEnabled } = require('../base/model');
16
18
 
17
19
  /**
18
20
  * Render the CSN model 'model' to CDS source text. One source is created per
@@ -33,6 +35,9 @@ function toCdsSourceCsn(csn, options) {
33
35
  // Skip compactModel if already using CSN
34
36
  // const csn = cloneCsn(model, options);
35
37
 
38
+ if (options.csnFlavor === 'universal' && isBetaEnabled(options, 'enableUniversalCsn'))
39
+ enrichUniversalCsn(csn, options);
40
+
36
41
  checkCSNVersion(csn, options);
37
42
 
38
43
  const result = Object.create(null);
@@ -50,16 +55,20 @@ function toCdsSourceCsn(csn, options) {
50
55
  result[main] += `${sourceStr}\n`;
51
56
  });
52
57
 
53
- // Apply possible subelement annotations with an "annotate X with"
58
+ // Apply possible subelement/action return annotations with an "annotate X with"
59
+ // Some of them appear in csn.extensions, some not...
54
60
  if (subelementAnnotates.length > 0) {
55
- for (const [ artName, element, elementName ] of subelementAnnotates) {
56
- let sourceStr = `annotate ${artName} with {\n`;
57
- sourceStr += ` ${elementName} {\n`;
61
+ for (const [ artName, element, elementName, suffix ] of subelementAnnotates) {
62
+ // Suffix is used with action return annotations
63
+ let sourceStr = `annotate ${artName} with ${suffix ? `${suffix} ` : ''}{\n`;
64
+ if (elementName) // action returns do not have element name - we need less {} there
65
+ sourceStr += ` ${elementName} {\n`;
58
66
  const env = increaseIndent(increaseIndent(createEnv()));
59
67
  const subelements = renderSubelementAnnotates(element, env);
60
68
  if (subelements !== '') {
61
69
  sourceStr += `${subelements}\n`;
62
- sourceStr += ' }\n';
70
+ if (elementName) // action returns do not have element name - we need less {} there
71
+ sourceStr += ' }\n';
63
72
  sourceStr += '}\n';
64
73
  result[main] += `${sourceStr}\n`;
65
74
  }
@@ -145,6 +154,12 @@ function toCdsSourceCsn(csn, options) {
145
154
  if (ext.elements)
146
155
  result += renderElementExtensions(ext.elements, env);
147
156
 
157
+ // Returns annotations
158
+ if (ext.returns) {
159
+ const childEnv = increaseIndent(env);
160
+ result += ` with returns${renderElementExtensions(ext.returns.elements, childEnv)}`;
161
+ }
162
+
148
163
  // Action annotations
149
164
  if (ext.actions) {
150
165
  result += ' actions {\n';
@@ -162,10 +177,19 @@ function toCdsSourceCsn(csn, options) {
162
177
 
163
178
  result += `${paramAnnotations.join(',\n')}\n${childEnv.indent})`;
164
179
  }
180
+ // Annotations on action returns
181
+ if (action.returns && action.returns.elements) {
182
+ const grandChildEnv = increaseIndent(childEnv);
183
+ result += ` returns${renderElementExtensions(action.returns.elements, grandChildEnv)}`;
184
+ }
185
+
186
+
165
187
  result += ';\n';
166
188
  }
167
189
  result += `${env.indent}}`;
168
190
  }
191
+
192
+
169
193
  result += ';';
170
194
  return result;
171
195
  }).join('\n');
@@ -205,6 +229,7 @@ function toCdsSourceCsn(csn, options) {
205
229
  function renderArtifact(artifactName, art, env) {
206
230
  // FIXME: Correctly build the paths during runtime to give better locations
207
231
  env.path = [ 'definitions', artifactName ];
232
+ env.artifactName = artifactName;
208
233
 
209
234
  switch (art.kind) {
210
235
  case 'entity':
@@ -509,6 +534,7 @@ function toCdsSourceCsn(csn, options) {
509
534
  * @param {Boolean} [isSubElement]
510
535
  */
511
536
  function renderElement(elementName, elm, env, isSubElement) {
537
+ env.elementName = elementName;
512
538
  let result = renderDocComment(elm, env) + renderAnnotationAssignments(elm, env);
513
539
  const quotedElementName = quoteOrUppercaseId(elementName);
514
540
  result += `${env.indent + (elm.virtual ? 'virtual ' : '') +
@@ -516,10 +542,11 @@ function toCdsSourceCsn(csn, options) {
516
542
  ((elm.masked && !elm._ignoreMasked) ? 'masked ' : '') +
517
543
  quotedElementName} : ${
518
544
  renderTypeReference(elm, env, undefined)
519
- }${renderNullability(elm)}`;
545
+ }${elm.on ? '' : renderNullability(elm)}`;
520
546
  if (elm.default)
521
547
  result += ` default ${renderExpr(elm.default, env)}`;
522
548
 
549
+ delete env.elementName;
523
550
  return `${result};\n`;
524
551
  }
525
552
 
@@ -688,7 +715,7 @@ function toCdsSourceCsn(csn, options) {
688
715
 
689
716
  // Even the first step might have parameters and/or a filter
690
717
  if (path.ref[0].args)
691
- result += `(${renderArgs(path.ref[0].args, ':', env)})`;
718
+ result += `(${renderArgs(path.ref[0], ':', env)})`;
692
719
 
693
720
  if (path.ref[0].where)
694
721
  result += `[${path.ref[0].cardinality ? (`${path.ref[0].cardinality.max}: `) : ''}${renderExpr(path.ref[0].where, env, true, true)}]`;
@@ -739,17 +766,13 @@ function toCdsSourceCsn(csn, options) {
739
766
  result += 'virtual ';
740
767
 
741
768
  const key = col.key ? 'key ' : '';
742
- result += key + renderExpr(col, env, true);
769
+ // Use special rendering for .expand/.inline - renderExpr cannot easily handle some cases
770
+ result += key + ((col.expand || col.inline) ? renderInlineExpand(col, env) : renderExpr(col, env, true));
743
771
 
744
- if (col.as)
772
+ // Alias is already handled by renderInlineExpand
773
+ if (!col.inline && !col.expand && col.as)
745
774
  result += ` as ${quoteOrUppercaseId(col.as)}`;
746
775
 
747
- if (col.inline)
748
- renderInlineExpand('inline');
749
-
750
- if (col.expand)
751
- renderInlineExpand('expand');
752
-
753
776
  // Explicit type provided for the view element?
754
777
  if (col.cast) {
755
778
  // Special case: Explicit association type is actually a redirect
@@ -764,24 +787,69 @@ function toCdsSourceCsn(csn, options) {
764
787
  }
765
788
  }
766
789
  return result;
790
+ }
767
791
 
768
- /**
769
- * For the current column, render a (nested) inline/expand.
770
- *
771
- * @param {'inline'|'expand'} prop
772
- */
773
- function renderInlineExpand(prop) {
774
- if (prop === 'inline')
792
+ /**
793
+ * For the current column, render a (nested) inline/expand. If the current column
794
+ * does not have an .expand/.inline, '' is returned
795
+ *
796
+ * @param {object} col Thing with .expand or .inline
797
+ * @param {CdlRenderEnvironment} parentEnv
798
+ * @returns {string}
799
+ */
800
+ function renderInlineExpand(col, parentEnv) {
801
+ if (!col.inline && !col.expand)
802
+ return '';
803
+
804
+ return renderIX(col, parentEnv);
805
+
806
+ function renderIX(obj, env) {
807
+ // No expression to render for { * } as alias
808
+ let result = (obj.as && obj.expand && !obj.ref) ? '' : renderExpr(obj, env);
809
+
810
+ // s as alias { * }
811
+ if (obj.as && (obj.ref || obj.xpr || obj.val !== undefined || obj.func !== undefined))
812
+ result += ` as ${obj.as}`;
813
+
814
+ // We found a leaf - no further drilling
815
+ if (!obj.inline && !obj.expand) {
816
+ if (obj.cast && obj.cast.type) {
817
+ result += ` : ${renderTypeReference(obj.cast, createEnv())}`;
818
+ }
819
+ else if (obj.cast && obj.cast.target) { // test tbd
820
+ result += ` : redirected to ${renderAbsoluteNameWithQuotes(obj.cast.target)}`;
821
+ if (obj.cast.on)
822
+ result += ` on ${renderExpr(obj.cast.on, env, true, true)}`;
823
+ else if (obj.cast.keys)
824
+ result += ` { ${Object.keys(obj.cast.keys).map(name => renderForeignKey(obj.cast.keys[name], env)).join(', ')} }`;
825
+ }
826
+ return result;
827
+ }
828
+
829
+ if (obj.inline)
775
830
  result += '.{\n';
776
831
  else
777
- result += ' {\n';
832
+ result += result !== '' ? ' {\n' : '{\n';
778
833
 
779
- col[prop].forEach((elm, i) => {
780
- result += `${renderViewColumn(elm, increaseIndent(env))}`;
781
- if (i < col[prop].length - 1)
834
+ // Drill down and render children of the expand/inline
835
+ const childEnv = increaseIndent(env);
836
+ const expandInline = obj.expand || obj.inline;
837
+ expandInline.forEach((elm, i) => {
838
+ result += `${childEnv.indent}${renderIX(elm, childEnv)}`;
839
+ if (i < expandInline.length - 1)
782
840
  result += ',\n';
783
841
  });
784
842
  result += `\n${env.indent}}`;
843
+
844
+ // Don't forget about the .excluding
845
+ if (obj.excluding)
846
+ result += ` excluding { ${obj.excluding.join(',')} }`;
847
+
848
+ // { * } as expand
849
+ if (!obj.ref && obj.as)
850
+ result += ` as ${obj.as}`;
851
+
852
+ return result;
785
853
  }
786
854
  }
787
855
 
@@ -1015,8 +1083,11 @@ function toCdsSourceCsn(csn, options) {
1015
1083
  const childEnv = increaseIndent(env);
1016
1084
  const parameters = Object.keys(act.params || []).map(name => renderParameter(name, act.params[name], childEnv)).join(',\n');
1017
1085
  result += (parameters === '') ? '()' : `(\n${parameters}\n${env.indent})`;
1018
- if (act.returns)
1086
+ if (act.returns) {
1087
+ if (act.returns.type && act.returns.elements) // action returns annotations
1088
+ subelementAnnotates.push([ actionName, act.returns, '', 'returns' ]);
1019
1089
  result += ` returns ${renderTypeReference(act.returns, env)}${renderNullability(act.returns)}`;
1090
+ }
1020
1091
 
1021
1092
  result += ';\n';
1022
1093
  return result;
@@ -1092,6 +1163,10 @@ function toCdsSourceCsn(csn, options) {
1092
1163
  let rc = `many ${renderTypeReference(elm.items, env)}`;
1093
1164
  if (elm.items.notNull != null)
1094
1165
  rc += elm.items.notNull ? ' not null' : ' null';
1166
+ // many sub element annotates
1167
+ if (elm.items.type && elm.items.elements && env.artifactName)
1168
+ subelementAnnotates.push([ env.artifactName, elm.items, env.elementName ]);
1169
+
1095
1170
  return rc;
1096
1171
  }
1097
1172
 
@@ -1275,7 +1350,7 @@ function toCdsSourceCsn(csn, options) {
1275
1350
  function renderExpr(x, env, inline = true, inExpr = false) {
1276
1351
  // Compound expression
1277
1352
  if (Array.isArray(x))
1278
- return processExprArray(x, renderExpr, env, inline, inExpr);
1353
+ return beautifyExprArray(x.map(item => renderExpr(item, env, inline, inExpr)));
1279
1354
 
1280
1355
  if (typeof x === 'object' && x !== null) {
1281
1356
  if (inExpr && x.cast && x.cast.type)
@@ -1399,18 +1474,19 @@ function toCdsSourceCsn(csn, options) {
1399
1474
 
1400
1475
  // Not really a path step but an object-like function call
1401
1476
  if (s.func)
1402
- return `${s.func}(${renderArgs(s.args, '=>', env)})`;
1477
+ return `${s.func}(${renderArgs(s, '=>', env)})`;
1403
1478
 
1404
1479
  // Path step, possibly with view parameters and/or filters
1405
1480
  let result = `${quoteOrUppercaseId(s.id)}`;
1406
1481
  if (s.args) {
1407
1482
  // View parameters
1408
- result += `(${renderArgs(s.args, ':', env)})`;
1483
+ result += `(${renderArgs(s, ':', env)})`;
1409
1484
  }
1410
1485
  if (s.where) {
1411
1486
  // Filter, possibly with cardinality
1412
1487
  result += `[${s.cardinality ? (`${s.cardinality.max}: `) : ''}${renderExpr(s.where, env, inline, true)}]`;
1413
1488
  }
1489
+
1414
1490
  return result;
1415
1491
  }
1416
1492
 
@@ -1422,12 +1498,13 @@ function toCdsSourceCsn(csn, options) {
1422
1498
  * Render function arguments or view parameters (positional if array, named if object/dict),
1423
1499
  * using 'sep' as separator for positional parameters
1424
1500
  *
1425
- * @param {object[]|object} args (Un)Named arguments
1501
+ * @param {object} node with `args` to render
1426
1502
  * @param {string} sep
1427
1503
  * @param {CdlRenderEnvironment} env
1428
1504
  * @returns {string}
1429
1505
  */
1430
- function renderArgs(args, sep, env) {
1506
+ function renderArgs(node, sep, env) {
1507
+ const args = node.args ? node.args : {};
1431
1508
  // Positional arguments
1432
1509
  if (Array.isArray(args))
1433
1510
  return args.map(arg => renderExpr(arg, env)).join(', ');
@@ -1819,6 +1896,8 @@ function toCdsSourceCsn(csn, options) {
1819
1896
  *
1820
1897
  * @property {string} indent Current indentation as a string, e.g. ' ' for two spaces.
1821
1898
  * @property {CSN.Path} [path] CSN path to the current artifact
1899
+ * @property {string} artifactName Name of the artifact - set in renderArtifact
1900
+ * @property {string} elementName Name of the element being rendered - set in renderElement
1822
1901
  * @property {{[name: string]: {
1823
1902
  quotedName: string,
1824
1903
  quotedAlias: string