Skip to content

Commit

Permalink
Add Programmatic Access (#189)
Browse files Browse the repository at this point in the history
* Add package-lock.json

* Rename main.ts to cli.ts

* Add main.ts

* Fix linter error

* Add test for package entry point

* Add package entry point

* Update bin path

* Change path to cli.js

* Update json test and expect object to be returned

* Update bin path and do not require executable permission

* Expect sqlLint to return array of type IMessage

* Make printer storage messages for later

* Return messages from printer

* Add interface for messages

* Check if printer has json messages and print them

* Add more test cases

* Remove unused interface

* Add IMessage to getMessage return type

* Add missing semicolons

* Make everything async

* Add entry point to readme

* Add install to readme

* Update docs with programmatic access

* Add promise syntax to readme and docs

* Add test case for postgres

* Make cli.js executable after compile
  • Loading branch information
lewissteele authored Jun 6, 2021
1 parent 63823d6 commit b934099
Show file tree
Hide file tree
Showing 81 changed files with 23,360 additions and 648 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
tags
node_modules/
package-lock.json
coverage/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ node_js:

before_script:
- npm run build
- chmod a+x ./dist/src/main.js
- chmod a+x ./dist/src/cli.js
66 changes: 65 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ A complete list of the checks it does is below.
## Installation

```
npm install -g sql-lint
npm i -g sql-lint
# or
yarn global add sql-lint
```

Or download a [binary](https://github.com/joereynolds/sql-lint/releases)
Expand All @@ -35,6 +37,68 @@ sql-lint create-person.sql
echo 'DELETE FROM person;' | sql-lint
```

## Programmatic Access

```
npm i sql-lint
# or
yarn add sql-lint
```

```
import sqlLint from 'sql-lint'
// using async/await
const errors = await sqlLint({
sql: 'SELECT my_column FROM my_table',
})
// or using promise
sqlLint({ sql: 'SELECT my_column FROM my_table' }).then(errors => {
for (const error of errors) {
// do something
}
})
```

### Parameters
sql-lint accepts an object using the following interface as its only argument
```
{
sql: string
host?: string
user?: string
port?: number
driver?: string
prefix?: string
password?: string
verbosity?: number
}
```

### Notes on some of the parameters
`sql`: can have multiple queries separated by `;`

`host`: if host is not provided `sql-lint` will only perform checks that do not require a connection

`driver`: defaults to mysql

`port`: if port is not provided it will use the default port for the driver you are using

### Output
sql-lint returns an `array` of objects with the following shape

```
{
line: number
error: string
source: string
additionalInformation: string
}
```

## Editor Integration

If your editor supports external tools, then it supports `sql-lint`.
Expand Down
2 changes: 1 addition & 1 deletion build/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rm -rf ./dist
npm run build
chmod a+x ./dist/src/main.js
chmod a+x ./dist/src/cli.js
npm run format
npm run lint
npm run test
Expand Down
Binary file modified docs/_build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/_build/doctrees/files/checks.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/files/configuration.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/files/development.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/files/installation.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/files/introduction.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/files/troubleshooting.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/index.doctree
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/_build/html/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 7b67b1c498866434cfbb81bd7c64d4ae
config: ae2bda56fd4fd969466967466009a4c8
tags: 645f666f9bcd5a90fca523b33c5a78b7
44 changes: 43 additions & 1 deletion docs/_build/html/_sources/files/checks.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,50 @@ Shown when a `DELETE` statement is missing a `WHERE` clause.

#### Example output

```
test/test-files/test.sql:20 [sql-lint: missing-where] DELETE statement missing WHERE clause.
```

### invalid-drop-option

Shown when an invalid option is given to the `DROP` statement.

#### Example output

```
test/test-files/test.sql:22 [sql-lint: invalid-drop-option] Option 'thing' is not a valid option, must be one of '["database","event","function","index","logfile","procedure","schema","server","table","view","tablespace","trigger"]'.
```

### invalid-create-option

Shown when an invalid option is given to the `CREATE` statement.

#### Example output

```
:24 [sql-lint: invalid-create-option] Option 'test' is not a valid option, must be one of '["algorithm","database","definer","event","function","index","or","procedure","server","table","tablespace","temporary","trigger","user","unique","view"]'.
```

### invalid-truncate-option

Shown when an invalid option is given to the `TRUNCATE` statement.

#### Example output

```
test/test-files/test.sql:26 [sql-lint: invalid-truncate-option] Option 'something' is not a valid option, must be one of '["table"]'.
```

### invalid-alter-option

Shown when an invalid option is given to the `ALTER` statement.

#### Example output

```
test/test-files/test.sql:28 [sql-lint: invalid-alter-option] Option 'mlady' is not a valid option, must be one of '["column","online","offline","ignore","database","event","function","procedure","server","table","tablespace","view"]'.
```

### odd-code-point

Shown when there are unsupported/unusual* code points in your code.
Expand All @@ -60,7 +82,6 @@ realising.</small>
test/test-files//test.sql:30 [sql-lint: odd-code-point] Unexpected code point.
```


### invalid-limit-quantifier

Shown when you specify something other than a number to the `LIMIT` statement.
Expand All @@ -70,3 +91,24 @@ Shown when you specify something other than a number to the `LIMIT` statement.
```
test/test-files//test.sql:32 [sql-lint: invalid-limit-quantifier] Argument 'test' is not a valid quantifier for LIMIT clause.
```

### hungarian-notation

Shown when the string `sp_` or `tbl_` is present in the query.


#### Example output

```
test/test-files/test.sql:34 [sql-lint: hungarian-notation] Hungarian notation present in query
```

### trailing-whitespace

Shown when a query has trailing whitespace.

#### Example output

```
test/test-files/test.sql:34 [sql-lint: trailing-whitespace] Trailing whitespace
```
22 changes: 11 additions & 11 deletions docs/_build/html/_sources/files/configuration.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ ways:
## Via CLI

```
sql-lint --driver="mysql" --host="localhost" --user="root" --password="hunter2" --query="SELECT 1;"
sql-lint --driver="mysql" --host="localhost" --user="root" --password="hunter2"
```

## Via `config.json`

A configuration file for `sql-lint` can reside in
`~/.config/sql-lint/config.json`. It follows the [XDG Base Directory
Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).
Specifically, it uses `$HOME/.config`.
Specifically, it uses `$HOME/.config`. (You can specify a different path for the
config with the `--config` flag)

You should put the following in there for more intelligent errors to come through

Expand All @@ -38,10 +39,10 @@ below.

### `driver`

Optional, default is `mysql`.

The driver to be used to check for errors.
Accepted ones are `mysql` and `psql`.
Accepted ones are `mysql` and `postgres`.

Optional, default is `mysql`.

### `host`

Expand Down Expand Up @@ -101,7 +102,9 @@ The below configuration contains every option available.
"invalid-drop-option",
"invalid-create-option",
"invalid-truncate-option",
"invalid-alter-option"
"invalid-alter-option",
"hungarian-notation",
"trailing-whitespace"
]
}
```
Expand All @@ -119,15 +122,12 @@ doing. Stick it in your global `.gitignore` to be safe.

#### Ale

`sql-lint` can be integrated into (Neo)Vim if you add this
[patch](https://github.com/dense-analysis/ale/pull/2988/commits/07080066e49d68910dccc19e4d95167300fb9422)
to Ale.

`sql-lint` can be integrated into (Neo)Vim with [Ale](https://github.com/dense-analysis/ale/).

#### Vanilla

If you want to go without a plugin, the simplest option is to run the following:

```
:!sql-lint -f %
:!sql-lint %
```
8 changes: 4 additions & 4 deletions docs/_build/html/_sources/files/development.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ Anyway, here are the steps.
- All checks live here so we can import them all conveniently
- Import your check in `src/checker/checkFactory.ts`
- Add your check to the `checkMap` in `src/checker/checkFactory.ts`
- Increment the splice last two spliced numbers in `checkerRunner` otherwise it
will remove the wrong things. Gross I know and needs to be fixed
- Add it to the README.md so people know it's a thing
- Add it to `configuration.md`. This is an exhaustive list of the checks
- Add tests. The name of the test should match the name of the check
- Add it to `checks.md`, the main documentation for checks
- `npm run build` to compile the changes

## Troubleshooting

Expand All @@ -53,10 +53,10 @@ of `checks` is in `checkerRunner` **after** the `splice`ing.

## Testing the code

Testing requires sql-lint and auto-changelog to be installed.
Testing requires sql-lint to be installed.

```
npm install -g sql-lint auto-changelog
npm install -g sql-lint
./build/build.sh //This will run more than just the tests (recommended)
```

Expand Down
5 changes: 4 additions & 1 deletion docs/_build/html/_sources/files/installation.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ experience.

`sql-lint` supports Mac, Linux, and Windows.

If you prefer, you can do `npm install sql-lint`.
If you prefer, you can do `npm i -g sql-lint` or `yarn global add sql-lint` if you're using yarn.

## Programmatic Access

For programmatic access you'll want to instead install `sql-lint` into your node project with `npm i sql-lint` or `yarn add sql-lint`.
Loading

0 comments on commit b934099

Please sign in to comment.