diff --git a/README.MD b/README.MD index 3d01afc1..73d6dacb 100644 --- a/README.MD +++ b/README.MD @@ -76,7 +76,7 @@ Below is the list that have direct support for `sql-lint` either through plugins ## Checks -A quick rundown of the checks is below but you should [read the documentation](https://sql-lint.readthedocs.io/en/latest/files/checks.html) +A quick rundown of the checks is below but you should [read the documentation](https://sql-lint.readthedocs.io/en/latest/files/checks.html) for an exhaustive list. `sql-lint` comes with its own suite of checks. It diff --git a/docs/files/configuration.md b/docs/files/configuration.md index 95a626a2..b898285e 100644 --- a/docs/files/configuration.md +++ b/docs/files/configuration.md @@ -33,7 +33,8 @@ You should put the following in there for more intelligent errors to come throug "host": "localhost", "user": "root", "password": "hunter2", - "port": 3306 + "port": 3306, + "database": "your_database" } ``` @@ -68,6 +69,12 @@ The port to connect to. Optional, default is `3306`. +### `database` + +The database to connect to. + +Optional, default is no database, where you will have to specify the database in your queries. + ### `ignore-errors` Don't want to be warned about a particular error? @@ -115,6 +122,8 @@ The below configuration contains every option available. "host": "localhost", "user": "root", "password": "password", + "port": 3306, + "database": "your_database", "ignore-errors": [ "odd-code-point", "missing-where", diff --git a/docs/files/development.md b/docs/files/development.md index 75bdd1ed..fd535477 100644 --- a/docs/files/development.md +++ b/docs/files/development.md @@ -93,6 +93,9 @@ Change your config file in `~/.config/sql-lint/config.json` to have the followin } ``` +Optionally, you can also add `"database": "your_database_name"` if you want to connect +to a specific database. However, most tests were written without this option. + ## This documentation This documentation is built on `sphinx` and `readthedocs`. To run it locally, diff --git a/src/checker/checkerRunner.ts b/src/checker/checkerRunner.ts index b44767ac..ea59b2d9 100644 --- a/src/checker/checkerRunner.ts +++ b/src/checker/checkerRunner.ts @@ -66,7 +66,6 @@ class CheckerRunner { printer.warnAboutUncategoriseableQuery(content, tokenised, prefix); } - for (const check of checks) { const checker = factory.build(check); diff --git a/src/cli.ts b/src/cli.ts index 368b2d21..8ed3b10f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -37,6 +37,7 @@ import databaseFactory from "./database/databaseFactory"; .option("--host ", "The host for the database connection") .option("--user ", "The user for the database connection") .option("--password ", "The password for the database connection") + .option("--database ", "The database for the database connection") .option("--port ", "The port for the database connection") .option("--config ", "The path to the configuration file") .option("--ignore-errors ", "The errors to ignore (comma separated)") @@ -113,7 +114,8 @@ import databaseFactory from "./database/databaseFactory"; program.host || configuration?.host || "localhost", program.user || configuration?.user || "root", // bad practice but unfortunately common, make it easier for the user program.password || configuration?.password, - program.port || configuration?.port || undefined // let mysql2 or pg figure out the default port + program.port || configuration?.port || undefined, // let mysql2 or pg figure out the default port + program.database || configuration?.database || undefined, ); } diff --git a/src/database/databaseFactory.ts b/src/database/databaseFactory.ts index 6c36956e..4933efb7 100644 --- a/src/database/databaseFactory.ts +++ b/src/database/databaseFactory.ts @@ -7,13 +7,14 @@ export default function databaseFactory( host: string, user: string, password: string, - port?: number + port?: number, + database?: string ): IDatabase { switch (driver) { case "mysql": - return new MySqlDatabase(host, user, password, port); + return new MySqlDatabase(host, user, password, port, database); case "postgres": - return new PostgresDatabase(host, user, password, port); + return new PostgresDatabase(host, user, password, port, database); default: throw new Error(`${driver} driver is unsupported`); } diff --git a/src/database/mySqlDatabase.ts b/src/database/mySqlDatabase.ts index 65436637..554d5fb0 100644 --- a/src/database/mySqlDatabase.ts +++ b/src/database/mySqlDatabase.ts @@ -4,12 +4,19 @@ import IDatabase, { sqlError } from "./interface"; export default class MySqlDatabase implements IDatabase { private connection: mysql.Connection; - constructor(host: string, user: string, password: string, port?: number) { + constructor( + host: string, + user: string, + password: string, + port?: number, + database?: string + ) { this.connection = mysql.createConnection({ host, user, password, port, + database, }); } diff --git a/src/database/postgresDatabase.ts b/src/database/postgresDatabase.ts index 335b204a..1a87e9a4 100644 --- a/src/database/postgresDatabase.ts +++ b/src/database/postgresDatabase.ts @@ -4,12 +4,19 @@ import IDatabase, { sqlError } from "./interface"; export default class PostgresDatabase implements IDatabase { private pool: Pool; - constructor(host: string, user: string, password: string, port?: number) { + constructor( + host: string, + user: string, + password: string, + port?: number, + database?: string + ) { this.pool = new Pool({ host, user, password, port, + database, }); } diff --git a/src/main.ts b/src/main.ts index 8610c106..63d8f985 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,6 +11,7 @@ interface Parameters { host?: string; user?: string; port?: number; + database?: string; driver?: string; prefix?: string; password?: string; @@ -21,41 +22,26 @@ export default async ({ sql, host, port, - user = '', - prefix = '', - password = '', + database, + user = "", + prefix = "", + password = "", verbosity = 0, - driver = 'mysql', + driver = "mysql", }: Parameters): Promise => { - const printer = new Printer( - verbosity, - new JsonFormat(), - ); + const printer = new Printer(verbosity, new JsonFormat()); - let db: IDatabase|undefined; + let db: IDatabase | undefined; if (host) { - db = databaseFactory( - driver, - host, - user, - password, - port, - ) + db = databaseFactory(driver, host, user, password, port, database); } const runner = new CheckerRunner(); - await runner.run( - putContentIntoLines(sql), - printer, - prefix, - [], - driver, - db, - ) + await runner.run(putContentIntoLines(sql), printer, prefix, [], driver, db); if (db) { db.end(); } return printer.messages; -} +}; diff --git a/src/reader/reader.ts b/src/reader/reader.ts index 8eaa4366..9ed04d74 100644 --- a/src/reader/reader.ts +++ b/src/reader/reader.ts @@ -31,7 +31,7 @@ export function putContentIntoLines(contents: string): Query[] { currentQueryContent += char; continue; } - + if (escape) { escape = false; currentQueryContent += char; @@ -39,7 +39,7 @@ export function putContentIntoLines(contents: string): Query[] { } // Toggle string state - if (char === "'" || char === "\"") { + if (char === "'" || char === '"') { if (currentQuote === null) { currentQuote = char; } else if (currentQuote === char) { diff --git a/test/integration/sql-lint.test.ts b/test/integration/sql-lint.test.ts index 67eb7335..262461af 100644 --- a/test/integration/sql-lint.test.ts +++ b/test/integration/sql-lint.test.ts @@ -52,19 +52,15 @@ test("it brings back a version number", (done) => { shelltest().cmd(`${sqlLint} --version`).expect("stdout", /^\d/).end(done); }); -test("--port is a valid option", (done) => { - shelltest() - .cmd(`${sqlLint} --help`) - .expect("stdout", /.*--port.*/) - .end(done); -}); - -test("--config is a valid option", (done) => { - shelltest() - .cmd(`${sqlLint} --help`) - .expect("stdout", /.*--config.*/) - .end(done); -}); +test.each(["--port", "--config", "--database"])( + "%s is a valid option", + (option) => { + shelltest() + .cmd(`${sqlLint} --help`) + .expect("stdout", new RegExp(`.*${option}.*`)) + .end(); + } +); test("Good queries exit with 0", (done) => { shelltest() diff --git a/test/unit/main.test.ts b/test/unit/main.test.ts index 9609c27c..3c0353c5 100644 --- a/test/unit/main.test.ts +++ b/test/unit/main.test.ts @@ -26,7 +26,7 @@ test.each([ error: "[sql-lint: trailing-whitespace] Trailing whitespace", }, ], -])("it can run programmatically", async (sql, expected) => { +])("it can run programmatically (%s)", async (sql, expected) => { const errors = await sqlLint({ sql: sql }); expect(errors[0]).toMatchObject(expected); });