All files / src/abap/5_syntax/statements update_database.ts

94.02% Statements 63/67
69.23% Branches 9/13
100% Functions 1/1
94.02% Lines 63/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 12x 12x 12x 12x 5x 7x 7x 7x 7x     7x 11x 11x 7x 7x     7x 7x 7x 7x 5x 5x 7x 7x 1x  
import * as Expressions from "../../2_statements/expressions";
import {ExpressionNode, StatementNode} from "../../nodes";
import {Source} from "../expressions/source";
import {ScopeType} from "../_scope_type";
import {StructureType} from "../../types/basic";
import {TypedIdentifier} from "../../types/_typed_identifier";
import {Identifier} from "../../1_lexer/tokens/identifier";
import {DatabaseTable} from "../expressions/database_table";
import {Dynamic} from "../expressions/dynamic";
import {StatementSyntax} from "../_statement_syntax";
import {SyntaxInput} from "../_syntax_input";
import {checkDatabaseFields} from "../expressions/_check_database_fields";
import {AbstractType} from "../../types/basic/_abstract_type";
import {DatabaseTableSource} from "../expressions/database_table";
import {checkDatabaseWorkArea} from "../expressions/_check_database_work_area";
 
export class UpdateDatabase implements StatementSyntax {
  public runSyntax(node: StatementNode, input: SyntaxInput): void {
 
    let dbSource: DatabaseTableSource = undefined;
    const dbtab = node.findFirstExpression(Expressions.DatabaseTable);
    if (dbtab !== undefined) {
      dbSource = DatabaseTable.runSyntax(dbtab, input);
      const fields = node.findAllExpressions(Expressions.SQLFieldAndValue)
        .flatMap(fieldAndValue => fieldAndValue.findDirectExpressions(Expressions.SQLFieldName))
        .map(field => field.concatTokens().toUpperCase());
      fields.push(...node.findAllExpressions(Expressions.SQLCompare)
        .map(compare => compare.findDirectExpression(Expressions.SQLFieldName)?.concatTokens().toUpperCase())
        .filter(field => field !== undefined));
      checkDatabaseFields(fields, [dbSource], input, node.getFirstToken());
    }
 
    const tableName = node.findDirectExpression(Expressions.DatabaseTable);
    const tokenName = tableName?.getFirstToken();
    if (tableName && tokenName) {
      // todo, this also finds structures, it should only find transparent tables
      const found = input.scope.getDDIC().lookupTable(tokenName.getStr());
      if (found instanceof StructureType) {
        input.scope.push(ScopeType.OpenSQL, "UPDATE", tokenName.getStart(), input.filename);
        for (const field of found.getComponents()) {
          const fieldToken = new Identifier(node.getFirstToken().getStart(), field.name);
          const id = new TypedIdentifier(fieldToken, input.filename, field.type);
          input.scope.addIdentifier(id);
        }
      }
    }
 
    const sourceTypes = new Map<ExpressionNode, AbstractType | undefined>();
    for (const s of node.findAllExpressions(Expressions.Source)) {
      sourceTypes.set(s, Source.runSyntax(s, input));
    }
    for (const s of node.findAllExpressions(Expressions.SimpleSource3)) {
      sourceTypes.set(s, Source.runSyntax(s, input));
    }
 
    for (const d of node.findAllExpressions(Expressions.Dynamic)) {
      Dynamic.runSyntax(d, input);
    }
 
    checkDatabaseWorkArea(node, dbSource, sourceTypes, input);
 
    if (input.scope.getType() === ScopeType.OpenSQL) {
      input.scope.pop(node.getLastToken().getEnd());
    }
 
  }
}