Skip to content

Commit 5ab441a

Browse files
authored
Add sqlc fmt: a comment-preserving SQL formatter, SQLite first (#4580)
1 parent 19038bb commit 5ab441a

54 files changed

Lines changed: 2082 additions & 159 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/howto/fmt.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# `fmt` - Formatting queries
2+
3+
`sqlc fmt` rewrites the query files referenced by your configuration file in a
4+
canonical format. Each query is parsed with the engine's parser and printed
5+
back from the syntax tree, so formatting never depends on how the query was
6+
written — only on what it means.
7+
8+
Like `gofmt`, the formatter does not impose a maximum line width. A statement
9+
written on a single line stays on a single line, and a statement the author
10+
broke across lines keeps its breaks: the printer notices which clause
11+
boundaries (`FROM`, `WHERE`, `ORDER BY`, ...) and list boundaries the author
12+
broke at and preserves them, normalizing indentation and spacing around them.
13+
14+
Comments inside a statement are formatted along with it: each comment is
15+
anchored to the code around it by source position and printed back there —
16+
a comment trailing a select-list item stays with that item, a comment above
17+
a clause stays above its keyword — and a comment that runs to the end of its
18+
line breaks the statement open around it. Any statement that cannot be
19+
proven to survive formatting unchanged is left exactly as written.
20+
21+
## Usage
22+
23+
```sh
24+
sqlc fmt [--diff]
25+
```
26+
27+
Without flags, the query files are rewritten in place. With `--diff`, the
28+
changes are printed to standard output instead and no files are modified.
29+
30+
## Examples
31+
32+
Given this query file:
33+
34+
```sql
35+
-- name: GetAuthor :one
36+
select id,name , bio
37+
from authors
38+
where id = ? limit 1;
39+
40+
-- name: SearchAuthors :many
41+
SELECT id, -- the primary key
42+
name, bio, created_at FROM authors WHERE name LIKE ? AND bio IS NOT NULL AND id > ? AND created_at > ? AND name <> ? ORDER BY name;
43+
```
44+
45+
running `sqlc fmt` rewrites it to:
46+
47+
```sql
48+
-- name: GetAuthor :one
49+
SELECT id, name, bio
50+
FROM authors
51+
WHERE id = ?
52+
LIMIT 1;
53+
54+
-- name: SearchAuthors :many
55+
SELECT
56+
id, -- the primary key
57+
name,
58+
bio,
59+
created_at
60+
FROM authors
61+
WHERE name LIKE ? AND bio IS NOT NULL AND id > ? AND created_at > ? AND name <> ?
62+
ORDER BY name;
63+
```
64+
65+
`GetAuthor` keeps the line breaks its author wrote; had it been written on
66+
one line, it would stay on one line. In `SearchAuthors`, the line comment
67+
cannot share a line with the code after it, so the statement breaks open
68+
around it, while the `WHERE` chain — written on one line — stays on one.

docs/reference/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Available Commands:
1010
completion Generate the autocompletion script for the specified shell
1111
createdb Create an ephemeral database
1212
diff Compare the generated files to the existing files
13+
fmt Format SQL queries
1314
generate Generate source code from SQL
1415
help Help about any command
1516
init Create an empty sqlc.yaml settings file

docs/toc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ sections:
2020
- title: Commands
2121
pages:
2222
- howto/analyze.md
23+
- howto/fmt.md
2324
- howto/generate.md
2425
- howto/parse.md
2526
- howto/push.md

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/sqlc-dev/darkwing v0.1.0
2222
github.com/sqlc-dev/doubleclick v1.0.0
2323
github.com/sqlc-dev/marino v0.3.0
24-
github.com/sqlc-dev/meyer v0.1.1
24+
github.com/sqlc-dev/meyer v0.1.2
2525
github.com/sqlc-dev/oliphant v0.1.0
2626
github.com/sqlc-dev/teesql v1.1.0
2727
github.com/sqlc-dev/zetajones v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ github.com/sqlc-dev/doubleclick v1.0.0 h1:2/OApfQ2eLgcfa/Fqs8WSMA6atH0G8j9hHbQIg
7171
github.com/sqlc-dev/doubleclick v1.0.0/go.mod h1:ODHRroSrk/rr5neRHlWMSRijqOak8YmNaO3VAZCNl5Y=
7272
github.com/sqlc-dev/marino v0.3.0 h1:e9cinBXJFFa3yRpokYNNinWvkewgd6XVDgnlG0bOmTw=
7373
github.com/sqlc-dev/marino v0.3.0/go.mod h1:mQxC2dgDE0DWHMb2B5jZNk7KToJuS6wnxnffBfYnq08=
74-
github.com/sqlc-dev/meyer v0.1.1 h1:BAeZcfgLyTnk9f90DyGEKXPrHxtgvVD/DTM6awq2kUY=
75-
github.com/sqlc-dev/meyer v0.1.1/go.mod h1:pS4USCRf/SLjWtaMcnTo4YrEEFKBj8CyyqlxcVUJQH8=
74+
github.com/sqlc-dev/meyer v0.1.2 h1:40Ng9Glnx7CTf3yOYV7jfvDIN7voIFjrStkIULS+uws=
75+
github.com/sqlc-dev/meyer v0.1.2/go.mod h1:pS4USCRf/SLjWtaMcnTo4YrEEFKBj8CyyqlxcVUJQH8=
7676
github.com/sqlc-dev/oliphant v0.1.0 h1:RAsO6BMitIzB2+swx/qzUR5nf6w4cQ1abgHIu+Fgppo=
7777
github.com/sqlc-dev/oliphant v0.1.0/go.mod h1:fRM/t4FutRddTIq2YCuS4O9o+2rRwSwELRvLMqtPloo=
7878
github.com/sqlc-dev/teesql v1.1.0 h1:3sVYQ9FGxQVcqrqQOQ27bk0aF4c4yN1H1zLL79uaSxQ=

internal/cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
4343
rootCmd.AddCommand(createDBCmd)
4444
rootCmd.AddCommand(diffCmd)
4545
rootCmd.AddCommand(genCmd)
46+
rootCmd.AddCommand(newFmtCmd())
4647
rootCmd.AddCommand(initCmd)
4748
rootCmd.AddCommand(newParseCmd())
4849
rootCmd.AddCommand(newAnalyzeCmd())

0 commit comments

Comments
 (0)