All files / src/objects table.ts

86.41% Statements 318/368
74.46% Branches 105/141
100% Functions 11/11
86.41% Lines 318/368

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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 3691x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 1175x 1175x 120x 120x 104x     104x 104x 104x 120x 120x 22x     22x 22x 22x 120x 120x 31x 31x 31x 31x 15x 15x 31x 31x 31x 31x 31x 31x 31x 120x 120x 151x 151x 151x 151x 120x 120x 191x     191x     191x 191x 191x 515x 4x 4x 4x 4x 4x 4x 515x 335x 335x 515x 191x 191x 120x 120x 333x 121x 121x     121x 333x 333x 333x 2x 2x 331x 331x 333x     331x 333x 209x 209x 122x 122x 122x 122x 333x 262x 262x 100x 100x 100x 6x 6x 262x 162x 17x     17x 17x 17x 12x 12x 17x     17x 12x 8x 8x 12x 12x 12x 6x 7x 7x 6x 6x 6x 9x 9x 6x 17x 5x 1x 5x   4x 4x 4x 4x     162x 1x 1x 1x 1x 1x 145x 7x     7x 1x 1x 1x 7x 1x 1x 1x 6x           5x           5x           5x 5x 5x 5x 4x 4x 5x 144x 1x 1x 1x 1x 1x 137x 136x 136x     136x 136x 136x 136x 136x 136x 136x 136x 136x 136x 136x         257x 262x 3x 3x 1x 1x 3x 262x 118x 333x 15x 15x 103x 103x 103x 103x 103x 120x 120x 363x     363x 363x 363x 120x 120x 22x     22x     22x 22x 120x 120x 120x 120x 121x 121x     121x 121x 121x 121x 12x 12x 109x 109x 121x 22x 121x 87x 87x 109x 109x 121x 121x 121x 121x 121x 121x 121x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 109x 109x 121x 121x 121x 3x 3x 3x 3x 8x 5x 5x 5x 8x 3x 3x 3x 3x 3x 109x 120x 120x  
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";
import {AnyType, DataReference, GenericObjectReferenceType} from "../abap/types/basic";
import {IObjectAndToken} from "../_iddic_references";
import {Version} from "../version";
import {IAllowedNaming} from "./_iobject";
 
export enum EnhancementCategory {
  NotClassified = "0",
  CannotBeEnhanced = "1",
  Character = "2",
  CharacterOrNumeric = "3",
  Deep = "4",
}
 
export enum TableCategory {
  Transparent = "TRANSP",
  Structure = "INTTAB",
  Cluster = "CLUSTER",
  Pooled = "POOL",
  View = "VIEW",
  Append = "APPEND",
}
 
export type SecondaryIndex = {
  name: string,
  fields: string[],
};
 
export class Table extends AbstractObject {
  private parsedData: {
    tableCategory?: TableCategory | undefined,
    dataClass?: string,
    enhancementCategory?: EnhancementCategory,
    description?: string,
    secondaryIndexes?: SecondaryIndex[],
    fields: {
      FIELDNAME: string,
      ROLLNAME?: string,
      COMPTYPE?: string,
      PRECFIELD?: string,
      LENG?: string,
      INTLEN?: string,
      DATATYPE?: string,
      DECIMALS?: string,
      KEYFLAG?: string,
      GROUPNAME?: string,
      CHECKTABLE?: string,
      REFTYPE?: string,
      DDTEXT?: string,
    }[]} | undefined;
  private parsedType: AbstractType | undefined = undefined;
 
  public getType(): string {
    return "TABL";
  }
 
  public getDescription(): string | undefined {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
 
    return this.parsedData?.description;
  }
 
  public getSecondaryIndexes(): SecondaryIndex[] | undefined {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
 
    return this.parsedData?.secondaryIndexes;
  }
 
  public getAllowedNaming(): IAllowedNaming {
    let length = 30;
    const regex = /^((\/[A-Z_\d]{3,8}\/)|[a-zA-Z0-9]{3}|CI_)\w+$/;
 
    if (this.getTableCategory() === TableCategory.Transparent) {
      length = 16;
    }
 
    return {
      maxLength: length,
      allowNamespace: true,
      customRegex: regex,
    };
  }
 
