All files / src ddic.ts

70.81% Statements 114/161
61.45% Branches 102/166
100% Functions 14/14
70.81% Lines 114/161

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            1x     1x       1x       2216x           542x     542x 14x   528x 528x 427x     101x   101x 157x 157x 35x     122x 122x       122x 56x   66x         101x   101x       222x     222x       2x 2x 1x   1x 1x     1x     1x         140x 140x 7x     133x 133x       133x 133x 1x     132x 132x       132x 132x 7x     125x 125x             125x         129x 129x 12x     117x 19x   98x         6x 6x 1x 5x     5x         7x     7x 7x 1x 6x     6x         36x     36x 36x 7x   29x 29x     29x 29x           29x       45x     45x 45x 3x   42x 42x     42x 42x     42x 42x           42x       2x     2x 2x 1x 1x     1x         29x     29x 29x   29x 3x   26x         1x     1x 1x 1x                                 61x   61x               2x   2x 1x   1x                               37x     37x     2x     2x                                       6x         7x       2x                 5x          
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 {DataDefinition} from "./objects";
 
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 {
    Iif (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);
      Iif (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;
  }
 
  public inErrorNamespace(name: string | undefined): boolean {
    Iif (name === undefined) {
      return true;
    }
    return this.reg.inErrorNamespace(name);
  }
 
  public lookupObject(name: string): AbstractType {
    const globalClas = this.reg.getObject("CLAS", name)?.getIdentifier();
    if (globalClas) {
      return new ObjectReferenceType(globalClas);
    }
    const globalIntf = this.reg.getObject("INTF", name)?.getIdentifier();
    Iif (globalIntf) {
      return new ObjectReferenceType(globalIntf);
    }
    Iif (this.inErrorNamespace(name) === true) {
      return new UnknownType(name);
    } else {
      return new VoidType(name);
    }
  }
 
  public lookupNoVoid(name: string): AbstractType | undefined {
    const foundTABL = this.reg.getObject("TABL", name) as Table | undefined;
    if (foundTABL) {
      return foundTABL.parseType(this.reg);
    }
 
    const foundVIEW = this.reg.getObject("VIEW", name) as Table | undefined;
    Iif (foundVIEW) {
      return foundVIEW.parseType(this.reg);
    }
 
    const foundTTYP = this.reg.getObject("TTYP", name) as TableType | undefined;
    if (foundTTYP) {
      return foundTTYP.parseType(this.reg);
    }
 
    const foundDDLS = this.reg.getObject("DDLS", name) as DataDefinition | undefined;
    Iif (foundDDLS) {
      return foundDDLS.parseType(this.reg);
    }
 
    const foundDTEL = this.reg.getObject("DTEL", name) as DataElement | undefined;
    if (foundDTEL) {
      return foundDTEL.parseType(this.reg);
    }
 
    const upper = name.toUpperCase();
    for (const obj of this.reg.getObjectsByType("DDLS")) {
      const ddls = obj as DataDefinition;
      if (ddls.getSQLViewName() === upper) {
        return ddls.parseType(this.reg);
      }
    }
 
    return undefined;
  }
 
  /** lookup with voiding and unknown types */
  public lookup(name: string): AbstractType {
    const found = this.lookupNoVoid(name);
    if (found) {
      return found;
    }
 
    if (this.reg.inErrorNamespace(name)) {
      return new Types.UnknownType(name + " not found, lookup");
    } else {
      return new Types.VoidType(name);
    }
  }
 
  public lookupDomain(name: string): AbstractType {
    const found = this.reg.getObject("DOMA", name) as Domain | undefined;
    if (found) {
      return found.parseType(this.reg);
    } else Iif (this.reg.inErrorNamespace(name)) {
      return new Types.UnknownType(name + ", lookupDomain");
    } else {
      return new Types.VoidType(name);
    }
  }
 
  public lookupDataElement(name: string | undefined): AbstractType {
    Iif (name === undefined) {
      return new Types.UnknownType("undefined, lookupDataElement");
    }
    const found = this.reg.getObject("DTEL", name) as DataElement | undefined;
    if (found) {
      return found.parseType(this.reg);
    } else Iif (this.reg.inErrorNamespace(name)) {
      return new Types.UnknownType(name + " not found, lookupDataElement");
    } else {
      return new Types.VoidType(name);
    }
  }
 
  public lookupTableOrView(name: string | undefined): AbstractType {
    Iif (name === undefined) {
      return new Types.UnknownType("undefined, lookupTableOrView");
    }
    const foundTABL = this.reg.getObject("TABL", name) as Table | undefined;
    if (foundTABL) {
      return foundTABL.parseType(this.reg);
    }
    const foundDDLS = this.reg.getObject("DDLS", name) as DataDefinition | undefined;
    Iif (foundDDLS) {
      return foundDDLS.parseType(this.reg);
    }
    const upper = name.toUpperCase();
    for (const obj of this.reg.getObjectsByType("DDLS")) {
      const ddls = obj as DataDefinition;
      if (ddls.getSQLViewName() === upper) {
        return ddls.parseType(this.reg);
      }
    }
    return this.lookupView(name);
  }
 
  public lookupTableOrView2(name: string | undefined): Table | DataDefinition | View | undefined {
    Iif (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;
    Iif (foundVIEW) {
      return foundVIEW;
    }
    const foundDDLS = this.reg.getObject("DDLS", name) as DataDefinition | undefined;
    Iif (foundDDLS) {
      return foundDDLS;
    }
    const upper = name.toUpperCase();
    for (const obj of this.reg.getObjectsByType("DDLS")) {
      const ddls = obj as DataDefinition;
      if (ddls.getSQLViewName() === upper) {
        return ddls;
      }
    }
    return undefined;
  }
 
  public lookupTable(name: string | undefined): AbstractType {
    Iif (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 Iif (this.reg.inErrorNamespace(name)) {
      return new Types.UnknownType(name + " not found, lookupTable");
    } else {
      return new Types.VoidType(name);
    }
  }
 
  public lookupView(name: string | undefined): AbstractType {
    Iif (name === undefined) {
      return new Types.UnknownType("undefined, lookupView");
    }
    const found = this.reg.getObject("VIEW", name) as Table | undefined;
    Iif (found) {
      return found.parseType(this.reg);
    } else if (this.reg.inErrorNamespace(name)) {
      return new Types.UnknownType(name + " not found, lookupView");
    } else {
      return new Types.VoidType(name);
    }
  }
 
  public lookupTableType(name: string | undefined): AbstractType {
    Iif (name === undefined) {
      return new Types.UnknownType("undefined, lookupTableType");
    }
    const found = this.reg.getObject("TTYP", name) as TableType | undefined;
    Eif (found) {
      return found.parseType(this.reg);
    } else if (this.reg.inErrorNamespace(name)) {
      return new Types.UnknownType(name + " not found, lookupTableType");
    } else {
      return new Types.VoidType(name);
    }
  }
 
  public textToType(
    text: string | undefined,
    length: string | undefined,
    decimals: string | undefined,
    parent: string,
    qualify = true): AbstractType {
 
// todo, support short strings, and length of different integers, NUMC vs CHAR, min/max length
 
    const qualified = qualify ? parent : undefined;
 
    switch (text) {
      case "DEC":      // 1 <= len <= 31
      case "D16F":     // 1 <= len <= 31
      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
        Iif (length === undefined) {
          return new Types.UnknownType(text + " unknown length, " + parent, parent);
        } else if (decimals === undefined) {
          return new Types.PackedType(parseInt(length, 10), 0, qualified);
        }
        return new Types.PackedType(parseInt(length, 10), parseInt(decimals, 10), qualified);
      case "ACCP":
        return new Types.CharacterType(6, qualified); // YYYYMM
      case "LANG":
        return new Types.CharacterType(1, qualified);
      case "CLNT":
        return new Types.CharacterType(3, qualified);
      case "CUKY":
        return new Types.CharacterType(5, qualified);
      case "UNIT":  // 2 <= len <= 3
        return new Types.CharacterType(3, qualified);
      case "UTCLONG":
        return new Types.CharacterType(27, qualified);
      case "NUMC": // 1 <= len <= 255
      case "CHAR": // 1 <= len <= 30000 (1333 for table fields)
      case "LCHR": // 256 <= len <= 32000
        Iif (length === undefined) {
          return new Types.UnknownType(text + " unknown length", parent);
        }
        return new Types.CharacterType(parseInt(length, 10), qualified);
      case "RAW":  // 1 <= len <= 32000
      case "LRAW": // 256 <= len <= 32000
        Iif (length === undefined) {
          return new Types.UnknownType(text + " unknown length", parent);
        }
        return new Types.HexType(parseInt(length, 10), qualified);
      case "TIMS":
        return new Types.TimeType(qualified); //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 (length === undefined) {
          return new Types.UnknownType(text + " unknown length", parent);
        }
        return new Types.FloatingPointType(parseInt(length, 10), qualified);
      case "DATS":
        return new Types.DateType(qualified); //YYYYMMDD
      case "INT1":
      case "INT2":
      case "INT4":
      case "INT8":
        return new Types.IntegerType(qualified);
      case "SSTR":    // 1 <= len <= 1333
      case "SSTRING": // 1 <= len <= 1333
      case "STRG":    // 256 <= len
      case "STRING":  // 256 <= len
        return new Types.StringType(qualified);
      case "RSTR":      // 256 <= len
      case "RAWSTRING": // 256 <= len
      case "GEOM_EWKB":
        return new Types.XStringType(qualified);
      case "D16S":
      case "D34S":
      case "DF16_SCL":
      case "DF34_SCL":
      case "PREC":
      case "VARC":
        return new Types.UnknownType(text + " is an obsolete data type", parent);
      default:
        return new Types.UnknownType(text + " unknown", parent);
    }
  }
 
}