All files / src/objects table_type.ts

94.29% Statements 215/228
72.09% Branches 62/86
90% Functions 9/10
94.29% Lines 215/228

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 216 217 218 219 220 221 222 223 224 225 226 227 228 2291x 1x 1x 1x 1x 1x 1x 1x 1x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 783x 783x 78x 78x 8x 8x 8x 8x 8x 78x 78x 119x 119x 119x 78x 78x       78x 78x 119x 119x 119x 78x 78x 124x 124x 124x 124x 124x 124x 124x 25x 124x 2x 2x 124x 124x 8x 6x 6x 8x 124x 2x 2x 124x 124x 78x 78x 124x 124x 108x 108x 124x 124x 124x 124x 124x 124x 124x 124x 124x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x   2x 2x 2x 2x 2x 2x 2x 2x 124x 124x 124x 78x 78x 124x 124x 124x 124x 124x 124x 124x 124x 124x   124x 68x 68x 68x 57x 57x 124x 6x 6x 6x 6x 6x 56x           50x 2x 50x 2x 48x 2x 2x 2x 2x 2x 46x 36x   36x 36x 36x 36x 36x 36x 36x 36x 36x 44x 8x 8x 124x 124x 124x 124x 78x 78x 78x 78x 243x 165x 165x 78x 78x 78x 78x 78x 78x 78x 243x 5x 5x 73x 243x 243x 2x 2x 71x 71x 243x 1x 1x 243x 243x 243x 243x 243x 243x 243x 243x 243x 243x 243x 4x 4x 4x 4x 4x 243x 1x 1x 1x 1x 1x 1x 1x 70x 78x 78x  
import {AbstractObject} from "./_abstract_object";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import * as Types from "../abap/types/basic";
import {IRegistry} from "../_iregistry";
import {DDIC} from "../ddic";
import {IObjectAndToken} from "../_iddic_references";
import {DataReference, GenericObjectReferenceType, ITableOptions, TableAccessType} from "../abap/types/basic";
import {xmlToArray} from "../xml_utils";
 
export class TableType extends AbstractObject {
  private parsedXML: {
    rowtype?: string,
    rowkind?: string,
    datatype?: string,
    leng?: string,
    decimals?: string,
    accessmode?: string,
    keykind?: string,
    ddtext?: string,
    keydef?: string,
    dd42v: {keyname: string, keyfield: string}[];
    dd43v: {keyname: string, accessmode: string, kind: string, unique: boolean}[];
  } | undefined = undefined;
 
