All files / src ddic.ts

92.81% Statements 452/487
85.58% Branches 190/222
100% Functions 19/19
92.81% Lines 452/487

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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 4881x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 112157x 112157x 1x 1x 1x 1x 1412x 5x 5x 1412x 31x 31x 1376x 1412x 1144x 1144x 232x 232x 232x 1412x 355x 355x 53x 53x 302x 355x 355x     302x 355x 123x 355x 179x 179x 355x 232x 232x 1412x 1412x 1412x 1412x 1x 1x 1x 76692x 76692x 1187x 76692x 1924x 76692x 73581x 76692x 76692x 1x 1x 97393x 97393x 13696x 97393x 1051x 97393x 1052x 97393x 1017x 97393x 179x 97393x 341x 97393x 2x 97393x 1078x 97393x 332x 97393x 1x 97393x 9x 97393x 466x 97393x 229x 97393x 1924x 97393x 463x 97393x 916x 97393x 238x 97393x 41777x 97393x 1188x 97393x 1461x 97393x 1453x 1140x 1453x 167x 313x 146x 146x 97393x 5304x 5057x 5304x 247x 247x 97393x 1813x 1731x 1813x 82x 82x 97393x 1174x 1125x 1174x 49x 49x 97393x 20229x 20229x 1x 1x 3329x     3329x 3329x 1x 1x 21x 21x 21x 11x 11x 11x 11x 11x 10x 21x 21x 9x 9x 9x 9x 9x 21x   21x 1x 1x 21x 1x 1x 3192x 3192x 304x 304x 2888x 2888x 3192x 2x 2x 2886x 2886x 3192x 62x 62x 2824x 2824x 3192x 574x 574x 2250x 2250x 3192x 6x 6x 2244x 2244x 3192x 2x 2x 2242x 2242x 2242x 1x 1x 1x 2266x 2266x 16x 16x 2250x 2250x 1x 1x 2969x     2969x 2969x 2969x 472x 472x 16x 16x 16x 472x 97x 97x 472x 2872x 2872x 2872x 1x 1x 1x 2041x 2041x 824x 824x 1217x 2041x 188x 2041x 1029x 1029x 2041x 1x 1x 200x 200x 196x 200x 2x 2x 2x 2x 200x 1x 1x 237x     237x 237x 123x 237x 31x 114x 83x 83x 237x 1x 1x 758x     758x 758x 395x 395x 363x 758x 41x 41x 322x 322x 1x 1x 1x 467x     467x 467x 163x 163x 304x 467x     304x 467x 45x 45x 259x 259x 1x 1x 11x     11x 11x 9x 11x 1x 1x 1x 1x 11x 1x 1x 322x     322x 322x   322x 82x 322x 240x 240x 322x 1x 1x 11x     11x 11x 10x 11x   1x 1x 1x 11x 1x 1x 1x 99x 99x 1x 1x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 33x     33x 33x 33x 33x 1859x 1859x 1859x 11x 1859x 31x 1859x   1859x 1x 1859x 1859x 75x 6x 6x 69x 1859x 31x     31x 1859x 1859x 1188x     1188x 1859x 1859x 23x     23x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 1859x 52x     52x 1859x 1859x 1859x 1859x 24x 1859x 23x 1859x 126x 1859x 24x 2x 2x 22x 1859x 1859x 1859x 1859x 132x 1859x 1859x 1859x 22x 1859x 1859x 1859x 1859x 1859x 1859x   1859x 2x 1859x 1859x 1x 1x  
/* eslint-disable default-case */
import {IRegistry} from "./_iregistry";
import {AbstractType} from "./abap/types/basic/_abstract_type";
import {Domain} from "./objects/domain";
import {DataElement} from "./objects/data_element";
import {Table} from "./objects/table";
import {TableType} from "./objects/table_type";
import * as Types from "./abap/types/basic";
import {ABAPObject} from "./objects/_abap_object";
import {InfoClassDefinition} from "./abap/4_file_information/_abap_file_information";
import {ObjectReferenceType, UnknownType, VoidType} from "./abap/types/basic";
import {View} from "./objects/view";
import {CDSType, DataDefinition} from "./objects";
import {IObject} from "./objects/_iobject";
import {ABAPRelease, Release, releaseAtLeast} from "./version";
 
