All files / src/objects table.ts

77.89% Statements 74/95
74.34% Branches 84/113
81.82% Functions 9/11
77.89% Lines 74/95

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 2161x 1x 1x   1x 1x     1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x                                 99x                 4x             35x 35x                                         28x 23x   28x       28x 28x 28x 50x 50x 7x     43x 3x 3x     3x 1x 1x       2x   1x 1x   1x   1x           40x       40x 1x     39x 1x     1x     38x 1x     37x 37x 37x 37x 37x                   27x       27x       1x       1x       6x 1x   6x     6x           24x 24x       24x     24x 7x   17x       24x     24x 24x 41x                              
import * as Types from "../abap/types/basic";
import {AbstractObject} from "./_abstract_object";
import {xmlToArray} from "../xml_utils";
import {IRegistry} from "../_iregistry";
import {DDIC} from "../ddic";
import {TypedIdentifier} from "../abap/types/_typed_identifier";
import {AbstractType} from "../abap/types/basic/_abstract_type";
 
export enum EnhancementCategory {
  NotClassified = "0",
  CannotBeEhanced = "1",
  Character = "2",
  CharacterOrNumeric = "3",
  Deep = "4",
}
 
export enum TableCategory {
  Transparent = "TRANSP",
  Structure = "INTTAB",
  Cluster = "CLUSTER",
  Pooled = "POOL",
  View = "VIEW",
  Append = "APPEND",
}
 
export class Table extends AbstractObject {
  private parsedData: {
    tableCategory?: TableCategory | undefined,
    enhancementCategory?: EnhancementCategory,
    fields: {
      FIELDNAME: string,
      ROLLNAME?: string,
      COMPTYPE?: string,
      PRECFIELD?: string,
      LENG?: string,
      INTLEN?: string,
      DATATYPE?: string,
      DECIMALS?: string,
      KEYFLAG?: string
    }[]} | undefined;
 
  public getType(): string {
    return "TABL";
  }
 
  public getDescription(): string | undefined {
    // todo
    return undefined;
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 30,
      allowNamespace: true,
    };
  }
 
  public setDirty(): void {
    this.parsedData = undefined;
    super.setDirty();
  }
 
  public listKeys(): string[] {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    if (this.parsedData === undefined) {
      return [];
    }
 
    const ret: string[] = [];
    for (const p of this.parsedData.fields) {
      if (p.KEYFLAG === "X") {
        ret.push(p.FIELDNAME);
      }
    }
    return ret;
  }
 
  public parseType(reg: IRegistry): AbstractType {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    Iif (this.parsedData === undefined) {
      return new Types.UnknownType("Table, parser error");
    }
 
    const components: Types.IStructureComponent[] = [];
    const ddic = new DDIC(reg);
    for (const field of this.parsedData.fields) {
      const comptype = field.COMPTYPE ? field.COMPTYPE : "";
      if (comptype === "E") { // data element
        components.push({
          name: field.FIELDNAME,
          type: ddic.lookupDataElement(field.ROLLNAME)});
      } else if (field.FIELDNAME === ".INCLUDE" || field.FIELDNAME === ".INCLU--AP") { // incude or append structure
        let found = ddic.lookupTableOrView(field.PRECFIELD);
        Iif (found instanceof TypedIdentifier) {
          found = found.getType();
        }
        if (found instanceof Types.StructureType) {
          for (const c of found.getComponents()) {
            components.push({
              name: c.name,
              type: c.type});
          }
        } else if ((field.PRECFIELD?.startsWith("CI_") || field.PRECFIELD?.startsWith("SI_"))
            && found instanceof Types.UnknownType) {
          continue;
        } else Iif (found instanceof Types.UnknownType) {
          return found;
        } else Eif (found instanceof Types.VoidType) {
          // set the full structure to void
          return found;
        } else {
          components.push({
            name: field.FIELDNAME,
            type: found});
        }
      } else Iif (comptype === "S" && field.FIELDNAME.startsWith(".INCLU-")) {
        components.push({
          name: field.FIELDNAME,
          type: new Types.UnknownType("Table " + this.getName() + ", todo, group named INCLUDE")});
      } else if (comptype === "S") {
        components.push({
          name: field.FIELDNAME,
          type: ddic.lookupTableOrView(field.ROLLNAME)});
      } else if (comptype === "R") {
        Iif (field.ROLLNAME === undefined) {
          throw new Error("Expected ROLLNAME");
        }
        components.push({
          name: field.FIELDNAME,
          type: ddic.lookupObject(field.ROLLNAME)});
      } else if (comptype === "L") {
        components.push({
          name: field.FIELDNAME,
          type: ddic.lookupTableType(field.ROLLNAME)});
      } else Eif (comptype === "") { // built in
        const datatype = field.DATATYPE;
        const length = field.LENG ? field.LENG : field.INTLEN;
        const decimals = field.DECIMALS ? field.DECIMALS : undefined;
        components.push({
          name: field.FIELDNAME,
          type: ddic.textToType(datatype, length, decimals, this.getName())});
      } else {
        components.push({
          name: field.FIELDNAME,
          type: new Types.UnknownType("Table " + this.getName() + ", unknown component type " + comptype)});
      }
    }
 
    Iif (components.length === 0) {
      throw new Error("Table/Structure " + this.getName() + " does not contain any components");
    }
 
    return new Types.StructureType(components, this.getName());
  }
 
  public getTableCategory(): TableCategory | undefined {
    Iif (this.parsedData === undefined) {
      this.parseXML();
    }
 
    return this.parsedData?.tableCategory;
  }
 
  public getEnhancementCategory(): EnhancementCategory {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    Iif (this.parsedData?.enhancementCategory === undefined) {
      return EnhancementCategory.NotClassified;
    }
    return this.parsedData.enhancementCategory;
  }
 
///////////////
 
  private parseXML() {
    const parsed = super.parseRaw2();
    Iif (parsed === undefined) {
      return;
    }
 
    this.parsedData = {fields: []};
 
// enhancement category
    if (parsed.abapGit["asx:abap"]["asx:values"]?.DD02V?.EXCLASS === undefined) {
      this.parsedData.enhancementCategory = EnhancementCategory.NotClassified;
    } else {
      this.parsedData.enhancementCategory = parsed.abapGit["asx:abap"]["asx:values"]?.DD02V?.EXCLASS;
    }
 
// table category
    this.parsedData.tableCategory = parsed.abapGit["asx:abap"]["asx:values"]?.DD02V?.TABCLASS;
 
// fields
    const fields = parsed.abapGit["asx:abap"]["asx:values"]?.DD03P_TABLE;
    for (const field of xmlToArray(fields?.DD03P)) {
      this.parsedData.fields.push({
        FIELDNAME: field.FIELDNAME,
        ROLLNAME: field.ROLLNAME,
        COMPTYPE: field.COMPTYPE,
        PRECFIELD: field.PRECFIELD,
        LENG: field.LENG,
        INTLEN: field.INTLEN,
        DATATYPE: field.DATATYPE,
        DECIMALS: field.DECIMALS,
        KEYFLAG: field.KEYFLAG,
      });
    }
  }
 
}