  public setDirty(): void {
    this.parsedData = undefined;
    this.parsedType = undefined;
    super.setDirty();
  }
 
  public listKeys(reg: IRegistry): 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" && p.FIELDNAME === ".INCLUDE") {
        const lookup = new DDIC(reg).lookupTableOrView(p.PRECFIELD).type;
        if (lookup instanceof Types.StructureType) {
          for (const c of lookup.getComponents()) {
            ret.push(c.name);
          }
        }
      } else if (p.KEYFLAG === "X") {
        ret.push(p.FIELDNAME);
      }
    }
    return ret;
  }
 
  public parseType(reg: IRegistry): AbstractType {
    if (this.parsedData === undefined) {
      this.parseXML();
      if (this.parsedData === undefined) {
        return new Types.UnknownType("Table, parser error");
      }
    }
 
    if (reg.getConfig().getVersion() === Version.Cloud
        && this.parsedData.dataClass === "USER3") {
      return new Types.UnknownType("Data class = USER3 not allowed in cloud");
    }
 
    if (this.getTableCategory() === TableCategory.Transparent
        && this.listKeys(reg).length === 0) {
      return new Types.UnknownType("Table " + this.getName() + " has no key fields");
    }
 
    if (this.parsedType) {
      return this.parsedType;
    }
 
    const references: IObjectAndToken[] = [];
    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
        const lookup = ddic.lookupDataElement(field.ROLLNAME);
        components.push({name: field.FIELDNAME, type: lookup.type});
        if (lookup.object) {
          references.push({object: lookup.object});
        }
      } else if (field.FIELDNAME === ".INCLUDE"
          || field.FIELDNAME.startsWith(".INCLU-")) {
        if (field.PRECFIELD === undefined) {
          return new Types.UnknownType("Table, parser error, PRECFIELD undefined, " + this.getName());
        }
        const lookup = ddic.lookupTableOrView(field.PRECFIELD);
        let found = lookup.type;
        if (lookup.object) {
          references.push({object: lookup.object});
        }
        if (found instanceof TypedIdentifier) {
          found = found.getType();
        }
        if (found instanceof Types.StructureType) {
          if (field.GROUPNAME !== undefined) {
            components.push({name: field.GROUPNAME, type: found, asInclude: true});
          }
 
          // Note: "--AP" are .APPENDS
          if (field.FIELDNAME.startsWith(".INCLU-") === false || field.FIELDNAME === ".INCLU--AP") {
            for (const c of found.getComponents()) {
              components.push({name: c.name, type: c.type});
            }
          } else if (field.FIELDNAME.startsWith(".INCLU-")) {
            const postfix = field.FIELDNAME.substring(".INCLU-".length);
            for (const c of found.getComponents()) {
              components.push({name: c.name + postfix, type: c.type});
            }
          }
        } else if ((field.PRECFIELD?.startsWith("CI_") || field.PRECFIELD?.startsWith("SI_"))
            && found instanceof Types.UnknownType) {
          continue;
        } else if (found instanceof Types.UnknownType) {
          return found;
        } else if (found instanceof Types.VoidType) {
          // set the full structure to void
          return found;
        } else {
          components.push({name: field.FIELDNAME, type: found});
        }
      } else if (comptype === "S") {
        const lookup = ddic.lookupTableOrView(field.ROLLNAME);
        components.push({name: field.FIELDNAME, type: lookup.type});
        if (lookup.object) {
          references.push({object: lookup.object});
        }
      } else if (comptype === "R") {
        if (field.ROLLNAME === undefined) {
          throw new Error("Expected ROLLNAME");
        }
        if (field.ROLLNAME === "DATA") {
          components.push({
            name: field.FIELDNAME,
            type: new DataReference(AnyType.get())});
        } else if (field.ROLLNAME === "OBJECT") {
          components.push({
            name: field.FIELDNAME,
            type: new GenericObjectReferenceType()});
        } else if (field.REFTYPE === "S") {
          const lookup = ddic.lookupTableOrView(field.ROLLNAME);
          components.push({name: field.FIELDNAME, type: new DataReference(lookup.type)});
          if (lookup.object) {
            references.push({object: lookup.object});
          }
        } else if (field.REFTYPE === "L") {
          const lookup = ddic.lookupTableType(field.ROLLNAME);
          components.push({name: field.FIELDNAME, type: new DataReference(lookup.type)});
          if (lookup.object) {
            references.push({object: lookup.object});
          }
        } else if (field.REFTYPE === "E") {
          const lookup = ddic.lookupDataElement(field.ROLLNAME);
          components.push({name: field.FIELDNAME, type: new DataReference(lookup.type)});
          if (lookup.object) {
            references.push({object: lookup.object});
          }
        } else {
          const lookup = ddic.lookupObject(field.ROLLNAME);
          components.push({name: field.FIELDNAME, type: lookup.type});
          if (lookup.object) {
            references.push({object: lookup.object});
          }
        }
      } else if (comptype === "L") {
        const lookup = ddic.lookupTableType(field.ROLLNAME);
        components.push({name: field.FIELDNAME, type: lookup.type});
        if (lookup.object) {
          references.push({object: lookup.object});
        }
      } else if (comptype === "") { // built in
        const datatype = field.DATATYPE;
        if (datatype === undefined) {
          throw new Error("Expected DATATYPE, while parsing TABL " + this.getName());
        }
        const length = field.LENG ? field.LENG : field.INTLEN;
        components.push({
          name: field.FIELDNAME,
          type: ddic.textToType({
            text: datatype,
            length: length,
            decimals: field.DECIMALS,
            infoText: this.getName() + "-" + field.FIELDNAME,
            description: field.DDTEXT,
          })});
      } else {
        components.push({
          name: field.FIELDNAME,
          type: new Types.UnknownType("Table " + this.getName() + ", unknown component type \"" + comptype + "\"")});
      }
 
      if (field.CHECKTABLE) {
        const lookup = ddic.lookupTableOrView2(field.CHECKTABLE);
        if (lookup) {
          references.push({object: lookup});
        }
      }
    }
 
    if (components.length === 0) {
      return new Types.UnknownType("Table/Structure " + this.getName() + " does not contain any components");
    }
 
    reg.getDDICReferences().setUsing(this, references);
    this.parsedType = new Types.StructureType(components, this.getName(), this.getName(), this.getDescription());
    return this.parsedType;
  }
 
  public getTableCategory(): TableCategory | undefined {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
 
    return this.parsedData?.tableCategory;
  }
 
  public getEnhancementCategory(): EnhancementCategory {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    if (this.parsedData?.enhancementCategory === undefined) {
      return EnhancementCategory.NotClassified;
    }
    return this.parsedData.enhancementCategory;
  }
 