  public getType(): string {
    return "TTYP";
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 30,
      allowNamespace: true,
    };
  }
 
  public getDescription(): string | undefined {
    this.parseXML();
    return this.parsedXML?.ddtext;
  }
 
  public getRowType(): string | undefined {
    this.parseXML();
    return this.parsedXML?.rowtype;
  }
 
  public setDirty(): void {
    this.parsedXML = undefined;
    super.setDirty();
  }
 
  private buildPrimaryKey() {
    const primaryKey: Types.ITableKey = {
      isUnique: this.parsedXML?.keykind === "U",
      type: TableAccessType.standard,
      keyFields: [],
      name: "primary_key",
    };
    if (this.parsedXML?.accessmode === "S") {
      primaryKey.type = TableAccessType.sorted;
    } else if (this.parsedXML?.accessmode === "H") {
      primaryKey.type = TableAccessType.hashed;
    }
 
    for (const f of this.parsedXML?.dd42v || []) {
      if (f.keyname === "") {
        primaryKey.keyFields.push(f.keyfield);
      }
    }
    if (this.parsedXML?.keydef === "T" && primaryKey.keyFields.length === 0) {
      primaryKey.keyFields.push("table_line");
    }
    return primaryKey;
  }
 
  private buildTableOptions(): ITableOptions {
    let keyType = Types.TableKeyType.user;
    if (this.parsedXML?.keydef === "D") {
      keyType = Types.TableKeyType.default;
    }
 
    const tableOptions: ITableOptions = {
      withHeader: false,
      keyType: keyType,
      primaryKey: this.buildPrimaryKey(),
      secondary: [],
    };
 
    for (const k of this.parsedXML?.dd43v || []) {
      const fields: string[] = [];
      for (const f of this.parsedXML?.dd42v || []) {
        if (f.keyname === k.keyname) {
          fields.push(f.keyfield);
        }
      }
      let accessType: TableAccessType = TableAccessType.standard;
      switch (k.accessmode) {
        case "S":
          accessType = TableAccessType.sorted;
          break;
        case "H":
          accessType = TableAccessType.hashed;
          break;
        default:
          break;
      }
      tableOptions.secondary?.push({
        name: k.keyname,
        type: accessType,
        keyFields: fields,
        isUnique: k.unique,
      });
    }
 
    return tableOptions;
  }
 
  public parseType(reg: IRegistry): AbstractType {
    this.parseXML();
 
    const ddic = new DDIC(reg);
 
    const references: IObjectAndToken[] = [];
    let type: AbstractType;
    const tableOptions = this.buildTableOptions();
 
    if (this.parsedXML === undefined) {
      type = new Types.UnknownType("Table Type, parser error", this.getName());
    } else if (this.parsedXML.rowkind === "S") {
      const lookup = ddic.lookupTableOrView(this.parsedXML.rowtype);
      type = new Types.TableType(lookup.type, tableOptions, this.getName(), this.getDescription());
      if (lookup.object) {
        references.push({object: lookup.object});
      }
    } else if (this.parsedXML.rowkind === "E") {
      const lookup = ddic.lookupDataElement(this.parsedXML.rowtype);
      type = new Types.TableType(lookup.type, tableOptions, this.getName(), this.getDescription());
      if (lookup.object) {
        references.push({object: lookup.object});
      }
    } else if (this.parsedXML.rowkind === "L") {
      const lookup = ddic.lookupTableType(this.parsedXML.rowtype);
      type = new Types.TableType(lookup.type, tableOptions, this.getName(), this.getDescription());
      if (lookup.object) {
        references.push({object: lookup.object});
      }
    } else if (this.parsedXML.rowkind === "R" && this.parsedXML.rowtype === "OBJECT") {
      type = new Types.TableType(new GenericObjectReferenceType(), tableOptions, this.getName(), this.getDescription());
    } else if (this.parsedXML.rowkind === "R" && this.parsedXML.rowtype === "DATA") {
      type = new Types.TableType(new DataReference(new Types.DataType()), tableOptions, this.getName(), this.getDescription());
    } else if (this.parsedXML.rowkind === "R" && this.parsedXML.rowtype !== undefined) {
      const lookup = ddic.lookupObject(this.parsedXML.rowtype);
      type = new Types.TableType(lookup.type, tableOptions, this.getName(), this.getDescription());
      if (lookup.object) {
        references.push({object: lookup.object});
      }
    } else if (this.parsedXML.rowkind === "") {
      if (this.parsedXML.datatype === undefined) {
        type = new Types.UnknownType("Table Type, empty DATATYPE" + this.getName(), this.getName());
      } else {
        const row = ddic.textToType({
          text: this.parsedXML.datatype,
          length: this.parsedXML.leng,
          decimals: this.parsedXML.decimals,
          infoText: this.getName(),
        });
        type = new Types.TableType(row, tableOptions, this.getName(), this.getDescription());
      }
    } else {
      type = new Types.UnknownType("Table Type, unknown kind \"" + this.parsedXML.rowkind + "\"" + this.getName(), this.getName());
    }
 
    reg.getDDICReferences().setUsing(this, references);
    return type;
  }
 
////////////////////
 
  private parseXML() {
    if (this.parsedXML !== undefined) {
      return;
    }
 
    this.parsedXML = {
      dd42v: [],
      dd43v: [],
    };
 
    const parsed = super.parseRaw2();
    if (parsed === undefined || parsed.abapGit === undefined) {
      return;
    }
 
    const values = parsed?.abapGit?.["asx:abap"]?.["asx:values"];
    if (values === undefined) {
      return;
    }
 
    const dd40v = values.DD40V;
    if (dd40v === undefined) {
      return;
    }
    this.parsedXML.rowtype = dd40v.ROWTYPE ? dd40v.ROWTYPE : "";
    this.parsedXML.rowkind = dd40v.ROWKIND ? dd40v.ROWKIND : "";
    this.parsedXML.datatype = dd40v.DATATYPE;
    this.parsedXML.leng = dd40v.LENG;
    this.parsedXML.decimals = dd40v.DECIMALS;
    this.parsedXML.accessmode = dd40v.ACCESSMODE;
    this.parsedXML.keykind = dd40v.KEYKIND;
    this.parsedXML.ddtext = dd40v.DDTEXT;
    this.parsedXML.keydef = dd40v.KEYDEF;
 
    for (const x of xmlToArray(values.DD42V?.DD42V)) {
      this.parsedXML.dd42v.push({
        keyname: x.SECKEYNAME || "",
        keyfield: x.KEYFIELD || "",
      });
    }
    for (const x of xmlToArray(values.DD43V?.DD43V)) {
      this.parsedXML.dd43v.push({
        keyname: x.SECKEYNAME || "",
        accessmode: x.ACCESSMODE || "",
        kind: x.KIND || "",
        unique: x.SECKEYUNIQUE === "X",
      });
    }
  }
 
}