All files / src/checker/checks/use databaseNotFound.ts

100% Statements 14/14
100% Branches 6/6
100% Functions 3/3
100% Lines 13/13
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  2x   2x 2x     4x       4x     4x 4x 8x 4x 4x 2x           2x       2x  
import { Query } from "../../../reader/query";
import { CheckerResult } from "../../checkerResult";
import { IChecker } from "../../interface";
import { Types } from "../../../lexer/tokens";
import { sprintf } from "sprintf-js";
 
class DatabaseNotFound implements IChecker {
  public message = "Database '%s' does not exist.";
 
  public databases: string[];
  constructor(databases: any[]) {
    this.databases = databases.map(result => result.Database);
  }
  public check(query: Query): CheckerResult {
    for (const line of query.lines) {
      for (const token of line.tokens) {
        if (token[0] === Types.TableReference) {
          const database = token[1];
          if (!this.databases.includes(database) && database !== ";") {
            return new CheckerResult(line.num, sprintf(this.message, database));
          }
        }
      }
    }
 
    return new CheckerResult(0, "");
  }
}
 
export { DatabaseNotFound };