From 3934c3ce71d493d903d69f9750bfeb4900e02ad4 Mon Sep 17 00:00:00 2001 From: Joe Reynolds Date: Sun, 6 Jun 2021 11:03:15 +0100 Subject: [PATCH] Bump to v0.0.18 --- dist/package.json | 7 +- dist/src/barrel/checks.js | 5 +- dist/src/barrel/checks.js.map | 2 +- dist/src/checker/checkFactory.js | 3 +- dist/src/checker/checkFactory.js.map | 2 +- dist/src/checker/checkerRunner.js | 17 ++-- dist/src/checker/checkerRunner.js.map | 2 +- dist/src/checker/checks/generic/sqlError.js | 27 +++++ .../checker/checks/generic/sqlError.js.map | 1 + dist/src/checker/checks/mysql/mySqlError.js | 24 +---- .../checker/checks/mysql/mySqlError.js.map | 2 +- .../checker/checks/postgres/postgresError.js | 8 ++ .../checks/postgres/postgresError.js.map | 1 + dist/src/cli.js | 98 ++++++++++++++++++ dist/src/cli.js.map | 1 + dist/src/database/databaseFactory.js | 4 +- dist/src/database/mySqlDatabase.js | 12 ++- dist/src/database/mySqlDatabase.js.map | 2 +- dist/src/database/postgresDatabase.js | 18 ++-- dist/src/database/postgresDatabase.js.map | 2 +- dist/src/formatter/formats/json.js | 2 +- dist/src/formatter/formats/json.js.map | 2 +- dist/src/lexer/statements/postgres/create.js | 1 + .../lexer/statements/postgres/create.js.map | 2 +- dist/src/main.js | 99 +++---------------- dist/src/main.js.map | 2 +- dist/src/printer.js | 8 +- dist/src/printer.js.map | 2 +- package.json | 2 +- src/checker/checkFactory.ts | 2 - src/database/interface.ts | 2 +- src/database/mySqlDatabase.ts | 6 +- src/database/postgresDatabase.ts | 6 +- src/formatter/interface.ts | 6 +- test/unit/main.test.ts | 32 +++--- 35 files changed, 235 insertions(+), 177 deletions(-) create mode 100644 dist/src/checker/checks/generic/sqlError.js create mode 100644 dist/src/checker/checks/generic/sqlError.js.map create mode 100644 dist/src/checker/checks/postgres/postgresError.js create mode 100644 dist/src/checker/checks/postgres/postgresError.js.map create mode 100755 dist/src/cli.js create mode 100644 dist/src/cli.js.map mode change 100755 => 100644 dist/src/main.js diff --git a/dist/package.json b/dist/package.json index baa20ff6..a17ad539 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "bin": { - "sql-lint": "./dist/src/main.js" + "sql-lint": "./dist/src/cli.js" }, "dependencies": { "@types/moo": "^0.5.3", @@ -30,6 +30,7 @@ "typescript": "^3.8.2" }, "homepage": "https://github.com/joereynolds/sql-lint", + "main": "./dist/src/main.js", "jest": { "coverageThreshold": { "global": { @@ -69,12 +70,12 @@ "url": "https://github.com/joereynolds/sql-lint.git" }, "scripts": { - "build": "tsc", + "build": "tsc && chmod +x dist/src/cli.js", "format": "prettier --write {src,test}/**/*.ts", "lint": "tslint --fix -c tslint.json -p tsconfig.json", "start": "./build/build.sh", "test": "jest test --coverage --silent", "nearley": "nearleyc" }, - "version": "0.0.17" + "version": "0.0.18" } diff --git a/dist/src/barrel/checks.js b/dist/src/barrel/checks.js index 18c91900..4c14cefa 100644 --- a/dist/src/barrel/checks.js +++ b/dist/src/barrel/checks.js @@ -16,13 +16,14 @@ __exportStar(require("../checker/checks/any/invalidLimitQuantifier"), exports); __exportStar(require("../checker/checks/any/missingWhere"), exports); __exportStar(require("../checker/checks/any/oddCodePoint"), exports); __exportStar(require("../checker/checks/any/tableNotFound"), exports); -__exportStar(require("../checker/checks/any/unmatchedParentheses"), exports); __exportStar(require("../checker/checks/any/trailingWhitespace"), exports); +__exportStar(require("../checker/checks/any/unmatchedParentheses"), exports); +__exportStar(require("../checker/checks/mysql/mySqlError"), exports); __exportStar(require("../checker/checks/mysql/mySqlInvalidAlterOption"), exports); __exportStar(require("../checker/checks/mysql/mySqlInvalidCreateOption"), exports); __exportStar(require("../checker/checks/mysql/mySqlInvalidDropOption"), exports); __exportStar(require("../checker/checks/mysql/mySqlInvalidTruncateOption"), exports); -__exportStar(require("../checker/checks/mysql/mySqlError"), exports); +__exportStar(require("../checker/checks/postgres/postgresError"), exports); __exportStar(require("../checker/checks/postgres/postgresInvalidAlterOption"), exports); __exportStar(require("../checker/checks/postgres/postgresInvalidCreateOption"), exports); __exportStar(require("../checker/checks/postgres/postgresInvalidDropOption"), exports); diff --git a/dist/src/barrel/checks.js.map b/dist/src/barrel/checks.js.map index 142e7b9a..493c5fad 100644 --- a/dist/src/barrel/checks.js.map +++ b/dist/src/barrel/checks.js.map @@ -1 +1 @@ -{"version":3,"file":"checks.js","sourceRoot":"","sources":["../../../src/barrel/checks.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yEAAuD;AACvD,0EAAwD;AACxD,+EAA6D;AAC7D,qEAAmD;AACnD,qEAAmD;AACnD,sEAAoD;AACpD,6EAA2D;AAC3D,2EAAyD;AAEzD,kFAAgE;AAChE,mFAAiE;AACjE,iFAA+D;AAC/D,qFAAmE;AACnE,qEAAmD;AAEnD,wFAAsE;AACtE,yFAAuE;AACvE,uFAAqE;AACrE,2FAAyE"} \ No newline at end of file +{"version":3,"file":"checks.js","sourceRoot":"","sources":["../../../src/barrel/checks.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yEAAuD;AACvD,0EAAwD;AACxD,+EAA6D;AAC7D,qEAAmD;AACnD,qEAAmD;AACnD,sEAAoD;AACpD,2EAAyD;AACzD,6EAA2D;AAE3D,qEAAmD;AACnD,kFAAgE;AAChE,mFAAiE;AACjE,iFAA+D;AAC/D,qFAAmE;AAEnE,2EAAyD;AACzD,wFAAsE;AACtE,yFAAuE;AACvE,uFAAqE;AACrE,2FAAyE"} \ No newline at end of file diff --git a/dist/src/checker/checkFactory.js b/dist/src/checker/checkFactory.js index f302a00c..b520280b 100644 --- a/dist/src/checker/checkFactory.js +++ b/dist/src/checker/checkFactory.js @@ -10,14 +10,15 @@ class CheckFactory { hungarianNotation: checks_1.HungarianNotation, invalidLimitQuantifier: checks_1.InvalidLimitQuantifier, missingWhere: checks_1.MissingWhere, - mySqlError: checks_1.MySqlError, oddCodePoint: checks_1.OddCodePoint, trailingWhitespace: checks_1.TrailingWhitespace, unmatchedParentheses: checks_1.UnmatchedParentheses, + mySqlError: checks_1.MySqlError, mySqlInvalidAlterOption: checks_1.MySqlInvalidAlterOption, mySqlInvalidCreateOption: checks_1.MySqlInvalidCreateOption, mySqlInvalidDropOption: checks_1.MySqlInvalidDropOption, mySqlInvalidTruncateOption: checks_1.MySqlInvalidTruncateOption, + postgresError: checks_1.PostgresError, postgresInvalidAlterOption: checks_1.PostgresInvalidAlterOption, postgresInvalidCreateOption: checks_1.PostgresInvalidCreateOption, postgresInvalidDropOption: checks_1.PostgresInvalidDropOption, diff --git a/dist/src/checker/checkFactory.js.map b/dist/src/checker/checkFactory.js.map index 798acefa..51afd921 100644 --- a/dist/src/checker/checkFactory.js.map +++ b/dist/src/checker/checkFactory.js.map @@ -1 +1 @@ -{"version":3,"file":"checkFactory.js","sourceRoot":"","sources":["../../../src/checker/checkFactory.ts"],"names":[],"mappings":";;;AACA,6CAiB0B;AAE1B,MAAM,YAAY;IACT,KAAK,CAAC,KAAa;QACxB,2BAA2B;QAC3B,MAAM,QAAQ,GAA2B;YACvC,gBAAgB,EAAE,yBAAgB;YAClC,iBAAiB,EAAE,0BAAiB;YACpC,sBAAsB,EAAE,+BAAsB;YAC9C,YAAY,EAAE,qBAAY;YAC1B,UAAU,EAAE,mBAAU;YACtB,YAAY,EAAE,qBAAY;YAC1B,kBAAkB,EAAE,2BAAkB;YACtC,oBAAoB,EAAE,6BAAoB;YAE1C,uBAAuB,EAAE,gCAAuB;YAChD,wBAAwB,EAAE,iCAAwB;YAClD,sBAAsB,EAAE,+BAAsB;YAC9C,0BAA0B,EAAE,mCAA0B;YAEtD,0BAA0B,EAAE,mCAA0B;YACtD,2BAA2B,EAAE,oCAA2B;YACxD,yBAAyB,EAAE,kCAAyB;YACpD,6BAA6B,EAAE,sCAA6B;SAC7D,CAAC;QAEF,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;IAC/B,CAAC;CACF;AAEQ,oCAAY"} \ No newline at end of file +{"version":3,"file":"checkFactory.js","sourceRoot":"","sources":["../../../src/checker/checkFactory.ts"],"names":[],"mappings":";;;AACA,6CAoB0B;AAE1B,MAAM,YAAY;IACT,KAAK,CAAC,KAAa;QACxB,2BAA2B;QAC3B,MAAM,QAAQ,GAA2B;YACvC,gBAAgB,EAAE,yBAAgB;YAClC,iBAAiB,EAAE,0BAAiB;YACpC,sBAAsB,EAAE,+BAAsB;YAC9C,YAAY,EAAE,qBAAY;YAC1B,YAAY,EAAE,qBAAY;YAC1B,kBAAkB,EAAE,2BAAkB;YACtC,oBAAoB,EAAE,6BAAoB;YAE1C,UAAU,EAAE,mBAAU;YACtB,uBAAuB,EAAE,gCAAuB;YAChD,wBAAwB,EAAE,iCAAwB;YAClD,sBAAsB,EAAE,+BAAsB;YAC9C,0BAA0B,EAAE,mCAA0B;YAEtD,aAAa,EAAE,sBAAa;YAC5B,0BAA0B,EAAE,mCAA0B;YACtD,2BAA2B,EAAE,oCAA2B;YACxD,yBAAyB,EAAE,kCAAyB;YACpD,6BAA6B,EAAE,sCAA6B;SAC7D,CAAC;QAEF,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;IAC/B,CAAC;CACF;AAEQ,oCAAY"} \ No newline at end of file diff --git a/dist/src/checker/checkerRunner.js b/dist/src/checker/checkerRunner.js index 3e55bab2..9a6ea8c3 100644 --- a/dist/src/checker/checkerRunner.js +++ b/dist/src/checker/checkerRunner.js @@ -10,7 +10,7 @@ const checks_1 = require("../barrel/checks"); * Runs all the checks. */ class CheckerRunner { - run(sqlQueries, printer, prefix, omittedErrors, driver, database) { + async run(sqlQueries, printer, prefix, omittedErrors, driver, database) { const checks = fs .readdirSync(`${__dirname}/checks/any`) .map((check) => { @@ -37,7 +37,7 @@ class CheckerRunner { }); checks.push(...driverSpecificChecks); const factory = new checkFactory_1.CheckFactory(); - sqlQueries.forEach((query) => { + for (const query of sqlQueries) { const content = query.getContent().trim(); if (content) { const category = lexer_1.categorise(content); @@ -45,7 +45,7 @@ class CheckerRunner { printer.warnAboutUncategoriseableQuery(content); } const tokenised = lexer_1.tokenise(query); - checks.forEach((check) => { + for (const check of checks) { const checker = factory.build(check); // Simple checks if (checker.appliesTo.includes(category) && @@ -56,14 +56,13 @@ class CheckerRunner { if (checker.requiresConnection && database && checker.appliesTo.includes(category)) { - database.lintQuery(content, (results) => { - const sqlChecker = new checks_1.MySqlError(results); - printer.printCheck(sqlChecker, tokenised, prefix); - }); + const results = await database.lintQuery(content); + const sqlChecker = new checks_1.MySqlError(results); + printer.printCheck(sqlChecker, tokenised, prefix); } - }); + } } - }); + } } } exports.CheckerRunner = CheckerRunner; diff --git a/dist/src/checker/checkerRunner.js.map b/dist/src/checker/checkerRunner.js.map index b19d2d74..6a7b6406 100644 --- a/dist/src/checker/checkerRunner.js.map +++ b/dist/src/checker/checkerRunner.js.map @@ -1 +1 @@ -{"version":3,"file":"checkerRunner.js","sourceRoot":"","sources":["../../../src/checker/checkerRunner.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAC7B,iDAA8C;AAI9C,0CAAsD;AACtD,6CAA8C;AAE9C;;GAEG;AACH,MAAM,aAAa;IACV,GAAG,CACR,UAAmB,EACnB,OAAgB,EAChB,MAAc,EACd,aAAuB,EACvB,MAAc,EACd,QAAoB;QAEpB,MAAM,MAAM,GAAG,EAAE;aACd,WAAW,CAAC,GAAG,SAAS,aAAa,CAAC;aACtC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YACb,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;QAChC,CAAC,CAAC;aACD,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;YACf,MAAM,aAAa,GAAG,CAAC,eAAe,EAAE,kBAAkB,CAAC,CAAC;YAE5D,gCAAgC;YAChC,yEAAyE;YACzE,sEAAsE;YACtE,yEAAyE;YACzE,+EAA+E;YAC/E,8EAA8E;YAC9E,+CAA+C;YAC/C,OAAO,CAAC,aAAa,CAAC,QAAQ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BAAmB;oBACnB,IACE,OAAO,CAAC,kBAAkB;wBAC1B,QAAQ;wBACR,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,EACpC;wBACA,QAAQ,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,OAAY,EAAE,EAAE;4BAC3C,MAAM,UAAU,GAAG,IAAI,mBAAU,CAAC,OAAO,CAAC,CAAC;4BAC3C,OAAO,CAAC,UAAU,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;wBACpD,CAAC,CAAC,CAAC;qBACJ;gBACH,CAAC,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAEQ,sCAAa"} \ No newline at end of file +{"version":3,"file":"checkerRunner.js","sourceRoot":"","sources":["../../../src/checker/checkerRunner.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAC7B,iDAA8C;AAI9C,0CAAsD;AACtD,6CAA8C;AAE9C;;GAEG;AACH,MAAM,aAAa;IACV,KAAK,CAAC,GAAG,CACd,UAAmB,EACnB,OAAgB,EAChB,MAAc,EACd,aAAuB,EACvB,MAAc,EACd,QAAoB;QAEpB,MAAM,MAAM,GAAG,EAAE;aACd,WAAW,CAAC,GAAG,SAAS,aAAa,CAAC;aACtC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YACb,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;QAChC,CAAC,CAAC;aACD,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;YACf,MAAM,aAAa,GAAG,CAAC,eAAe,EAAE,kBAAkB,CAAC,CAAC;YAE5D,gCAAgC;YAChC,yEAAyE;YACzE,sEAAsE;YACtE,yEAAyE;YACzE,+EAA+E;YAC/E,8EAA8E;YAC9E,+CAA+C;YAC/C,OAAO,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KA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AC,QAAQ,CAAC,QAAQ,CAAC,EACpC;wBACA,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;wBAClD,MAAM,UAAU,GAAG,IAAI,mBAAU,CAAC,OAAO,CAAC,CAAC;wBAC3C,OAAO,CAAC,UAAU,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;qBACnD;iBACF;aACF;SACF;IACH,CAAC;CACF;AAEQ,sCAAa"} \ No newline at end of file diff --git a/dist/src/checker/checks/generic/sqlError.js b/dist/src/checker/checks/generic/sqlError.js new file mode 100644 index 00000000..41939ed0 --- /dev/null +++ b/dist/src/checker/checks/generic/sqlError.js @@ -0,0 +1,27 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const checkerResult_1 = require("../../checkerResult"); +class SqlError { + constructor(errors) { + this.requiresConnection = true; + // Note that we don't follow the interface correctly for RDBMS Errors + // since the error message is dynamically generated. + this.message = ""; + this.appliesTo = ["select", "create", "update", "drop", "insert"]; + this.additionalInformation = ""; + this.errors = errors; + } + check(query) { + if (this.appliesTo.includes(query.category)) { + const lineNumber = query.lines[0].num; + const message = this.concatErrorObject(this.errors); + return new checkerResult_1.CheckerResult(lineNumber, message); + } + return new checkerResult_1.CheckerResult(0, ""); + } + concatErrorObject(error) { + return `[${error.code}] ${error.sqlMessage}`; + } +} +exports.default = SqlError; +//# sourceMappingURL=sqlError.js.map \ No newline at end of file diff --git a/dist/src/checker/checks/generic/sqlError.js.map b/dist/src/checker/checks/generic/sqlError.js.map new file mode 100644 index 00000000..932af923 --- /dev/null +++ b/dist/src/checker/checks/generic/sqlError.js.map @@ -0,0 +1 @@ +{"version":3,"file":"sqlError.js","sourceRoot":"","sources":["../../../../../src/checker/checks/generic/sqlError.ts"],"names":[],"mappings":";;AACA,uDAAoD;AAGpD,MAAqB,QAAQ;IAW3B,YAAY,MAAW;QAThB,uBAAkB,GAAG,IAAI,CAAC;QAEjC,qEAAqE;QACrE,oDAAoD;QAC7C,YAAO,GAAG,EAAE,CAAC;QAEb,cAAS,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;QAC7D,0BAAqB,GAAG,EAAE,CAAC;QAGhC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,KAAY;QACvB,IAAI,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE;YAC3C,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;YAEtC,MAAM,OAAO,GAAG,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACpD,OAAO,IAAI,6BAAa,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;SAC/C;QAED,OAAO,IAAI,6BAAa,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAClC,CAAC;IAEO,iBAAiB,CAAC,KAAU;QAClC,OAAO,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,UAAU,EAAE,CAAC;IAC/C,CAAC;CACF;AA7BD,2BA6BC"} \ No newline at end of file diff --git a/dist/src/checker/checks/mysql/mySqlError.js b/dist/src/checker/checks/mysql/mySqlError.js index 7e73fa85..da90fd06 100644 --- a/dist/src/checker/checks/mysql/mySqlError.js +++ b/dist/src/checker/checks/mysql/mySqlError.js @@ -1,28 +1,8 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MySqlError = void 0; -const checkerResult_1 = require("../../checkerResult"); -class MySqlError { - constructor(errors) { - this.requiresConnection = true; - // Note that we don't follow the interface correctly for MySQL Error - // since the error message is dynamically generated. - this.message = ""; - this.appliesTo = ["select", "create", "update", "drop", "insert"]; - this.additionalInformation = ""; - this.errors = errors; - } - check(query) { - if (this.appliesTo.includes(query.category)) { - const lineNumber = query.lines[0].num; - const message = this.concatErrorObject(this.errors); - return new checkerResult_1.CheckerResult(lineNumber, message); - } - return new checkerResult_1.CheckerResult(0, ""); - } - concatErrorObject(error) { - return `[${error.code}] ${error.sqlMessage}`; - } +const sqlError_1 = require("../generic/sqlError"); +class MySqlError extends sqlError_1.default { } exports.MySqlError = MySqlError; //# sourceMappingURL=mySqlError.js.map \ No newline at end of file diff --git a/dist/src/checker/checks/mysql/mySqlError.js.map b/dist/src/checker/checks/mysql/mySqlError.js.map index 799d5e72..a942b726 100644 --- a/dist/src/checker/checks/mysql/mySqlError.js.map +++ b/dist/src/checker/checks/mysql/mySqlError.js.map @@ -1 +1 @@ -{"version":3,"file":"mySqlError.js","sourceRoot":"","sources":["../../../../../src/checker/checks/mysql/mySqlError.ts"],"names":[],"mappings":";;;AACA,uDAAoD;AAGpD,MAAM,UAAU;IAWd,YAAY,MAAW;QAThB,uBAAkB,GAAG,IAAI,CAAC;QAEjC,oEAAoE;QACpE,oDAAoD;QAC7C,YAAO,GAAG,EAAE,CAAC;QAEb,cAAS,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;QAC7D,0BAAqB,GAAG,EAAE,CAAC;QAGhC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,KAAY;QACvB,IAAI,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE;YAC3C,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;YAEtC,MAAM,OAAO,GAAG,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACpD,OAAO,IAAI,6BAAa,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;SAC/C;QAED,OAAO,IAAI,6BAAa,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAClC,CAAC;IAEO,iBAAiB,CAAC,KAAU;QAClC,OAAO,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,UAAU,EAAE,CAAC;IAC/C,CAAC;CACF;AAEQ,gCAAU"} \ No newline at end of file +{"version":3,"file":"mySqlError.js","sourceRoot":"","sources":["../../../../../src/checker/checks/mysql/mySqlError.ts"],"names":[],"mappings":";;;AAAA,kDAA2C;AAE3C,MAAa,UAAW,SAAQ,kBAAQ;CAAG;AAA3C,gCAA2C"} \ No newline at end of file diff --git a/dist/src/checker/checks/postgres/postgresError.js b/dist/src/checker/checks/postgres/postgresError.js new file mode 100644 index 00000000..ac35f80d --- /dev/null +++ b/dist/src/checker/checks/postgres/postgresError.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.PostgresError = void 0; +const sqlError_1 = require("../generic/sqlError"); +class PostgresError extends sqlError_1.default { +} +exports.PostgresError = PostgresError; +//# sourceMappingURL=postgresError.js.map \ No newline at end of file diff --git a/dist/src/checker/checks/postgres/postgresError.js.map b/dist/src/checker/checks/postgres/postgresError.js.map new file mode 100644 index 00000000..a3b7a232 --- /dev/null +++ b/dist/src/checker/checks/postgres/postgresError.js.map @@ -0,0 +1 @@ +{"version":3,"file":"postgresError.js","sourceRoot":"","sources":["../../../../../src/checker/checks/postgres/postgresError.ts"],"names":[],"mappings":";;;AAAA,kDAA2C;AAE3C,MAAa,aAAc,SAAQ,kBAAQ;CAAG;AAA9C,sCAA8C"} \ No newline at end of file diff --git a/dist/src/cli.js b/dist/src/cli.js new file mode 100755 index 00000000..8e38d55c --- /dev/null +++ b/dist/src/cli.js @@ -0,0 +1,98 @@ +#!/usr/bin/env node +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const program = require("commander"); +const fs = require("fs"); +const process = require("process"); +const checkerRunner_1 = require("./checker/checkerRunner"); +const config_1 = require("./config"); +const file_1 = require("./file"); +const formatterFactory_1 = require("./formatter/formatterFactory"); +const reader_1 = require("./reader/reader"); +const printer_1 = require("./printer"); +const package_json_1 = require("../package.json"); +const databaseFactory_1 = require("./database/databaseFactory"); +(async () => { + program + .version(package_json_1.version) + .option("--fix [string]", "The .sql string to fix") + .option("-d, --driver ", "The driver to use, must be one of ['mysql', 'postgres']") + .option("-v, --verbose", "Brings back information on the what it's linting and the tokens generated", increaseVerbosity, 0) + .option("--format ", "The format of the output, can be one of ['simple', 'json']", "simple") + .option("--host ", "The host for the connection") + .option("--user ", "The user for the connection") + .option("--password ", "The password for the connection") + .option("--port ", "The port for the connection") + .option("--config ", "The path to the configuration file") + .parse(process.argv); + let queries = []; + let prefix = ""; + const formatterFactory = new formatterFactory_1.FormatterFactory(); + const format = formatterFactory.build(program.format); + const printer = new printer_1.Printer(program.verbose, format); + const configuration = config_1.getConfiguration(program.config || config_1.file); + const runner = new checkerRunner_1.CheckerRunner(); + const programFile = program.args[0]; + if (program.fix) { + let query; + // Read from stdin if nothing is specified. + // We default to '-'' if no argument is supplied to --fix + // so we don't nag the user + if (typeof program.fix === "boolean") { + query = reader_1.getQueryFromLine(fs.readFileSync(0).toString()); + } + else { + query = reader_1.getQueryFromLine(program.fix); + } + printer.printFix(query); + process.exit(0); + } + if (programFile && !fs.existsSync(programFile)) { + printer.warnAboutFileNotFound(programFile); + process.exit(1); + } + // Read from stdin if no args are supplied + if (!programFile) { + queries = reader_1.getQueryFromLine(fs.readFileSync(0).toString()); + prefix = "stdin"; + } + let omittedErrors = []; + if (configuration !== null && "ignore-errors" in configuration) { + omittedErrors = configuration["ignore-errors"] || []; + } + let db; + if (configuration === null) { + printer.warnAboutNoConfiguration(config_1.file); + } + const driver = program.driver || (configuration === null || configuration === void 0 ? void 0 : configuration.driver) || "mysql"; + if (program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host)) { + db = databaseFactory_1.default(driver, program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host) || "localhost", program.user || (configuration === null || configuration === void 0 ? void 0 : configuration.user) || "root", // bad practice but unfortunately common, make it easier for the user + program.password || (configuration === null || configuration === void 0 ? void 0 : configuration.password), program.port || (configuration === null || configuration === void 0 ? void 0 : configuration.port) || undefined // let mysql2 or pg figure out the default port + ); + } + if (programFile) { + if (fs.lstatSync(programFile).isDirectory()) { + const sqlFiles = file_1.findByExtension(programFile, "sql"); + for (const sqlFile of sqlFiles) { + queries = reader_1.getQueryFromFile(sqlFile); + await runner.run(queries, printer, sqlFile, omittedErrors, driver, db); + } + } + else { + queries = reader_1.getQueryFromFile(programFile); + prefix = programFile; + } + } + await runner.run(queries, printer, prefix, omittedErrors, driver, db); + if (program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host)) { + db.end(); + } + if (printer.messages.length) { + console.log(JSON.stringify(printer.messages)); + process.exit(1); + } +})(); +function increaseVerbosity(v, total) { + return total + 1; +} +//# sourceMappingURL=cli.js.map \ No newline at end of file diff --git a/dist/src/cli.js.map b/dist/src/cli.js.map new file mode 100644 index 00000000..f5845c15 --- /dev/null +++ b/dist/src/cli.js.map @@ -0,0 +1 @@ +{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";;;AAEA,qCAAqC;AACrC,yBAAyB;AACzB,mCAAmC;AAEnC,2DAAwD;AACxD,qCAAkD;AAClD,iCAAyC;AACzC,mEAAgE;AAChE,4CAAqE;AACrE,uCAAoC;AAEpC,kDAA0C;AAC1C,gEAAyD;AAEzD,CAAC,KAAK,IAAI,EAAE;IACV,OAAO;SACJ,OAAO,CAAC,sBAAO,CAAC;SAChB,MAAM,CAAC,gBAAgB,EAAE,wBAAwB,CAAC;SAClD,MAAM,CACL,uBAAuB,EACvB,yDAAyD,CAC1D;SACA,MAAM,CACL,eAAe,EACf,2EAA2E,EAC3E,iBAAiB,EACjB,CAAC,CACF;SACA,MAAM,CACL,mBAAmB,EACnB,4DAA4D,EAC5D,QAAQ,CACT;SACA,MAAM,CAAC,iBAAiB,EAAE,6BAA6B,CAAC;SACxD,MAAM,CAAC,iBAAiB,EAAE,6BAA6B,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S,CAAC,WAAW,CAAC,CAAC,WAAW,EAAE,EAAE;YAC3C,MAAM,QAAQ,GAAG,sBAAe,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;YACrD,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;gBAC9B,OAAO,GAAG,yBAAgB,CAAC,OAAO,CAAC,CAAC;gBACpC,MAAM,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;aACxE;SACF;aAAM;YACL,OAAO,GAAG,yBAAgB,CAAC,WAAW,CAAC,CAAC;YACxC,MAAM,GAAG,WAAW,CAAC;SACtB;KACF;IAED,MAAM,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;IAEtE,IAAI,OAAO,CAAC,IAAI,KAAI,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,IAAI,CAAA,EAAE;QACvC,EAAE,CAAC,GAAG,EAAE,CAAC;KACV;IAED,IAAI,OAAO,CAAC,QAAQ,CAAC,MAAM,EAAE;QAC3B,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC9C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;AACH,CAAC,CAAC,EAAE,CAAC;AAEL,SAAS,iBAAiB,CAAC,CAAM,EAAE,KAAU;IAC3C,OAAO,KAAK,GAAG,CAAC,CAAC;AACnB,CAAC"} \ No newline at end of file diff --git a/dist/src/database/databaseFactory.js b/dist/src/database/databaseFactory.js index 6a78cc53..801199ff 100644 --- a/dist/src/database/databaseFactory.js +++ b/dist/src/database/databaseFactory.js @@ -4,9 +4,9 @@ const mySqlDatabase_1 = require("./mySqlDatabase"); const postgresDatabase_1 = require("./postgresDatabase"); function databaseFactory(driver, host, user, password, port) { switch (driver) { - case 'mysql': + case "mysql": return new mySqlDatabase_1.default(host, user, password, port); - case 'postgres': + case "postgres": return new postgresDatabase_1.default(host, user, password, port); default: throw new Error(`${driver} driver is unsupported`); diff --git a/dist/src/database/mySqlDatabase.js b/dist/src/database/mySqlDatabase.js index 82720906..5a62569b 100644 --- a/dist/src/database/mySqlDatabase.js +++ b/dist/src/database/mySqlDatabase.js @@ -10,11 +10,13 @@ class MySqlDatabase { port, }); } - lintQuery(query, callback) { - this.connection.query(`EXPLAIN ${query}`, err => { - if (err) { - callback(err); - } + lintQuery(query) { + return new Promise(resolve => { + this.connection.query(`EXPLAIN ${query}`, (err) => { + if (err) { + resolve(err); + } + }); }); } end() { diff --git a/dist/src/database/mySqlDatabase.js.map b/dist/src/database/mySqlDatabase.js.map index dc8e6e43..bdd5c3a6 100644 --- a/dist/src/database/mySqlDatabase.js.map +++ b/dist/src/database/mySqlDatabase.js.map @@ -1 +1 @@ -{"version":3,"file":"mySqlDatabase.js","sourceRoot":"","sources":["../../../src/database/mySqlDatabase.ts"],"names":[],"mappings":";;AAAA,gCAAgC;AAGhC,MAAqB,aAAa;IAGhC,YACE,IAAY,EACZ,IAAY,EACZ,QAAgB,EAChB,IAAa;QAEb,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,gBAAgB,CAAC;YACvC,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,IAAI;SACL,CAAC,CAAC;IACL,CAAC;IAEM,SAAS,CAAC,KAAa,EAAE,QAAa;QAC3C,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,WAAW,KAAK,EAAE,EAAE,GAAG,CAAC,EAAE;YAC9C,IAAI,GAAG,EAAE;gBACP,QAAQ,CAAC,GAAG,CAAC,CAAC;aACf;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,GAAG;QACR,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;IACxB,CAAC;CACF;AA5BD,gCA4BC"} \ No newline at end of file +{"version":3,"file":"mySqlDatabase.js","sourceRoot":"","sources":["../../../src/database/mySqlDatabase.ts"],"names":[],"mappings":";;AAAA,gCAAgC;AAGhC,MAAqB,aAAa;IAGhC,YAAY,IAAY,EAAE,IAAY,EAAE,QAAgB,EAAE,IAAa;QACrE,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,gBAAgB,CAAC;YACvC,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,IAAI;SACL,CAAC,CAAC;IACL,CAAC;IAEM,SAAS,CAAC,KAAa;QAC5B,OAAO,IAAI,OAAO,CAAgB,OAAO,CAAC,EAAE;YAC1C,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,WAAW,KAAK,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;gBAChD,IAAI,GAAG,EAAE;oBACP,OAAO,CAAC,GAA0B,CAAC,CAAC;iBACrC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,GAAG;QACR,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;IACxB,CAAC;CACF;AAzBD,gCAyBC"} \ No newline at end of file diff --git a/dist/src/database/postgresDatabase.js b/dist/src/database/postgresDatabase.js index fe12d40b..5866e14b 100644 --- a/dist/src/database/postgresDatabase.js +++ b/dist/src/database/postgresDatabase.js @@ -10,14 +10,16 @@ class PostgresDatabase { port, }); } - lintQuery(query, callback) { - this.pool.query(`EXPLAIN ${query}`, err => { - if (err) { - callback({ - code: err.name, - sqlMessage: err.message, - }); - } + async lintQuery(query) { + return new Promise(resolve => { + this.pool.query(`EXPLAIN ${query}`, err => { + if (err) { + resolve({ + code: err.name, + sqlMessage: err.message, + }); + } + }); }); } end() { diff --git a/dist/src/database/postgresDatabase.js.map b/dist/src/database/postgresDatabase.js.map index b70f01da..d8eeb6e7 100644 --- a/dist/src/database/postgresDatabase.js.map +++ b/dist/src/database/postgresDatabase.js.map @@ -1 +1 @@ -{"version":3,"file":"postgresDatabase.js","sourceRoot":"","sources":["../../../src/database/postgresDatabase.ts"],"names":[],"mappings":";;AAAA,2BAA0B;AAG1B,MAAqB,gBAAgB;IAGnC,YACE,IAAY,EACZ,IAAY,EACZ,QAAgB,EAChB,IAAa;QAEb,IAAI,CAAC,IAAI,GAAG,IAAI,SAAI,CAAC;YACnB,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,IAAI;SACL,CAAC,CAAC;IACL,CAAC;IAEM,SAAS,CAAC,KAAa,EAAE,QAAa;QAC3C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,KAAK,EAAE,EAAE,GAAG,CAAC,EAAE;YACxC,IAAI,GAAG,EAAE;gBACP,QAAQ,CAAC;oBACP,IAAI,EAAE,GAAG,CAAC,IAAI;oBACd,UAAU,EAAE,GAAG,CAAC,OAAO;iBACxB,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,GAAG;QACR,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;IAClB,CAAC;CACF;AA/BD,mCA+BC"} \ No newline at end of file +{"version":3,"file":"postgresDatabase.js","sourceRoot":"","sources":["../../../src/database/postgresDatabase.ts"],"names":[],"mappings":";;AAAA,2BAA0B;AAG1B,MAAqB,gBAAgB;IAGnC,YAAY,IAAY,EAAE,IAAY,EAAE,QAAgB,EAAE,IAAa;QACrE,IAAI,CAAC,IAAI,GAAG,IAAI,SAAI,CAAC;YACnB,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,IAAI;SACL,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,SAAS,CAAC,KAAa;QAClC,OAAO,IAAI,OAAO,CAAgB,OAAO,CAAC,EAAE;YAC1C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,KAAK,EAAE,EAAE,GAAG,CAAC,EAAE;gBACxC,IAAI,GAAG,EAAE;oBACP,OAAO,CAAC;wBACN,IAAI,EAAE,GAAG,CAAC,IAAI;wBACd,UAAU,EAAE,GAAG,CAAC,OAAO;qBACxB,CAAC,CAAC;iBACJ;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,GAAG;QACR,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;IAClB,CAAC;CACF;AA5BD,mCA4BC"} \ No newline at end of file diff --git a/dist/src/formatter/formats/json.js b/dist/src/formatter/formats/json.js index d92578b4..82018f0c 100644 --- a/dist/src/formatter/formats/json.js +++ b/dist/src/formatter/formats/json.js @@ -12,7 +12,7 @@ class JsonFormat { if (verbosity) { message.additionalInformation = result.additionalInformation; } - return JSON.stringify(message); + return message; } } exports.JsonFormat = JsonFormat; diff --git a/dist/src/formatter/formats/json.js.map b/dist/src/formatter/formats/json.js.map index 0eaaa91d..41a5f1e3 100644 --- a/dist/src/formatter/formats/json.js.map +++ b/dist/src/formatter/formats/json.js.map @@ -1 +1 @@ -{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../../src/formatter/formats/json.ts"],"names":[],"mappings":";;;AAGA,MAAM,UAAU;IACP,UAAU,CAAC,MAAc,EAAE,MAAqB,EAAE,SAAiB;QACxE,MAAM,OAAO,GAAG;YACd,MAAM,EAAE,MAAM;YACd,KAAK,EAAE,MAAM,CAAC,OAAO;YACrB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,qBAAqB,EAAE,EAAE;SAC1B,CAAC;QAEF,IAAI,SAAS,EAAE;YACb,OAAO,CAAC,qBAAqB,GAAG,MAAM,CAAC,qBAAqB,CAAC;SAC9D;QAED,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;CACF;AAEQ,gCAAU"} \ No newline at end of file +{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../../src/formatter/formats/json.ts"],"names":[],"mappings":";;;AAGA,MAAM,UAAU;IACP,UAAU,CAAC,MAAc,EAAE,MAAqB,EAAE,SAAiB;QACxE,MAAM,OAAO,GAAG;YACd,MAAM,EAAE,MAAM;YACd,KAAK,EAAE,MAAM,CAAC,OAAO;YACrB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,qBAAqB,EAAE,EAAE;SAC1B,CAAC;QAEF,IAAI,SAAS,EAAE;YACb,OAAO,CAAC,qBAAqB,GAAG,MAAM,CAAC,qBAAqB,CAAC;SAC9D;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AAEQ,gCAAU"} \ No newline at end of file diff --git a/dist/src/lexer/statements/postgres/create.js b/dist/src/lexer/statements/postgres/create.js index 1a28ea6f..23efbf07 100644 --- a/dist/src/lexer/statements/postgres/create.js +++ b/dist/src/lexer/statements/postgres/create.js @@ -12,6 +12,7 @@ class PostgresCreate { "database", "definer", "event", + "extension", "function", "index", "or", diff --git a/dist/src/lexer/statements/postgres/create.js.map b/dist/src/lexer/statements/postgres/create.js.map index 6c3ee515..61643aad 100644 --- a/dist/src/lexer/statements/postgres/create.js.map +++ b/dist/src/lexer/statements/postgres/create.js.map @@ -1 +1 @@ -{"version":3,"file":"create.js","sourceRoot":"","sources":["../../../../../src/lexer/statements/postgres/create.ts"],"names":[],"mappings":";;;AAEA,uCAAsD;AACtD,uDAAmD;AACnD,uCAAoC;AACpC,uCAAoC;AAEpC,MAAM,cAAc;IAApB;QACS,YAAO,GAAa;YACzB,WAAW;YACX,UAAU;YACV,SAAS;YACT,OAAO;YACP,UAAU;YACV,OAAO;YACP,IAAI;YACJ,WAAW;YACX,MAAM;YACN,QAAQ;YACR,QAAQ;YACR,OAAO;YACP,YAAY;YACZ,WAAW;YACX,SAAS;YACT,MAAM;YACN,MAAM;YACN,QAAQ;YACR,MAAM;SACP,CAAC;IAyBJ,CAAC;IAvBQ,QAAQ,CAAC,KAAY;QAC1B,IAAI,SAAS,GAAG,EAAE,CAAC;QAEnB,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;gBACvC,IAAI,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC;gBACrC,IAAI,IAAI,KAAK,kBAAO,CAAC,MAAM,EAAE;oBAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,aAAK,CAAC,aAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;iBAClD;qBAAM,IAAI,SAAS,KAAK,kBAAO,CAAC,MAAM,EAAE;oBACvC,IAAI,GAAG,+BAAuB,CAAC,IAAI,CAAC,CAAC;oBAErC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;wBACnB,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,IAAI,aAAK,CAAC,aAAK,CAAC,MAAM,EAAE,+BAAuB,CAAC,IAAI,CAAC,CAAC,CACvD,CAAC;qBACH;iBACF;gBACD,SAAS,GAAG,IAAI,CAAC;YACnB,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAEQ,wCAAc"} \ No newline at end of file +{"version":3,"file":"create.js","sourceRoot":"","sources":["../../../../../src/lexer/statements/postgres/create.ts"],"names":[],"mappings":";;;AAEA,uCAAsD;AACtD,uDAAmD;AACnD,uCAAoC;AACpC,uCAAoC;AAEpC,MAAM,cAAc;IAApB;QACS,YAAO,GAAa;YACzB,WAAW;YACX,UAAU;YACV,SAAS;YACT,OAAO;YACP,WAAW;YACX,UAAU;YACV,OAAO;YACP,IAAI;YACJ,WAAW;YACX,MAAM;YACN,QAAQ;YACR,QAAQ;YACR,OAAO;YACP,YAAY;YACZ,WAAW;YACX,SAAS;YACT,MAAM;YACN,MAAM;YACN,QAAQ;YACR,MAAM;SACP,CAAC;IAyBJ,CAAC;IAvBQ,QAAQ,CAAC,KAAY;QAC1B,IAAI,SAAS,GAAG,EAAE,CAAC;QAEnB,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;gBACvC,IAAI,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC;gBACrC,IAAI,IAAI,KAAK,kBAAO,CAAC,MAAM,EAAE;oBAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,aAAK,CAAC,aAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;iBAClD;qBAAM,IAAI,SAAS,KAAK,kBAAO,CAAC,MAAM,EAAE;oBACvC,IAAI,GAAG,+BAAuB,CAAC,IAAI,CAAC,CAAC;oBAErC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;wBACnB,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,IAAI,aAAK,CAAC,aAAK,CAAC,MAAM,EAAE,+BAAuB,CAAC,IAAI,CAAC,CAAC,CACvD,CAAC;qBACH;iBACF;gBACD,SAAS,GAAG,IAAI,CAAC;YACnB,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAEQ,wCAAc"} \ No newline at end of file diff --git a/dist/src/main.js b/dist/src/main.js old mode 100755 new mode 100644 index 2192455d..499ef398 --- a/dist/src/main.js +++ b/dist/src/main.js @@ -1,92 +1,21 @@ -#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -const program = require("commander"); -const fs = require("fs"); -const process = require("process"); -const checkerRunner_1 = require("./checker/checkerRunner"); -const databaseFactory_1 = require("./database/databaseFactory"); -const formatterFactory_1 = require("./formatter/formatterFactory"); const printer_1 = require("./printer"); -const config_1 = require("./config"); const reader_1 = require("./reader/reader"); -const package_json_1 = require("../package.json"); -const file_1 = require("./file"); -function increaseVerbosity(v, total) { - return total + 1; -} -program - .version(package_json_1.version) - .option("--fix [string]", "The .sql string to fix") - .option("-d, --driver ", "The driver to use, must be one of ['mysql', 'postgres']") - .option("-v, --verbose", "Brings back information on the what it's linting and the tokens generated", increaseVerbosity, 0) - .option("--format ", "The format of the output, can be one of ['simple', 'json']", "simple") - .option("--host ", "The host for the connection") - .option("--user ", "The user for the connection") - .option("--password ", "The password for the connection") - .option("--port ", "The port for the connection") - .option("--config ", "The path to the configuration file") - .parse(process.argv); -let queries = []; -let prefix = ""; -const formatterFactory = new formatterFactory_1.FormatterFactory(); -const format = formatterFactory.build(program.format); -const printer = new printer_1.Printer(program.verbose, format); -const configuration = config_1.getConfiguration(program.config || config_1.file); -const runner = new checkerRunner_1.CheckerRunner(); -const programFile = program.args[0]; -if (program.fix) { - let query; - // Read from stdin if nothing is specified. - // We default to '-'' if no argument is supplied to --fix - // so we don't nag the user - if (typeof program.fix === "boolean") { - query = reader_1.getQueryFromLine(fs.readFileSync(0).toString()); - } - else { - query = reader_1.getQueryFromLine(program.fix); - } - printer.printFix(query); - process.exit(0); -} -if (programFile && !fs.existsSync(programFile)) { - printer.warnAboutFileNotFound(programFile); - process.exit(1); -} -// Read from stdin if no args are supplied -if (!programFile) { - queries = reader_1.getQueryFromLine(fs.readFileSync(0).toString()); - prefix = "stdin"; -} -let omittedErrors = []; -if (configuration !== null && "ignore-errors" in configuration) { - omittedErrors = configuration["ignore-errors"] || []; -} -let db; -if (configuration === null) { - printer.warnAboutNoConfiguration(config_1.file); -} -const driver = program.driver || (configuration === null || configuration === void 0 ? void 0 : configuration.driver) || "mysql"; -if (program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host)) { - db = databaseFactory_1.default(driver, program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host) || "localhost", program.user || (configuration === null || configuration === void 0 ? void 0 : configuration.user) || "root", // bad practice but unfortunately common, make it easier for the user - program.password || (configuration === null || configuration === void 0 ? void 0 : configuration.password), program.port || (configuration === null || configuration === void 0 ? void 0 : configuration.port) || undefined // let mysql2 or pg figure out the default port - ); -} -if (programFile) { - if (fs.lstatSync(programFile).isDirectory()) { - const sqlFiles = file_1.findByExtension(programFile, "sql"); - sqlFiles.forEach((sqlFile) => { - queries = reader_1.getQueryFromFile(sqlFile); - runner.run(queries, printer, sqlFile, omittedErrors, driver, db); - }); +const json_1 = require("./formatter/formats/json"); +const checkerRunner_1 = require("./checker/checkerRunner"); +const databaseFactory_1 = require("./database/databaseFactory"); +exports.default = async ({ sql, host, port, user = '', prefix = '', password = '', verbosity = 0, driver = 'mysql', }) => { + const printer = new printer_1.Printer(verbosity, new json_1.JsonFormat()); + let db; + if (host) { + db = databaseFactory_1.default(driver, host, user, password, port); } - else { - queries = reader_1.getQueryFromFile(programFile); - prefix = programFile; + const runner = new checkerRunner_1.CheckerRunner(); + await runner.run(reader_1.putContentIntoLines(sql), printer, prefix, [], driver, db); + if (db) { + db.end(); } -} -runner.run(queries, printer, prefix, omittedErrors, driver, db); -if (program.host || (configuration === null || configuration === void 0 ? void 0 : configuration.host)) { - db.end(); -} + return printer.messages; +}; //# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/dist/src/main.js.map b/dist/src/main.js.map index 6ddee90a..1285b138 100644 --- a/dist/src/main.js.map +++ b/dist/src/main.js.map @@ -1 +1 @@ -{"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/main.ts"],"names":[],"mappings":";;;AAEA,qCAAqC;AACrC,yBAAyB;AACzB,mCAAmC;AAEnC,2DAAwD;AACxD,gEAAyD;AACzD,mEAAgE;AAChE,uCAAoC;AAEpC,qCAAkD;AAClD,4CAAqE;AACrE,kDAA0C;AAE1C,iCAAyC;AAEzC,SAAS,iBAAiB,CAAC,CAAM,EAAE,KAAU;IAC3C,OAAO,KAAK,GAAG,CAAC,CAAC;AACnB,CAAC;AAED,OAAO;KACJ,OAAO,CAAC,sBAAO,CAAC;KAChB,MAAM,CAAC,gBAAgB,EAAE,wBAAwB,CAAC;KAClD,MAAM,CACL,uBAAuB,EACvB,yDAAyD,CAC1D;KACA,MAAM,CACL,eAAe,E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MAAM,OAAO,GAAG,IAAI,iBAAO,CACzB,SAAS,EACT,IAAI,iBAAU,EAAE,CACjB,CAAC;IAEF,IAAI,EAAuB,CAAC;IAC5B,IAAI,IAAI,EAAE;QACR,EAAE,GAAG,yBAAe,CAClB,MAAM,EACN,IAAI,EACJ,IAAI,EACJ,QAAQ,EACR,IAAI,CACL,CAAA;KACF;IAED,MAAM,MAAM,GAAG,IAAI,6BAAa,EAAE,CAAC;IACnC,MAAM,MAAM,CAAC,GAAG,CACd,4BAAmB,CAAC,GAAG,CAAC,EACxB,OAAO,EACP,MAAM,EACN,EAAE,EACF,MAAM,EACN,EAAE,CACH,CAAA;IAED,IAAI,EAAE,EAAE;QACN,EAAE,CAAC,GAAG,EAAE,CAAC;KACV;IAED,OAAO,OAAO,CAAC,QAAQ,CAAC;AAC1B,CAAC,CAAA"} \ No newline at end of file diff --git a/dist/src/printer.js b/dist/src/printer.js index b333ec34..8876c2be 100644 --- a/dist/src/printer.js +++ b/dist/src/printer.js @@ -4,6 +4,7 @@ exports.Printer = void 0; const fixer_1 = require("./fixer"); class Printer { constructor(verbosity, format) { + this.messages = []; this.verbosity = verbosity; this.format = format; } @@ -25,7 +26,12 @@ class Printer { console.log(tokenisedForPrint); } if (result.content) { - console.log(this.format.getMessage(prefix, result, this.verbosity)); + const message = this.format.getMessage(prefix, result, this.verbosity); + if (typeof message !== "string") { + this.messages.push(message); + return; + } + console.log(message); // If there are any errors whatsoever, we want to exit // with 1 for build scripts and the like. process.exitCode = 1; diff --git a/dist/src/printer.js.map b/dist/src/printer.js.map index a8321234..54f1d06a 100644 --- a/dist/src/printer.js.map +++ b/dist/src/printer.js.map @@ -1 +1 @@ -{"version":3,"file":"printer.js","sourceRoot":"","sources":["../../src/printer.ts"],"names":[],"mappings":";;;AAGA,mCAAgC;AAEhC,MAAM,OAAO;IAIX,YAAY,SAAiB,EAAE,MAAe;QAC5C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,MA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b20..a17ad539 100644 --- a/package.json +++ b/package.json @@ -77,5 +77,5 @@ "test": "jest test --coverage --silent", "nearley": "nearleyc" }, - "version": "0.0.17" + "version": "0.0.18" } diff --git a/src/checker/checkFactory.ts b/src/checker/checkFactory.ts index 4feed11b..f7631762 100644 --- a/src/checker/checkFactory.ts +++ b/src/checker/checkFactory.ts @@ -7,13 +7,11 @@ import { OddCodePoint, TrailingWhitespace, UnmatchedParentheses, - MySqlError, MySqlInvalidAlterOption, MySqlInvalidCreateOption, MySqlInvalidDropOption, MySqlInvalidTruncateOption, - PostgresError, PostgresInvalidAlterOption, PostgresInvalidCreateOption, diff --git a/src/database/interface.ts b/src/database/interface.ts index cb77f615..2f42d55c 100644 --- a/src/database/interface.ts +++ b/src/database/interface.ts @@ -3,7 +3,7 @@ export default interface IDatabase { * Runs an EXPLAIN on the query. If it doesn't run successfully, errors will come through, * which is what we want. */ - lintQuery(query: string): Promise; + lintQuery(query: string): Promise; end(): void; } diff --git a/src/database/mySqlDatabase.ts b/src/database/mySqlDatabase.ts index e8228ff3..5699bb76 100644 --- a/src/database/mySqlDatabase.ts +++ b/src/database/mySqlDatabase.ts @@ -13,11 +13,11 @@ export default class MySqlDatabase implements IDatabase { }); } - public lintQuery(query: string): Promise { - return new Promise(resolve => { + public lintQuery(query: string): Promise { + return new Promise((resolve) => { this.connection.query(`EXPLAIN ${query}`, (err) => { if (err) { - resolve(err as unknown as sqlError); + resolve((err as unknown) as sqlError); } }); }); diff --git a/src/database/postgresDatabase.ts b/src/database/postgresDatabase.ts index 06e54591..d9878743 100644 --- a/src/database/postgresDatabase.ts +++ b/src/database/postgresDatabase.ts @@ -13,9 +13,9 @@ export default class PostgresDatabase implements IDatabase { }); } - public async lintQuery(query: string): Promise { - return new Promise(resolve => { - this.pool.query(`EXPLAIN ${query}`, err => { + public async lintQuery(query: string): Promise { + return new Promise((resolve) => { + this.pool.query(`EXPLAIN ${query}`, (err) => { if (err) { resolve({ code: err.name, diff --git a/src/formatter/interface.ts b/src/formatter/interface.ts index 89a55903..548cb34e 100644 --- a/src/formatter/interface.ts +++ b/src/formatter/interface.ts @@ -1,7 +1,11 @@ import { CheckerResult } from "../checker/checkerResult"; export interface IFormat { - getMessage(prefix: string, result: CheckerResult, verbosity: number): string|IMessage; + getMessage( + prefix: string, + result: CheckerResult, + verbosity: number + ): string | IMessage; } export interface IMessage { diff --git a/test/unit/main.test.ts b/test/unit/main.test.ts index cfbfad05..9609c27c 100644 --- a/test/unit/main.test.ts +++ b/test/unit/main.test.ts @@ -5,15 +5,16 @@ test.each([ "DELETE FROM some_table;", { line: 1, - error: '[sql-lint: missing-where] DELETE statement missing WHERE clause.', - } + error: "[sql-lint: missing-where] DELETE statement missing WHERE clause.", + }, ], [ "SELECT some_column FROM tbl_table;", { line: 1, - error: '[sql-lint: hungarian-notation] Hungarian notation present in query', - } + error: + "[sql-lint: hungarian-notation] Hungarian notation present in query", + }, ], [ ` @@ -22,16 +23,13 @@ test.each([ `, { line: 2, - error: '[sql-lint: trailing-whitespace] Trailing whitespace', - } + error: "[sql-lint: trailing-whitespace] Trailing whitespace", + }, ], -])( - "it can run programmatically", - async (sql, expected) => { - const errors = await sqlLint({ sql: sql }); - expect(errors[0]).toMatchObject(expected); - }, -); +])("it can run programmatically", async (sql, expected) => { + const errors = await sqlLint({ sql: sql }); + expect(errors[0]).toMatchObject(expected); +}); test("it can return multiple errors", async () => { const sql = ` @@ -42,17 +40,17 @@ test("it can return multiple errors", async () => { `; const errors = await sqlLint({ sql: sql }); expect(errors).toHaveLength(3); - expect(errors[0]).toMatchObject( { + expect(errors[0]).toMatchObject({ line: 2, - error: '[sql-lint: missing-where] DELETE statement missing WHERE clause.', + error: "[sql-lint: missing-where] DELETE statement missing WHERE clause.", }); expect(errors[1]).toMatchObject({ line: 3, - error: '[sql-lint: hungarian-notation] Hungarian notation present in query', + error: "[sql-lint: hungarian-notation] Hungarian notation present in query", }); expect(errors[2]).toMatchObject({ line: 4, - error: '[sql-lint: trailing-whitespace] Trailing whitespace', + error: "[sql-lint: trailing-whitespace] Trailing whitespace", }); });