export interface ILookupResult {
  type: AbstractType;
  object?: IObject;
}
 
export class DDIC {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  // the class might be local with a local super class with a global exception class as super
  // todo: returns true for both local and global exception classes
  public isException(def: InfoClassDefinition | undefined, _obj: ABAPObject): boolean {
    if (def === undefined) {
      return false;
    }
    if (def.name.toUpperCase() === "CX_ROOT") {
      return true;
    }
    let superClassName = def.superClassName;
    if (superClassName === undefined) {
      return false;
    }
 
    let i = 0;
    // max depth, make sure not to hit cyclic super class defintions
    while (i++ < 10 && superClassName !== undefined) {
      const found = this.reg.getObject("CLAS", superClassName) as ABAPObject | undefined;
      if (found === undefined) {
        break;
      }
 
      const superDef: InfoClassDefinition | undefined = found.getMainABAPFile()?.getInfo().getClassDefinitionByName(superClassName);
      if (superDef === undefined) {
        break;
      }
 
      if (superDef.superClassName) {
        superClassName = superDef.superClassName;
      } else {
        break;
      }
    }
 
    // todo, this should check for "CX_ROOT"
    const isException = (superClassName?.match(/^.?cx_.*$/i) || superClassName?.match(/^\/.+\/cx_.*$/i)) ? true : false;
 
    return isException;
  }
 
  /** first release where the built-in ABAP type exists, undefined if it exists in all supported releases */
  public static builtinTypeRelease(name: string): ABAPRelease | undefined {
    switch (name.toUpperCase()) {
      case "INT8":
        return Release.v750;
      case "UTCLONG":
        return Release.v754;
      default:
        return undefined;
    }
  }
 
  public lookupBuiltinType(name: string, length?: number, decimals?: number, qualifiedName?: string): AbstractType | undefined {
    switch (name) {
      case "STRING":
        return Types.StringType.get({qualifiedName: qualifiedName || name});
      case "XSTRING":
        return Types.XStringType.get({qualifiedName: qualifiedName || name});
      case "D":
        return new Types.DateType({qualifiedName: qualifiedName || name});
      case "T":
        return new Types.TimeType({qualifiedName: qualifiedName || name});
      case "XSEQUENCE":
        return new Types.XSequenceType({qualifiedName: qualifiedName});
      case "CLIKE":
        return Types.CLikeType.get();
      case "DECFLOAT":
        return new Types.DecFloatType({qualifiedName: qualifiedName});
      case "ANY":
        return Types.AnyType.get({qualifiedName: qualifiedName});
      case "SIMPLE":
        return Types.SimpleType.get();
      case "%_C_POINTER":
        return new Types.HexType(8, qualifiedName);
      case "TABLE":
        return new Types.TableType(Types.AnyType.get(), {withHeader: false, keyType: Types.TableKeyType.default});
      case "DATA":
        return new Types.DataType({qualifiedName: qualifiedName});
      case "NUMERIC":
        return new Types.NumericGenericType({qualifiedName: qualifiedName});
      case "UTCLONG": // release checked by check_syntax, see DDIC.builtinTypeRelease
        return new Types.UTCLongType({qualifiedName: qualifiedName});
      case "DECFLOAT16":
        return new Types.DecFloat16Type({qualifiedName: qualifiedName});
      case "DECFLOAT34":
        return new Types.DecFloat34Type({qualifiedName: qualifiedName});
      case "CSEQUENCE":
        return new Types.CSequenceType({qualifiedName: qualifiedName});
      case "I":
        return Types.IntegerType.get({qualifiedName: qualifiedName || name});
      case "INT8": // release checked by check_syntax, see DDIC.builtinTypeRelease
        return new Types.Integer8Type({qualifiedName: qualifiedName || name});
      case "F":
        return new Types.FloatType({qualifiedName: qualifiedName || name});
      case "P":
        if (length && decimals) {
          return new Types.PackedType(length, decimals, {qualifiedName: qualifiedName});
        } else if (length) {
          return new Types.PackedType(length, 0, {qualifiedName: qualifiedName});
        } else {
          return new Types.PackedType(8, decimals ?? 0, {qualifiedName: qualifiedName});
        }
      case "C":
        if (length) {
          return new Types.CharacterType(length, {qualifiedName: qualifiedName});
        } else {
          return new Types.CharacterType(1, {qualifiedName: qualifiedName});
        }
      case "X":
        if (length) {
          return new Types.HexType(length, qualifiedName);
        } else {
          return new Types.HexType(1, qualifiedName);
        }
      case "N":
        if (length) {
          return new Types.NumericType(length, qualifiedName);
        } else {
          return new Types.NumericType(1, qualifiedName);
        }
    }
    return undefined;
  }
 
