Skip to content

Commit ea9e133

Browse files
committed
update some documentation and call it 3.0.0 for real
1 parent dafe235 commit ea9e133

File tree

3 files changed

+97
-26
lines changed

3 files changed

+97
-26
lines changed

README.md

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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)
78
with code consolidated from [kufii/sql-formatter-plus](https://github.com/kufii/sql-formatter-plus)
89
and a number of other forks scattered around GitHub.
910

@@ -16,16 +17,66 @@ SQL Formatter supports [Standard SQL][], [Couchbase N1QL][], [IBM DB2][],
1617

1718
Get 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
2677
import sqlFormatter from '@gwax/sql-formatter';
2778

28-
console.log(sqlFormatter.format('SELECT * FROM table1'));
79+
console.log(sqlFormatter.format('SELECT * FROM tbl'));
2980
```
3081

3182
This will output:
@@ -34,15 +85,17 @@ This will output:
3485
SELECT
3586
*
3687
FROM
37-
table1
88+
tbl
3889
```
3990

4091
You 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.

index.html

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ <h1>SQL Formatter</h1>
104104
</div>
105105

106106
<div class="select-wrapper">
107-
Format
107+
<label for="lanugage">Format</label>
108108
<select id="language">
109109
<option value="sql">
110110
SQL
@@ -122,43 +122,60 @@ <h1>SQL Formatter</h1>
122122
Spark
123123
</option>
124124
</select>
125+
|
126+
<label for="uppercae">Uppercase</label>
127+
<input type="checkbox" id="uppercase" name="uppercase" checked />
125128
</div>
126129
</header>
127130
<main>
128131
<section class="input">
129132
<textarea id="input" autofocus="true" wrap="off">
130-
SELECT supplier_name, city FROM suppliers&#10;WHERE supplier_id > 500&#10;ORDER BY supplier_name ASC, city DESC;</textarea
133+
select supplier_name,city from
134+
(select * from suppliers join addresses on suppliers.address_id=addresses.id)
135+
as suppliers
136+
where supplier_id>500
137+
order by supplier_name asc,city desc;
138+
</textarea
131139
>
132140
</section>
133141
<section class="output">
134142
<textarea id="output" readonly="true" wrap="off"></textarea>
135143
</section>
136144
</main>
137145

138-
<script
139-
type="text/javascript"
140-
src="https://unpkg.com/@gwax/sql-formatter@latest/dist/sql-formatter.min.js"
141-
></script>
146+
<!-- Load from local (for development) -->
147+
<script type="text/javascript" src="dist/sql-formatter.js"></script>
148+
<!-- Fallback to reading from npm -->
142149
<script>
143-
(function () {
150+
window.sqlFormatter ||
151+
document.write(
152+
unescape(
153+
'%3Cscript type="text/javascript" src="https://unpkg.com/@gwax/sql-formatter@latest/dist/sql-formatter.min.js"%3E%3C/script%3E'
154+
)
155+
);
156+
</script>
157+
<script>
158+
document.addEventListener('DOMContentLoaded', function () {
144159
let language = document.getElementById('language');
160+
let uppercase = document.getElementById('uppercase');
145161
let input = document.getElementById('input');
146162
let output = document.getElementById('output');
147163

148-
input.addEventListener('input', format);
149-
language.addEventListener('change', format);
150-
151164
function format() {
152165
console.time('formatting');
153-
154166
output.value = sqlFormatter.format(input.value, {
155167
language: language.options[language.selectedIndex].value,
168+
uppercase: uppercase.checked,
156169
});
157-
158170
console.timeEnd('formatting');
159171
}
172+
173+
input.addEventListener('input', format);
174+
language.addEventListener('change', format);
175+
uppercase.addEventListener('change', format);
176+
160177
format();
161-
})();
178+
});
162179
</script>
163180
</body>
164181
</html>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gwax/sql-formatter",
3-
"version": "3.0.0-beta.2",
3+
"version": "3.0.0",
44
"description": "Format whitespace in a SQL query to make it more readable",
55
"license": "MIT",
66
"main": "lib/sqlFormatter.js",

0 commit comments

Comments
 (0)