All files / src/checker/checks/generic tableNotFound.ts

95% Statements 19/20
87.5% Branches 7/8
100% Functions 3/3
95% Lines 19/20
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 512x   2x   2x 2x     1x       1x       1x 1x 4x 2x   2x       1x                         1x 1x 1x 1x 1x     1x       2x  
import { extractTableReference } from "../../../lexer/lexer";
import { Query } from "../../../reader/query";
import { CheckerResult } from "../../checkerResult";
import { IChecker } from "../../interface";
import { Types } from "../../../lexer/tokens";
import { sprintf } from "sprintf-js";
 
class TableNotFound implements IChecker {
  public message = "Table '%s' does not exist in database '%s'.";
 
  public tables: string[];
  constructor(tables: any[]) {
    this.tables = this.cleanTables(tables);
  }
 
  public check(query: Query): CheckerResult {
    for (const line of query.lines) {
      for (const token of line.tokens) {
        if (token[0] === Types.TableReference) {
          const reference = extractTableReference(token[1]);
 
          if (
            !this.tables.includes(reference.table) &&
            reference.table !== "*"
          ) {
            return new CheckerResult(
              line.num,
              sprintf(this.message, reference.table, reference.database)
            );
          }
        }
      }
    }
 
    return new CheckerResult(0, "");
  }
 
  private cleanTables(tables: any): string[] {
    const cleanTables: string[] = [];
    for (const obj of tables) {
      const cleanTable = (<any>Object).values(obj)[0];
      Eif (cleanTable.length > 0) {
        cleanTables.push(cleanTable);
      }
    }
    return cleanTables;
  }
}
 
export { TableNotFound };