  public inErrorNamespace(name: string | undefined): boolean {
    if (name === undefined) {
      return true;
    }
    return this.reg.inErrorNamespace(name);
  }
 
  public lookupObject(name: string): ILookupResult {
    const clas = this.reg.getObject("CLAS", name);
    const globalClas = clas?.getIdentifier();
    if (globalClas) {
      return {
        type: new ObjectReferenceType(globalClas, {qualifiedName: name, RTTIName: "\\CLASS=" + name}),
        object: clas,
      };
    }
    const intf = this.reg.getObject("INTF", name);
    const globalIntf = intf?.getIdentifier();
    if (globalIntf) {
      return {
        type: new ObjectReferenceType(globalIntf, {qualifiedName: name, RTTIName: "\\INTERFACE=" + name}),
        object: intf,
      };
    }
    if (this.inErrorNamespace(name) === true) {
      return {type: new UnknownType(name)};
    } else {
      return {type: VoidType.get(name)};
    }
  }
 
  public lookupNoVoid(name: string): ILookupResult | undefined {
    const foundTABL = this.reg.getObject("TABL", name) as Table | undefined;
    if (foundTABL) {
      return {type: foundTABL.parseType(this.reg), object: foundTABL};
    }
 
    const foundVIEW = this.reg.getObject("VIEW", name) as Table | undefined;
    if (foundVIEW) {
      return {type: foundVIEW.parseType(this.reg), object: foundVIEW};
    }
 
    const foundTTYP = this.reg.getObject("TTYP", name) as TableType | undefined;
    if (foundTTYP) {
      return {type: foundTTYP.parseType(this.reg), object: foundTTYP};
    }
 
    const foundDTEL = this.reg.getObject("DTEL", name) as DataElement | undefined;
    if (foundDTEL) {
      return {type: foundDTEL.parseType(this.reg), object: foundDTEL};
    }
 
    const foundDRTY = this.lookupCDSType(name);
    if (foundDRTY) {
      return foundDRTY;
    }
 
    const foundDDLS = this.lookupDDLS(name);
    if (foundDDLS) {
      return foundDDLS;
    }
 
    return undefined;
  }
 
  /** CDS type, DRTY: a CDS simple type or a CDS enumerated type */
  public lookupCDSType(name: string): ILookupResult | undefined {
    const found = this.reg.getObject("DRTY", name) as CDSType | undefined;
    if (found) {
      return {type: found.parseType(this.reg), object: found};
    }
    return undefined;
  }
 
  public lookupDDLS(name?: string) {
    if (name === undefined) {
      return undefined;
    }
 
    const upper = name.toUpperCase();
    for (const obj of this.reg.getObjectsByType("DDLS")) {
      const ddls = obj as DataDefinition;
      if (ddls.isExtension()) {
        // an extension is named like the entity it extends, it does not define that entity
        continue;
      }
      if (ddls.getSQLViewName() === upper || ddls.getDefinitionName()?.toUpperCase() === upper) {
        return {type: ddls.parseType(this.reg), object: ddls};
      }
    }
 
    return undefined;
  }
 
  /** lookup with voiding and unknown types */
  public lookup(name: string): ILookupResult {
    const found = this.lookupNoVoid(name);
    if (found) {
      return found;
    }
 
    if (this.reg.inErrorNamespace(name)) {
      return {type: new Types.UnknownType(name + " not found, lookup")};
    } else {
      return {type: Types.VoidType.get(name)};
    }
  }
 
