All files / src/objects table_type.ts

93.86% Statements 199/212
70.83% Branches 51/72
88.88% Functions 8/9
93.86% Lines 199/212

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 2131x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 333x 333x 31x 31x 4x 4x 4x 4x 4x 31x 31x       31x 31x 33x 33x 33x 31x 31x 63x 63x 63x 63x 63x 63x 63x 6x 63x 2x 2x 63x 63x 8x 6x 6x 8x 63x 2x 2x 63x 63x 31x 31x 63x 63x 51x 51x 63x 63x 63x 63x 63x 63x 63x 63x 63x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x   2x 2x 2x 2x 2x 2x 2x 2x 63x 63x 63x 31x 31x 63x 63x 63x 63x 63x 63x 63x 63x 63x   63x 29x 29x 29x 18x 18x 63x 6x 6x 6x 6x 6x 34x           28x 2x 28x 2x 26x 2x 2x 2x 2x 2x 24x 18x   18x 18x 18x 18x 22x 4x 4x 63x 63x 63x 63x 31x 31x 31x 31x 63x 32x 32x 31x 31x 31x 31x 31x 31x 31x 63x 4x 4x 27x 27x 27x 27x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 4x 4x 4x 4x 4x 63x 1x 1x 1x 1x 1x 1x 1x 27x 31x 31x  
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 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());
      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());
      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());
      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());
    } else if (this.parsedXML.rowkind === "R" && this.parsedXML.rowtype === "DATA") {
      type = new Types.TableType(new DataReference(new Types.DataType()), tableOptions, 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());
      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(this.parsedXML.datatype, this.parsedXML.leng, this.parsedXML.decimals, this.getName());
        type = new Types.TableType(row, tableOptions, 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"];
 
    const dd40v = values.DD40V;
    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",
      });
    }
  }
 
}