///////////////
 
  private parseXML() {
    const parsed = super.parseRaw2();
    if (parsed === undefined) {
      return;
    }
 
    this.parsedData = {fields: []};
 
    if (parsed.abapGit === undefined) {
      return;
    }
 
// 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;
    this.parsedData.description = parsed.abapGit["asx:abap"]["asx:values"]?.DD02V?.DDTEXT;
    this.parsedData.dataClass = parsed.abapGit["asx:abap"]["asx:values"]?.DD09L?.TABART;
 
// 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,
        GROUPNAME: field.GROUPNAME,
        CHECKTABLE: field.CHECKTABLE,
        REFTYPE: field.REFTYPE,
        DDTEXT: field.DDTEXT,
      });
    }
 
// secondary indexes
    const indexes = parsed.abapGit["asx:abap"]["asx:values"]?.DD12V;;
    this.parsedData.secondaryIndexes = [];
    for (const index of xmlToArray(indexes?.DD12V)) {
      const indexName = index.INDEXNAME;
      const indexFields: string[] = [];
      const indexFieldsXml = parsed.abapGit["asx:abap"]["asx:values"]?.DD17V;
      for (const indexField of xmlToArray(indexFieldsXml?.DD17V)) {
        if (indexField.INDEXNAME === indexName) {
          // assumption: fields are listed by POSITION in the xml
          indexFields.push(indexField.FIELDNAME);
        }
      }
      this.parsedData.secondaryIndexes.push({
        name: indexName,
        fields: indexFields,
      });
    }
  }
 
}