  public lookupDomain(name: string, dataElement?: string, description?: string): ILookupResult {
    const found = this.reg.getObject("DOMA", name) as Domain | undefined;
    if (found) {
      return {type: found.parseDomainType(this.reg, dataElement, description), object: found};
    } else if (this.reg.inErrorNamespace(name)) {
      return {type: new Types.UnknownType(name + ", lookupDomain"), object: undefined};
    } else {
      return {type: Types.VoidType.get(name), object: undefined};
    }
  }
 
  public lookupDataElement(name: string | undefined): ILookupResult {
    if (name === undefined) {
      return {type: new Types.UnknownType("undefined, lookupDataElement")};
    }
    const found = this.reg.getObject("DTEL", name) as DataElement | undefined;
    if (found) {
      return {type: found.parseType(this.reg), object: found};
    } else if (this.reg.inErrorNamespace(name)) {
      return {type: new Types.UnknownType(name + " not found, lookupDataElement")};
    } else {
      return {type: Types.VoidType.get(name)};
    }
  }
 
  public lookupTableOrView(name: string | undefined): ILookupResult {
    if (name === undefined) {
      return {type: new Types.UnknownType("undefined, lookupTableOrView")};
    }
    const foundTABL = this.reg.getObject("TABL", name) as Table | undefined;
    if (foundTABL) {
      return {type: foundTABL.parseType(this.reg), object: foundTABL};
    }
    const foundDDLS = this.lookupDDLS(name);
    if (foundDDLS) {
      return foundDDLS;
    }
    return this.lookupView(name);
  }
 
  /** this method only looks up the object, does not parse the type */
  public lookupTableOrView2(name: string | undefined): Table | DataDefinition | View | undefined {
    if (name === undefined) {
      return undefined;
    }
    const foundTABL = this.reg.getObject("TABL", name) as Table | undefined;
    if (foundTABL) {
      return foundTABL;
    }
    const foundVIEW = this.reg.getObject("VIEW", name) as View | undefined;
    if (foundVIEW) {
      return foundVIEW;
    }
    const foundDDLS = this.lookupDDLS(name);
    if (foundDDLS) {
      return foundDDLS.object;
    }
    return undefined;
  }
 
  public lookupTable(name: string | undefined): AbstractType {
    if (name === undefined) {
      return new Types.UnknownType("undefined, lookupTable");
    }
    const found = this.reg.getObject("TABL", name) as Table | undefined;
    if (found) {
      return found.parseType(this.reg);
    } else if (this.reg.inErrorNamespace(name)) {
      return new Types.UnknownType(name + " not found, lookupTable");
    } else {
      return Types.VoidType.get(name);
    }
  }
 
  private lookupView(name: string | undefined): ILookupResult {
    if (name === undefined) {
      return {type: new Types.UnknownType("undefined, lookupView")};
    }
    const found = this.reg.getObject("VIEW", name) as Table | undefined;
    if (found) {
      return {type: found.parseType(this.reg), object: found};
    } else if (this.reg.inErrorNamespace(name)) {
      return {type: new Types.UnknownType(name + " not found, lookupView")};
    } else {
      return {type: Types.VoidType.get(name)};
    }
  }
 
  public lookupTableType(name: string | undefined): ILookupResult {
    if (name === undefined) {
      return {type: new Types.UnknownType("undefined, lookupTableType")};
    }
    const found = this.reg.getObject("TTYP", name) as TableType | undefined;
    if (found) {
      return {type: found.parseType(this.reg), object: found};
    } else if (this.reg.inErrorNamespace(name)) {
      return {type: new Types.UnknownType(name + " not found, lookupTableType")};
    } else {
      return {type: Types.VoidType.get(name)};
    }
  }
 
  // DDIC types newer than the configured release do not exist, open-abap supports them
  private beforeRelease(required: ABAPRelease): boolean {
    return !releaseAtLeast(this.reg.getConfig().getRelease(), required) && !this.reg.getConfig().isOpenABAP();
  }
 
