All files / src/objects data_definition.ts

91.57% Statements 163/178
64.7% Branches 33/51
82.35% Functions 14/17
91.57% Lines 163/178

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 1791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 22x 22x 13x 13x           13x 13x 1x 1x 1x 13x 13x 1x 1x 1x 13x 13x       13x 13x 4x 4x 4x 13x 13x 1x 1x 13x 13x       13x 13x 13x 13x 13x 13x 13x 13x 30x 30x 13x 13x 2x 2x 13x 13x 18x 6x 6x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 18x 10x 10x 10x 18x 2x 2x 12x 12x 12x 12x 13x 13x 3x 3x 13x 13x 13x 13x 12x 12x 9x 9x 12x 13x 13x 10x 21x 21x 15x 15x 15x 21x     21x 21x 21x 1x 1x 20x 20x 21x 4x 4x 20x 20x 20x 20x 20x 20x 20x 10x 13x 13x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 10x 10x 1x 1x     1x 1x 1x 1x 1x 1x 1x 10x 13x  
import {ExpressionNode} from "../abap/nodes";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {CDSDetermineTypes} from "../cds/cds_determine_types";
import {CDSParser} from "../cds/cds_parser";
import {CDSAnnotation, CDSAs, CDSAssociation, CDSElement, CDSName, CDSRelation, CDSSource} from "../cds/expressions";
import {IRegistry} from "../_iregistry";
import {AbstractObject} from "./_abstract_object";
import {IParseResult} from "./_iobject";
 
export type ParsedDataDefinition = {
  sqlViewName: string | undefined;
  definitionName: string | undefined;
  fields: {key: boolean, name: string, annotations: string[]}[];
  sources: {name: string, as: string | undefined}[];
  associations: {name: string, as: string | undefined}[],
  relations: {name: string, as: string | undefined}[];
  tree: ExpressionNode | undefined;
};
 
export class DataDefinition extends AbstractObject {
  private parserError: boolean | undefined = undefined;
  private parsedData: ParsedDataDefinition | undefined = undefined;
 
  public getType(): string {
    return "DDLS";
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 40,
      allowNamespace: true,
    };
  }
 
  public getSQLViewName(): string | undefined {
    this.parse();
    return this.parsedData?.sqlViewName;
  }
 
  public getDefinitionName(): string | undefined {
    this.parse();
    return this.parsedData?.definitionName;
  }
 
  public getDescription(): string | undefined {
    // todo
    return undefined;
  }
 
  public parseType(reg: IRegistry): AbstractType {
    this.parse();
    return new CDSDetermineTypes().parseType(reg, this.parsedData!);
  }
 
  public getParsedData() {
    return this.parsedData;
  }
 
  public listSources() {
    this.parse();
    return this.parsedData?.sources;
  }
 
  public setDirty(): void {
    this.parsedData = undefined;
    this.parserError = undefined;
    super.setDirty();
  }
 
  public findSourceFile() {
    return this.getFiles().find(f => f.getFilename().endsWith(".asddls") || f.getFilename().endsWith(".acds"));
  }
 
  public hasParserError() {
    return this.parserError;
  }
 
  public parse(): IParseResult {
    if (this.isDirty() === false) {
      return {updated: false, runtime: 0};
    }
 
    const start = Date.now();
 
    this.parsedData = {
      sqlViewName: undefined,
      definitionName: undefined,
      fields: [],
      sources: [],
      relations: [],
      associations: [],
      tree: undefined,
    };
 
    this.findSQLViewName();
 
    this.parsedData.tree = new CDSParser().parse(this.findSourceFile());
    if (this.parsedData.tree) {
      this.parsedData.definitionName = this.parsedData.tree?.findFirstExpression(CDSName)?.getFirstToken().getStr();
      this.findSourcesAndRelations(this.parsedData.tree);
      this.findFieldNames(this.parsedData.tree);
    } else {
      this.parserError = true;
    }
 
    this.dirty = false;
    return {updated: true, runtime: Date.now() - start};
  }
 
  public getTree() {
    return this.parsedData?.tree;
  }
 
//////////
 
  private findSQLViewName(): void {
    const match = this.findSourceFile()?.getRaw().match(/@AbapCatalog\.sqlViewName: '(\w+)'/);
    if (match) {
      this.parsedData!.sqlViewName = match[1].toUpperCase();
    }
  }
 
  private findFieldNames(tree: ExpressionNode) {
    for (const e of tree.findAllExpressions(CDSElement)) {
      let found = e.findDirectExpression(CDSAs)?.findDirectExpression(CDSName);
      if (found === undefined) {
        const list = e.findDirectExpressions(CDSName);
        found = list[list.length - 1];
      }
      if (found === undefined) {
        continue;
      }
      const name = found?.concatTokens();
      if (this.parsedData?.associations.some(a =>
        a.name.toUpperCase() === name.toUpperCase() || a.as?.toUpperCase() === name.toUpperCase())) {
        continue;
      }
 
      const annotations: string[] = [];
      for (const a of e.findDirectExpressions(CDSAnnotation)) {
        annotations.push(a.concatTokens());
      }
 
      this.parsedData!.fields.push({
        name: name,
        annotations: annotations,
        key: e.findDirectTokenByText("KEY") !== undefined,
      });
    }
  }
 
  private findSourcesAndRelations(tree: ExpressionNode) {
    for (const e of tree.findAllExpressions(CDSSource)) {
      const name = e.getFirstToken().getStr();
      const as = e.findDirectExpression(CDSAs)?.findDirectExpression(CDSName)?.getFirstToken().getStr();
      this.parsedData!.sources.push({name, as});
    }
 
    for (const e of tree.findAllExpressions(CDSRelation)) {
      const name = e.getFirstToken().getStr();
      const as = e.findDirectExpression(CDSAs)?.findDirectExpression(CDSName)?.getFirstToken().getStr();
      this.parsedData!.relations.push({name, as});
    }
 
    for (const e of tree.findAllExpressions(CDSAssociation)) {
      const j = e.findDirectExpression(CDSRelation);
      if (j === undefined) {
        continue;
      }
      const name = j.getFirstToken().getStr();
      const as = j.findDirectExpression(CDSAs)?.findDirectExpression(CDSName)?.getFirstToken().getStr();
      this.parsedData!.associations.push({
        name: name || "ERROR",
        as: as,
      });
    }
  }
}