@abaplint/core 2.120.99 → 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.
|
@@ -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
|
-
|
|
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
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
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
|
|
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
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
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
|
-
|
|
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 (((
|
|
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 = ((
|
|
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
|
}
|
package/build/src/registry.js
CHANGED