22
33** SQL Formatter** is a JavaScript library for pretty-printing SQL queries.
44It started as a port of a [ PHP Library] [ ] , but has since considerably diverged.
5- It supports [ Standard SQL] [ ] , [ Couchbase N1QL] [ ] , [ IBM DB2] [ ] and [ Oracle PL/SQL] [ ] dialects.
5+
6+ SQL Formatter supports [ Standard SQL] [ ] , [ Couchbase N1QL] [ ] , [ IBM DB2] [ ] ,
7+ [ Oracle PL/SQL] [ ] , [ Amazon Redshift] [ ] , and [ Spark] [ ] dialects.
68
79&rarr ; [ Try the demo.] ( https://zeroturnaround.github.io/sql-formatter/ )
810
911## Install
1012
1113Get the latest version from NPM:
1214
13- ```
15+ ``` sh
1416npm install sql-formatter
1517```
1618
19+ ## Command Line Interface
20+
21+ The CLI tool will be installed under ` sql-formatter `
22+ and may be invoked via ` npx sql-formatter ` :
23+
24+ ``` sh
25+ sql-formatter -h
26+ ```
27+
28+ ```
29+ usage: sql-formatter [-h] [-v] [-f FILE] [-o OUTPUT]
30+ [-l {db2,n1ql,pl/sql,plsql,redshift,spark,sql}]
31+ [-i N | -t] [-u] [--lines-between-queries N]
32+
33+
34+ SQL Formatter
35+
36+ Optional arguments:
37+ -h, --help Show this help message and exit.
38+ -v, --version Show program's version number and exit.
39+ -f FILE, --file FILE Input SQL file (defaults to stdin)
40+ -o OUTPUT, --output OUTPUT
41+ File to write SQL output (defaults to stdout)
42+ -l {db2,n1ql,pl/sql,plsql,redshift,spark,sql}, --langauge {db2,n1ql,pl/sql,plsql,redshift,spark,sql}
43+ SQL Formatter dialect (defaults to basic sql)
44+ -i N, --indent N Number of spaces to indent query blocks (defaults to
45+ 2)
46+ -t, --tab-indent Indent query blocks with tabs instead of spaces
47+ -u, --uppercase Capitalize language keywords
48+ --lines-between-queries N
49+ How many newlines to insert between queries
50+ (separated by ";")
51+ ```
52+
53+ By default, the tool takes queries from stdin and processes them to stdout but
54+ the ` -f ` /` --file ` and ` -o ` /` --output ` flags can be used to alter this behavior.
55+
56+ ``` sh
57+ echo ' select * from tbl where id = 3' | sql-formatter -u
58+ ```
59+
60+ ``` sql
61+ SELECT
62+ *
63+ FROM
64+ tbl
65+ WHERE
66+ id = 3
67+ ```
68+
1769## Usage
1870
1971``` js
20- import sqlFormatter from " sql-formatter" ;
72+ import sqlFormatter from ' sql-formatter' ;
2173
22- console .log (sqlFormatter .format (" SELECT * FROM table1 " ));
74+ console .log (sqlFormatter .format (' SELECT * FROM tbl ' ));
2375```
2476
2577This will output:
2678
27- ```
79+ ``` sql
2880SELECT
2981 *
3082FROM
31- table1
83+ tbl
3284```
3385
3486You can also pass in configuration options:
3587
3688``` js
37- sqlFormatter .format (" SELECT *" , {
38- language: " n1ql" , // Defaults to "sql"
39- indent: " " // Defaults to two spaces
89+ sqlFormatter .format (' SELECT * FROM tbl' , {
90+ language: ' spark' , // Defaults to "sql"
91+ indent: ' ' , // Defaults to two spaces
92+ uppercase: bool, // Defaults to false
93+ linesBetweenQueries: 2 , // Defaults to 1
4094});
4195```
4296
43- Currently just four SQL dialects are supported:
97+ Currently six SQL dialects are supported:
4498
4599- ** sql** - [ Standard SQL] [ ]
46100- ** n1ql** - [ Couchbase N1QL] [ ]
47101- ** db2** - [ IBM DB2] [ ]
48102- ** pl/sql** - [ Oracle PL/SQL] [ ]
103+ - ** redshift** - [ Amazon Redshift] [ ]
104+ - ** spark** - [ Spark] [ ]
49105
50106### Placeholders replacement
51107
52108``` js
53109// Named placeholders
54110sqlFormatter .format (" SELECT * FROM tbl WHERE foo = @foo" , {
55- params: {foo: " 'bar'" }
111+ params: {foo: " 'bar'" }
56112}));
57113
58114// Indexed placeholders
59115sqlFormatter .format (" SELECT * FROM tbl WHERE foo = ?" , {
60- params: [" 'bar'" ]
116+ params: [" 'bar'" ]
61117}));
62118```
63119
@@ -79,9 +135,10 @@ This makes SQL Formatter available as a global variable `window.sqlFormatter`.
79135
80136## Contributing
81137
82- ``` bash
83- # run linter and tests
84- $ npm run check
138+ Make sure to run all checks:
139+
140+ ``` sh
141+ npm run check
85142```
86143
87144...and you're ready to poke us with a pull request.
@@ -90,8 +147,10 @@ $ npm run check
90147
91148[ MIT] ( https://github.com/zeroturnaround/sql-formatter/blob/master/LICENSE )
92149
93- [ PHP library ] : https://github.com/jdorn/sql-formatter
94- [ Standard SQL ] : https://en.wikipedia.org/wiki/SQL:2011
95- [ Couchbase N1QL ] : http://www.couchbase.com/n1ql
96- [ IBM DB2 ] : https://www.ibm.com/analytics/us/en/technology/db2/
97- [ Oracle PL/SQL ] : http://www.oracle.com/technetwork/database/features/plsql/index.html
150+ [ php library ] : https://github.com/jdorn/sql-formatter
151+ [ standard sql ] : https://en.wikipedia.org/wiki/SQL:2011
152+ [ couchbase n1ql ] : http://www.couchbase.com/n1ql
153+ [ ibm db2 ] : https://www.ibm.com/analytics/us/en/technology/db2/
154+ [ oracle pl/sql ] : http://www.oracle.com/technetwork/database/features/plsql/index.html
155+ [ amazon redshift ] : https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html
156+ [ spark ] : https://spark.apache.org/docs/latest/api/sql/index.html
0 commit comments