All files / src/checker checkerRunner.ts

20.93% Statements 9/43
4.17% Branches 1/24
37.5% Functions 3/8
20.93% Lines 9/43
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      1x 1x 1x                                         1x   1x                                                                                                                               2x       2x       1x                 1x  
import { Query } from "../reader/query";
import { Database } from "../database";
import { Printer } from "../printer";
import { Keyword } from "../lexer/tokens";
import { categorise, tokenise } from "../lexer/lexer";
import {
  MySqlError,
  MissingWhere,
  OddCodePoint,
  DatabaseNotFound,
  InvalidCreateOption,
  InvalidDropOption
} from "../barrel/checks";
 
/**
 * Runs all the checks.
 */
class CheckerRunner {
  /**
   * Simple checks are ones that don't require a database connection
   */
  public runSimpleChecks(
    sqlQueries: Query[],
    printer: Printer,
    prefix: string
  ) {
    const checks = this.getSqlLintChecks();
 
    sqlQueries.forEach((query: any) => {
      const content = query.getContent().trim();
 
      if (content) {
        const category = categorise(content);
        const tokenised: Query = tokenise(query);
 
        if (category === Keyword.Select) {
          printer.printCheck(checks.oddCodePoint, tokenised, prefix);
        } else if (category === Keyword.Delete) {
          printer.printCheck(checks.missingWhere, tokenised, prefix);
        } else if (category === Keyword.Drop) {
          printer.printCheck(checks.invalidDropOption, tokenised, prefix);
        } else if (category === Keyword.Create) {
          printer.printCheck(checks.invalidCreateOption, tokenised, prefix);
        }
      }
    });
  }
 
  public runDatabaseChecks(
    sqlQueries: Query[],
    database: Database,
    printer: Printer,
    prefix: string
  ) {
    const checks = this.getSqlLintChecks();
 
    sqlQueries.forEach((query: any) => {
      const content = query.getContent().trim();
 
      if (content) {
        const category = categorise(content);
        const tokenised: Query = tokenise(query);
 
        database.lintQuery(database.connection, content, (results: any) => {
          const checker = new MySqlError(results);
          printer.printCheck(checker, tokenised, prefix);
        });
 
        if (category === Keyword.Select) {
          printer.printCheck(checks.oddCodePoint, tokenised, prefix);
        } else if (category === Keyword.Use) {
          database.getDatabases(database.connection, (results: any) => {
            const checker = new DatabaseNotFound(results);
            printer.printCheck(checker, tokenised, prefix);
          });
        } else if (category === Keyword.Delete) {
          printer.printCheck(checks.missingWhere, tokenised, prefix);
        } else if (category === Keyword.Drop) {
          printer.printCheck(checks.invalidDropOption, tokenised, prefix);
        } else if (category === Keyword.Create) {
          printer.printCheck(checks.invalidCreateOption, tokenised, prefix);
        }
      }
    });
  }
 
  public run(
    sqlQueries: Query[],
    printer: Printer,
    prefix: string,
    database?: Database
  ) {
    Iif (database) {
      return this.runDatabaseChecks(sqlQueries, database, printer, prefix);
    }
 
    return this.runSimpleChecks(sqlQueries, printer, prefix);
  }
 
  private getSqlLintChecks() {
    return {
      oddCodePoint: new OddCodePoint(),
      missingWhere: new MissingWhere(),
      invalidDropOption: new InvalidDropOption(),
      invalidCreateOption: new InvalidCreateOption()
    };
  }
}
 
export { CheckerRunner };