All files / src/abap/types/basic table_type.ts

96.84% Statements 92/95
96.15% Branches 25/26
90% Functions 9/10
96.84% Lines 92/95

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 951x 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 1x 5184x 5184x 5184x 5184x 5184x 5184x 5184x 5184x 1x 1x 5184x 1x 1x 388x 388x 1x 1x 1112x 1112x 1x 1x 5379x 5379x 1x 1x 7171x 7171x 1x 1x 6x 6x 6x 1x 1x 20x 20x 20x   20x 20x 20x 20x 1x 1x 5150x 5150x 5150x 7x 7x 5143x 5143x 1x 1x 33x 33x 1x 1x     1x
import {AbstractType} from "./_abstract_type";
 
export enum TableAccessType {
  standard = "STANDARD",
  sorted = "SORTED",
  hashed = "HASHED",
  index = "INDEX",
  any = "ANY",
}
 
export enum TableKeyType {
  default = "DEFAULT",
  user = "USER",
  empty = "EMPTY",
}
 
export type ITableKey = {
  name: string,
  type: TableAccessType,
  keyFields: string[],
  isUnique: boolean,
};
 
export type ITableOptions = {
  withHeader: boolean,
  keyType: TableKeyType,
  primaryKey?: ITableKey,
  secondary?: ITableKey[],
};
 
export class TableType extends AbstractType {
  private readonly rowType: AbstractType;
  private readonly options: ITableOptions;
 
  public constructor(rowType: AbstractType, options: ITableOptions, qualifiedName?: string, description?: string, ddicName?: string) {
    super({
      qualifiedName: qualifiedName,
      description: description,
      ddicName: ddicName,
    });
    this.rowType = rowType;
    this.options = options;
    if (options.primaryKey?.type === TableAccessType.standard && options.primaryKey.isUnique === true) {
      throw new Error("STANDARD tables cannot have UNIQUE key");
    }
  }
 
  public getOptions(): ITableOptions {
    return this.options;
  }
 
  public isWithHeader(): boolean {
    return this.options.withHeader;
  }
 
  public getAccessType(): TableAccessType | undefined {
    return this.options.primaryKey?.type;
  }
 
  public getRowType(): AbstractType {
    return this.rowType;
  }
 
  public toABAP(): string {
// todo, this is used for downport, so use default key for now
    return "STANDARD TABLE OF " + this.rowType.toABAP() + " WITH DEFAULT KEY";
  }
 
  public toText(level: number) {
    const type = this.rowType;
 
    if (this.options.withHeader === true) {
      return "Table with header of " + type.toText(level + 1);
    } else {
      return "Table of " + type.toText(level + 1);
    }
  }
 
  public isGeneric() {
    if (this.options.primaryKey?.type !== TableAccessType.standard
        && this.options.keyType === TableKeyType.user
        && this.options.primaryKey?.keyFields.length === 0) {
      return true;
    }
    return this.rowType.isGeneric();
  }
 
  public containsVoid() {
    return this.rowType.containsVoid();
  }
 
  public toCDS() {
    return "abap.TODO_TABLE";
  }
}