  public textToType(input: {
      text: string | undefined,
      length: string | undefined,
      decimals: string | undefined,
      infoText: string,
      qualifiedName?: string,
      conversionExit?: string,
      ddicName?: string,
      description?: string,
    }): AbstractType {
 
// todo: support short strings, and length of different integers, NUMC vs CHAR, min/max length
 
    const extra = {
      qualifiedName: input.qualifiedName,
      conversionExit: input.conversionExit,
      ddicName: input.ddicName,
      description: input.description,
    };
 
    switch (input.text) {
      case "DEC":      // 1 <= len <= 31
      case "D16F":     // 1 <= len <= 31
      case "D16D":
      case "D34D":
      case "D34F":     // 1 <= len <= 31
      case "DF16_DEC": // 1 <= len <= 31
      case "DF34_DEC": // 1 <= len <= 31
      case "CURR":     // 1 <= len <= 31
      case "QUAN": {   // 1 <= len <= 31
        if (input.length === undefined) {
          return new Types.UnknownType(input.text + " unknown length, " + input.infoText, input.infoText);
        }
        const packedLength = Math.ceil((parseInt(input.length, 10) + 1) / 2);
        const decimals = input.decimals === undefined ? 0 : parseInt(input.decimals, 10);
        return new Types.PackedType(packedLength, decimals, extra);
      }
      case "ACCP":
        return new Types.CharacterType(6, extra); // YYYYMM
      case "LANG":
        return new Types.CharacterType(1, extra);
      case "CLNT":
        return new Types.CharacterType(3, extra);
      case "CUKY":
        return new Types.CharacterType(5, extra);
      case "UNIT":  // 2 <= len <= 3
        return new Types.CharacterType(3, extra);
      case "UTCL":
      case "UTCLONG":
        if (this.beforeRelease(Release.v754)) {
          return new Types.UnknownType(input.text + " requires 7.54", input.infoText);
        }
        return new Types.UTCLongType(extra);
      case "NUMC": // 1 <= len <= 255
        if (input.length === undefined) {
          return new Types.UnknownType(input.text + " unknown length", input.infoText);
        }
        return new Types.NumericType(parseInt(input.length, 10), extra);
      case "CHAR": // 1 <= len <= 30000 (1333 for table fields)
      case "LCHR": // 256 <= len <= 32000
        if (input.length === undefined) {
          return new Types.UnknownType(input.text + " unknown length", input.infoText);
        }
        return new Types.CharacterType(parseInt(input.length, 10), extra);
      case "RAW":  // 1 <= len <= 32000
      case "LRAW": // 256 <= len <= 32000
        if (input.length === undefined) {
          return new Types.UnknownType(input.text + " unknown length", input.infoText);
        }
        return new Types.HexType(parseInt(input.length, 10), extra);
      case "TIMN": // Native HANA
      case "TIMS":
        return new Types.TimeType(extra); //HHMMSS
      case "DECFLOAT16": // len = 16
      case "DECFLOAT34": // len = 34
      case "D16R":       // len = 16
      case "D34R":       // len = 34
      case "DF16_RAW":   // len = 16
      case "DF34_RAW":   // len = 34
      case "FLTP":       // len = 16
        if (input.length === undefined) {
          return new Types.UnknownType(input.text + " unknown length", input.infoText);
        }
        return new Types.FloatingPointType(parseInt(input.length, 10), extra);
      case "DATN": // Native HANA
      case "DATS":
        return new Types.DateType(extra); //YYYYMMDD
      case "INT1":
        return Types.IntegerType.get(extra, 1);
      case "INT2":
        return Types.IntegerType.get(extra, 2);
      case "INT4":
        return Types.IntegerType.get(extra);
      case "INT8":
        if (this.beforeRelease(Release.v750)) {
          return new Types.UnknownType(input.text + " requires 7.50", input.infoText);
        }
        return new Types.Integer8Type(extra);
      case "SSTR":    // 1 <= len <= 1333
      case "SSTRING": // 1 <= len <= 1333
      case "STRG":    // 256 <= len
      case "STRING":  // 256 <= len
        return Types.StringType.get(extra);
      case "RSTR":      // 256 <= len
      case "RAWSTRING": // 256 <= len
      case "GEOM_EWKB":
        return Types.XStringType.get(extra);
      case "D16S":
      case "D34S":
      case "DF16_SCL":
      case "DF34_SCL":
      case "PREC":
      case "VARC":
        return new Types.UnknownType(input.text + " is an obsolete data type", input.infoText);
      default:
        return new Types.UnknownType(input.text + " unknown basic ddic type", input.infoText);
    }
  }
 
}