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

100% Statements 11/11
100% Branches 2/2
100% Functions 3/3
100% Lines 11/11
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  2x               2x     2x       2x   2x 1x 1x 1x     1x       1x       2x  
import { Query } from "../../../reader/query";
import { CheckerResult } from "../../checkerResult";
import { IChecker } from "../../interface";
 
class MySqlError implements IChecker {
  public errors: any;
 
  // Note that we don't follow the interface correctly for MySQL Error
  // since the error message is dynamically generated.
  public message = "";
 
  constructor(errors: any) {
    this.errors = errors;
  }
 
  public check(query: Query): CheckerResult {
    const allowedCategories = ["select", "insert", "replace", "update"];
 
    if (allowedCategories.includes(query.category)) {
      const lineNumber = query.lines[0].num;
      const message = this.concatErrorObject(this.errors);
      return new CheckerResult(lineNumber, message);
    }
 
    return new CheckerResult(0, "");
  }
 
  private concatErrorObject(error: any) {
    return `[${error.code}] ${error.sqlMessage}`;
  }
}
 
export { MySqlError };