All files / src/objects table_type.ts

93.64% Statements 221/236
72.52% Branches 66/91
90% Functions 9/10
93.64% Lines 221/236

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 229 230 231 232 233 234 235 236 2371x 1x 1x 1x 1x 1x 1x 1x 1x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 1118x 1118x 110x 110x 10x 10x 10x 10x 10x 110x 110x 200x 200x 200x 110x 110x       110x 110x 160x 160x 160x 110x 110x 205x 205x 205x 205x 205x 205x 205x 25x 205x 2x 2x 205x 205x 8x 6x 6x 8x 205x 2x 2x 205x 205x 110x 110x 205x 205x 189x 189x 205x 205x 205x 205x 205x 205x 205x 205x 205x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x   2x 2x 2x 2x 2x 2x 2x 2x 205x 205x 205x 110x 110x 205x 205x 205x 205x 205x 205x 205x 205x 205x   205x 72x 72x 72x 61x 61x 205x 6x 6x 6x 6x 6x 133x           127x 2x 127x 2x 2x 125x 10x 10x 10x 10x 10x 123x 4x 4x 4x 4x     113x 101x   101x 101x 101x 101x 101x 101x 101x 101x 101x 109x 8x 8x 205x 205x 205x 205x 110x 110x 110x 110x 405x 293x 293x 112x 112x 112x 112x 112x 112x 112x 405x 5x 5x 107x 405x 405x 2x 2x 105x 105x 405x 1x 1x 405x 405x 405x 405x 405x 405x 405x 405x 405x 405x 405x 4x 4x 4x 4x 4x 405x 1x 1x 1x 1x 1x 1x 1x 104x 110x 110x  
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(), this.getName());
      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(), this.getName());
      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(), this.getName());
      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(), this.getName());
    } else if (this.parsedXML.rowkind === "R" && this.parsedXML.rowtype === "DATA") {
      const row = new DataReference(new Types.DataType());
      type = new Types.TableType(row, tableOptions, this.getName(), this.getDescription(), this.getName());
    } 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(), this.getName());
      if (lookup.object) {
        references.push({object: lookup.object});
      }
    } else if (this.parsedXML.rowkind === "" && this.parsedXML.rowtype !== undefined && this.parsedXML.rowtype !== "") {
      // ROWTYPE given without ROWKIND, it must refer to a DDIC object, predefined ABAP types like "I" are not allowed
      const lookup = ddic.lookup(this.parsedXML.rowtype);
      type = new Types.TableType(lookup.type, tableOptions, this.getName(), this.getDescription(), this.getName());
      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(), this.getName());
      }
    } 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",
      });
    }
  }
 
}