@abaplint/core 2.120.98 → 2.120.100

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.
@@ -8459,6 +8459,9 @@ declare class View extends AbstractObject {
8459
8459
  TABNAME: string;
8460
8460
  FIELDNAME: string;
8461
8461
  KEYFLAG: string;
8462
+ EXVDATATYPE?: string;
8463
+ EXVFLENGTH?: string;
8464
+ EXVDECIMALS?: string;
8462
8465
  }[] | undefined;
8463
8466
  getJoin(): {
8464
8467
  LTAB: string;
@@ -35,6 +35,7 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Select = void 0;
37
37
  const Expressions = __importStar(require("../../2_statements/expressions"));
38
+ const nodes_1 = require("../../nodes");
38
39
  const basic_1 = require("../../types/basic");
39
40
  const inline_data_1 = require("./inline_data");
40
41
  const target_1 = require("./target");
@@ -50,6 +51,8 @@ const _syntax_input_1 = require("../_syntax_input");
50
51
  const version_1 = require("../../../version");
51
52
  const _check_database_fields_1 = require("./_check_database_fields");
52
53
  const isSimple = /^\w+$/;
54
+ // SQL functions where the result has the same type as the first argument
55
+ const sameTypeAsFirstArgument = new Set(["UPPER", "LOWER", "LTRIM", "RTRIM"]);
53
56
  class Select {
54
57
  static runSyntax(node, input, skipImplicitInto = false, selectLoop = false) {
55
58
  var _a;
@@ -255,7 +258,7 @@ class Select {
255
258
  if (intoStructure) {
256
259
  const inlineList = intoStructure.findAllExpressions(Expressions.InlineData);
257
260
  if (inlineList.length === 1) {
258
- inline_data_1.InlineData.runSyntax(inlineList[0], input, this.buildStructureType(fields, dbSources, input.scope));
261
+ inline_data_1.InlineData.runSyntax(inlineList[0], input, this.buildStructureType(fields, dbSources, input.scope, from));
259
262
  }
260
263
  else {
261
264
  for (const inline of inlineList) {
@@ -285,7 +288,12 @@ class Select {
285
288
  return;
286
289
  }
287
290
  let type = basic_1.VoidType.get("SELECT_todo2");
288
- if (isSimple.test(field.code)) {
291
+ const fn = this.findFunctionColumn(field, false);
292
+ if (fn) {
293
+ const sourceInfos = this.buildDatabaseSourceInfos(from, dbSources);
294
+ type = this.inferFunctionType(fn, code => this.findFieldType(code, sourceInfos, input.scope)) || type;
295
+ }
296
+ else if (isSimple.test(field.code)) {
289
297
  for (const dbSource of dbSources) {
290
298
  if (dbSource === undefined) {
291
299
  continue;
@@ -317,7 +325,7 @@ class Select {
317
325
  return basic_1.IntegerType.get();
318
326
  }
319
327
  }
320
- static buildStructureType(fields, dbSources, scope) {
328
+ static buildStructureType(fields, dbSources, scope, from) {
321
329
  var _a, _b, _c, _d, _e, _f;
322
330
  if (fields.length === 1 && dbSources.length === 1) {
323
331
  // COUNT( * ) yields an integer regardless of the (possibly dynamic) table
@@ -353,10 +361,16 @@ class Select {
353
361
  return basic_1.IntegerType.get();
354
362
  }
355
363
  }
356
- else {
357
- // todo: aggregated/calculated values
358
- return basic_1.VoidType.get("SELECT_todo11");
364
+ const fn = this.findFunctionColumn(fields[0], false);
365
+ if (fn) {
366
+ const sourceInfos = this.buildDatabaseSourceInfos(from, dbSources);
367
+ const type = this.inferFunctionType(fn, code => this.findFieldType(code, sourceInfos, scope));
368
+ if (type) {
369
+ return type;
370
+ }
359
371
  }
372
+ // todo: aggregated/calculated values
373
+ return basic_1.VoidType.get("SELECT_todo11");
360
374
  }
361
375
  else {
362
376
  return basic_1.VoidType.get("SELECT_todo10");
@@ -377,15 +391,10 @@ class Select {
377
391
  if (!(dbType instanceof basic_1.StructureType)) {
378
392
  return basic_1.VoidType.get("SELECT_todo14");
379
393
  }
380
- const allFieldsSimple = fields.every(f => isSimple.test(f.code));
381
- if (allFieldsSimple === true) {
382
- const components = [];
383
- for (const field of fields) {
384
- const type = dbType.getComponentByName(field.code);
385
- if (type === undefined) {
386
- return basic_1.VoidType.get("SELECT_todo9");
387
- }
388
- components.push({ name: field.as || field.code, type });
394
+ if (this.allFieldsSupported(fields) === true) {
395
+ const components = this.buildSingleSourceComponents(fields, dbType, from, dbSources, scope);
396
+ if (components === undefined) {
397
+ return basic_1.VoidType.get("SELECT_todo9");
389
398
  }
390
399
  return new basic_1.StructureType(components);
391
400
  }
@@ -411,9 +420,15 @@ class Select {
411
420
  }
412
421
  return new basic_1.TableType(new basic_1.StructureType(components), tableOptions, undefined);
413
422
  }
423
+ const lookup = code => this.findFieldType(code, sourceInfos, scope);
414
424
  const components = [];
415
425
  for (const field of fields) {
416
- const type = this.findFieldType(field.code, sourceInfos, scope);
426
+ const fn = this.findFunctionColumn(field);
427
+ if (fn) {
428
+ components.push({ name: field.as, type: this.inferFunctionType(fn, lookup) || basic_1.VoidType.get("SELECT_function") });
429
+ continue;
430
+ }
431
+ const type = lookup(field.code);
417
432
  if (type === undefined) {
418
433
  return basic_1.VoidType.get("SELECT_todo3");
419
434
  }
@@ -436,20 +451,101 @@ class Select {
436
451
  if (fields.length === 1 && fields[0].code === "*") {
437
452
  return new basic_1.TableType(dbType, tableOptions, undefined);
438
453
  }
439
- const allFieldsSimple = fields.every(f => isSimple.test(f.code));
440
- if (allFieldsSimple === true) {
441
- const components = [];
442
- for (const field of fields) {
443
- const type = dbType.getComponentByName(field.code);
444
- if (type === undefined) {
445
- return basic_1.VoidType.get("SELECT_todo6");
446
- }
447
- components.push({ name: field.as || field.code, type });
454
+ if (this.allFieldsSupported(fields) === true) {
455
+ const components = this.buildSingleSourceComponents(fields, dbType, from, dbSources, scope);
456
+ if (components === undefined) {
457
+ return basic_1.VoidType.get("SELECT_todo6");
448
458
  }
449
459
  return new basic_1.TableType(new basic_1.StructureType(components), tableOptions, undefined);
450
460
  }
451
461
  return basic_1.VoidType.get("SELECT_todo7");
452
462
  }
463
+ /** plain field names, or SQL function columns with an alias */
464
+ static allFieldsSupported(fields) {
465
+ return fields.every(f => isSimple.test(f.code) || this.findFunctionColumn(f) !== undefined);
466
+ }
467
+ /** returns undefined if a plain field is not found in the database table */
468
+ static buildSingleSourceComponents(fields, dbType, from, dbSources, scope) {
469
+ const sourceInfos = this.buildDatabaseSourceInfos(from, dbSources);
470
+ const lookup = code => this.findFieldType(code, sourceInfos, scope);
471
+ const components = [];
472
+ for (const field of fields) {
473
+ const fn = this.findFunctionColumn(field);
474
+ if (fn) {
475
+ components.push({ name: field.as, type: this.inferFunctionType(fn, lookup) || basic_1.VoidType.get("SELECT_function") });
476
+ continue;
477
+ }
478
+ const type = dbType.getComponentByName(field.code);
479
+ if (type === undefined) {
480
+ return undefined;
481
+ }
482
+ components.push({ name: field.as || field.code, type });
483
+ }
484
+ return components;
485
+ }
486
+ /** returns the SQLFunction node if the field list entry is a single SQL function call */
487
+ static findFunctionColumn(field, requireAlias = true) {
488
+ if (requireAlias === true && field.as === "") {
489
+ return undefined;
490
+ }
491
+ else if (!(field.expression.get() instanceof Expressions.SQLField)) {
492
+ return undefined;
493
+ }
494
+ const children = field.expression.getChildren().filter(c => !(c.get() instanceof Expressions.SQLAsName) && c.concatTokens().toUpperCase() !== "AS");
495
+ if (children.length === 1
496
+ && children[0] instanceof nodes_1.ExpressionNode
497
+ && children[0].get() instanceof Expressions.SQLFunction) {
498
+ return children[0];
499
+ }
500
+ return undefined;
501
+ }
502
+ /** infers the result type of a SQL function, returns undefined if not possible */
503
+ static inferFunctionType(fn, lookup) {
504
+ const name = fn.getFirstToken().getStr().toUpperCase();
505
+ const inputs = fn.findDirectExpressions(Expressions.SQLFunctionInput);
506
+ if (inputs.length === 0) {
507
+ return undefined;
508
+ }
509
+ if (name === "SUBSTRING" && inputs.length === 3) {
510
+ const len = inputs[2].concatTokens();
511
+ if (/^\d+$/.test(len) === false || parseInt(len, 10) <= 0) {
512
+ return undefined;
513
+ }
514
+ const argType = this.inferFunctionInputType(inputs[0], lookup);
515
+ if (argType instanceof basic_1.StringType) {
516
+ return argType;
517
+ }
518
+ else if (argType instanceof basic_1.CharacterType
519
+ || argType instanceof basic_1.NumericType
520
+ || argType instanceof basic_1.DateType
521
+ || argType instanceof basic_1.TimeType) {
522
+ return new basic_1.CharacterType(parseInt(len, 10));
523
+ }
524
+ return undefined;
525
+ }
526
+ else if (sameTypeAsFirstArgument.has(name)) {
527
+ const argType = this.inferFunctionInputType(inputs[0], lookup);
528
+ if (argType instanceof basic_1.CharacterType || argType instanceof basic_1.StringType) {
529
+ return argType;
530
+ }
531
+ return undefined;
532
+ }
533
+ return undefined;
534
+ }
535
+ static inferFunctionInputType(input, lookup) {
536
+ const children = input.getChildren();
537
+ if (children.length !== 1 || !(children[0] instanceof nodes_1.ExpressionNode)) {
538
+ return undefined;
539
+ }
540
+ const child = children[0];
541
+ if (child.get() instanceof Expressions.SQLFieldName || child.get() instanceof Expressions.SQLAliasField) {
542
+ return lookup(child.concatTokens().toUpperCase());
543
+ }
544
+ else if (child.get() instanceof Expressions.SQLFunction) {
545
+ return this.inferFunctionType(child, lookup);
546
+ }
547
+ return undefined;
548
+ }
453
549
  static buildDatabaseSourceInfos(from, dbSources) {
454
550
  const fromSources = from.findAllExpressions(Expressions.SQLFromSource);
455
551
  return dbSources.map((source, index) => {
@@ -484,16 +580,20 @@ class Select {
484
580
  return undefined;
485
581
  }
486
582
  static findFields(node, input) {
487
- var _a, _b;
488
- const expr = node.findFirstExpression(Expressions.SQLFieldList);
583
+ var _a, _b, _c;
584
+ let expr = node.findFirstExpression(Expressions.SQLFieldList);
585
+ // a single field can be wrapped in a nested SQLFieldList
586
+ while (expr && expr.getChildren().length === 1 && ((_a = expr.getFirstChild()) === null || _a === void 0 ? void 0 : _a.get()) instanceof Expressions.SQLFieldList) {
587
+ expr = expr.getFirstChild();
588
+ }
489
589
  const ret = [];
490
- if (((_a = expr === null || expr === void 0 ? void 0 : expr.getFirstChild()) === null || _a === void 0 ? void 0 : _a.get()) instanceof Expressions.Dynamic) {
590
+ if (((_b = expr === null || expr === void 0 ? void 0 : expr.getFirstChild()) === null || _b === void 0 ? void 0 : _b.get()) instanceof Expressions.Dynamic) {
491
591
  dynamic_1.Dynamic.runSyntax(expr.getFirstChild(), input);
492
592
  }
493
593
  // eslint-disable-next-line max-len
494
594
  for (const field of (expr === null || expr === void 0 ? void 0 : expr.findDirectExpressionsMulti([Expressions.SQLField, Expressions.SQLFieldName, Expressions.SQLAggregation])) || []) {
495
595
  let code = field.concatTokens().toUpperCase();
496
- const as = ((_b = field.findDirectExpression(Expressions.SQLAsName)) === null || _b === void 0 ? void 0 : _b.concatTokens()) || "";
596
+ const as = ((_c = field.findDirectExpression(Expressions.SQLAsName)) === null || _c === void 0 ? void 0 : _c.concatTokens()) || "";
497
597
  if (as !== "") {
498
598
  code = code.replace(" AS " + as.toUpperCase(), "");
499
599
  }
@@ -86,9 +86,19 @@ class View extends _abstract_object_1.AbstractObject {
86
86
  continue;
87
87
  }
88
88
  else if (this.parsedData.header.VIEWCLASS === ViewClass.ExternalView) {
89
+ // for external views TABNAME is the view itself, the types are given directly in the xml
90
+ let type = Types.VoidType.get("ExternalView");
91
+ if (field.EXVDATATYPE !== undefined) {
92
+ type = ddic.textToType({
93
+ text: field.EXVDATATYPE,
94
+ length: field.EXVFLENGTH,
95
+ decimals: field.EXVDECIMALS,
96
+ infoText: this.getName() + "-" + field.VIEWFIELD,
97
+ });
98
+ }
89
99
  components.push({
90
100
  name: field.VIEWFIELD,
91
- type: Types.VoidType.get("ExternalView")
101
+ type: type
92
102
  });
93
103
  continue;
94
104
  }
@@ -176,6 +186,9 @@ class View extends _abstract_object_1.AbstractObject {
176
186
  TABNAME: field.TABNAME,
177
187
  FIELDNAME: field.FIELDNAME,
178
188
  KEYFLAG: field.KEYFLAG,
189
+ EXVDATATYPE: typeof field.EXVDATATYPE === "string" && field.EXVDATATYPE !== "" ? field.EXVDATATYPE : undefined,
190
+ EXVFLENGTH: typeof field.EXVFLENGTH === "string" ? field.EXVFLENGTH : undefined,
191
+ EXVDECIMALS: typeof field.EXVDECIMALS === "string" ? field.EXVDECIMALS : undefined,
179
192
  });
180
193
  }
181
194
  const join = (_j = (_h = (_g = parsed === null || parsed === void 0 ? void 0 : parsed.abapGit) === null || _g === void 0 ? void 0 : _g["asx:abap"]) === null || _h === void 0 ? void 0 : _h["asx:values"]) === null || _j === void 0 ? void 0 : _j.DD28J_TABLE;
@@ -75,7 +75,7 @@ class Registry {
75
75
  }
76
76
  static abaplintVersion() {
77
77
  // magic, see build script "version.js"
78
- return "2.120.98";
78
+ return "2.120.100";
79
79
  }
80
80
  getDDICReferences() {
81
81
  return this.ddicReferences;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abaplint/core",
3
- "version": "2.120.98",
3
+ "version": "2.120.100",
4
4
  "description": "abaplint - Core API",
5
5
  "main": "build/src/index.js",
6
6
  "typings": "build/abaplint.d.ts",