11# SQL Formatter [ ![ NPM version] ( https://img.shields.io/npm/v/@gwax/sql-formatter.svg )] ( https://npmjs.com/package/@gwax/sql-formatter ) [ ![ Build Status] ( https://travis-ci.com/gwax/sql-formatter.svg?branch=master )] ( https://travis-ci.com/gwax/sql-formatter ) [ ![ Coverage Status] ( https://coveralls.io/repos/github/gwax/sql-formatter/badge.svg?branch=master )] ( https://coveralls.io/github/gwax/sql-formatter?branch=master )
22
3- ** SQL Formatter** is a JavaScript library for pretty-printing SQL queries.
4- It started as a Javascript port of a [ PHP Library] [ ] , but has diverged
5- considerably, and been forked/joined multiple times in the past. The current
6- formatter (@gwax/sql-formatter ) forked from [ zeroturnaround/sql-formatter] ( https://github.com/zeroturnaround/sql-formatter )
3+ ** SQL Formatter** is a JavaScript library and command line tool for
4+ pretty-printing SQL queries. It started as a Javascript port of a
5+ [ PHP Library] [ ] , but has diverged considerably, and been forked/joined multiple
6+ times in the past. The current formatter (@gwax/sql-formatter ) forked from
7+ [ zeroturnaround/sql-formatter] ( https://github.com/zeroturnaround/sql-formatter )
78with code consolidated from [ kufii/sql-formatter-plus] ( https://github.com/kufii/sql-formatter-plus )
89and a number of other forks scattered around GitHub.
910
@@ -16,16 +17,66 @@ SQL Formatter supports [Standard SQL][], [Couchbase N1QL][], [IBM DB2][],
1617
1718Get the latest version from NPM:
1819
20+ ``` sh
21+ npm install @gwax/sql-formatter
1922```
20- npm install sql-formatter
23+
24+ ## Command Line Interface
25+
26+ The CLI tool will be installed under ` @gwax/sql-formatter ` and under
27+ ` sql-formatter ` and may be invoked via ` npx @gwax/sql-formatter ` :
28+
29+ ``` sh
30+ npx @gwax/sql-formatter -h
31+ ```
32+
33+ ```
34+ usage: sql-formatter [-h] [-v] [-f FILE] [-o OUTPUT]
35+ [-l {db2,n1ql,pl/sql,plsql,redshift,spark,sql}]
36+ [-i N | -t] [-u] [--lines-between-queries N]
37+
38+
39+ SQL Formatter
40+
41+ Optional arguments:
42+ -h, --help Show this help message and exit.
43+ -v, --version Show program's version number and exit.
44+ -f FILE, --file FILE Input SQL file (defaults to stdin)
45+ -o OUTPUT, --output OUTPUT
46+ File to write SQL output (defaults to stdout)
47+ -l {db2,n1ql,pl/sql,plsql,redshift,spark,sql}, --langauge {db2,n1ql,pl/sql,plsql,redshift,spark,sql}
48+ SQL Formatter dialect (defaults to basic sql)
49+ -i N, --indent N Number of spaces to indent query blocks (defaults to
50+ 2)
51+ -t, --tab-indent Indent query blocks with tabs instead of spaces
52+ -u, --uppercase Capitalize language keywords
53+ --lines-between-queries N
54+ How many newlines to insert between queries
55+ (separated by ";")
56+ ```
57+
58+ By default, the tool takes queries from stdin and processes them to stdout but
59+ the ` -f ` /` --file ` and ` -o ` /` --output ` flags can be used to alter this behavior.
60+
61+ ``` sh
62+ echo ' select * from tbl where id = 3' | npx @gwax/sql-formatter -u
63+ ```
64+
65+ ``` sql
66+ SELECT
67+ *
68+ FROM
69+ tbl
70+ WHERE
71+ id = 3
2172```
2273
2374## Usage
2475
2576``` js
2677import sqlFormatter from ' @gwax/sql-formatter' ;
2778
28- console .log (sqlFormatter .format (' SELECT * FROM table1 ' ));
79+ console .log (sqlFormatter .format (' SELECT * FROM tbl ' ));
2980```
3081
3182This will output:
@@ -34,15 +85,17 @@ This will output:
3485SELECT
3586 *
3687FROM
37- table1
88+ tbl
3889```
3990
4091You can also pass in configuration options:
4192
4293``` js
43- sqlFormatter .format (' SELECT *' , {
44- language: ' n1ql ' , // Defaults to "sql"
94+ sqlFormatter .format (' SELECT * FROM tbl ' , {
95+ language: ' spark ' , // Defaults to "sql"
4596 indent: ' ' , // Defaults to two spaces
97+ uppercase: bool, // Defaults to false
98+ linesBetweenQueries: 2 , // Defaults to 1
4699});
47100```
48101
@@ -87,9 +140,10 @@ This makes SQL Formatter available as a global variable `window.sqlFormatter`.
87140
88141## Contributing
89142
90- ``` bash
91- # run linter and tests
92- $ npm run check
143+ Make sure to run all checks:
144+
145+ ``` sh
146+ npm run check
93147```
94148
95149...and you're ready to poke us with a pull request.
0 commit comments