Skip to content

Commit 41ba88f

Browse files
committed
Document the parse and analyze commands
Add how-to pages for the new parse and analyze commands, list them in the CLI reference, and register them in the Commands toctree.
1 parent 51413e1 commit 41ba88f

4 files changed

Lines changed: 159 additions & 0 deletions

File tree

docs/howto/analyze.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# `analyze` - Analyzing query result types
2+
3+
`sqlc analyze` analyzes a query against a schema and prints the inferred result
4+
columns and parameters as a single JSON document.
5+
6+
Unlike [`generate`](generate.md), this command does not require a configuration
7+
file and does not connect to a database. It uses sqlc's native static analysis
8+
to infer types directly from the provided schema.
9+
10+
## Usage
11+
12+
```sh
13+
sqlc analyze --dialect <dialect> --schema <schema-file> [query-file]
14+
```
15+
16+
The query is read from the given file, or from standard input when no file is
17+
provided. The schema is always read from the `--schema` file.
18+
19+
## Flags
20+
21+
- `--dialect`, `-d` - The SQL dialect to use. One of `postgresql`, `mysql`, or
22+
`sqlite`. Required.
23+
- `--schema`, `-s` - Path to the schema (DDL) file. Required.
24+
- `--ast` - Include each statement's AST in the output. Defaults to `false`.
25+
26+
## Examples
27+
28+
Given a schema in `schema.sql`:
29+
30+
```sql
31+
CREATE TABLE authors (
32+
id BIGSERIAL PRIMARY KEY,
33+
name text NOT NULL,
34+
bio text
35+
);
36+
```
37+
38+
and a query in `query.sql`:
39+
40+
```sql
41+
-- name: GetAuthor :one
42+
SELECT * FROM authors WHERE id = $1;
43+
```
44+
45+
Running:
46+
47+
```sh
48+
sqlc analyze --dialect postgresql --schema schema.sql query.sql
49+
```
50+
51+
reports the result columns and parameters:
52+
53+
```json
54+
[
55+
{
56+
"name": "GetAuthor",
57+
"cmd": ":one",
58+
"columns": [
59+
{
60+
"name": "id",
61+
"data_type": "bigserial",
62+
"not_null": true,
63+
"is_array": false,
64+
"table": "authors"
65+
},
66+
{
67+
"name": "name",
68+
"data_type": "text",
69+
"not_null": true,
70+
"is_array": false,
71+
"table": "authors"
72+
},
73+
{
74+
"name": "bio",
75+
"data_type": "text",
76+
"not_null": false,
77+
"is_array": false,
78+
"table": "authors"
79+
}
80+
],
81+
"params": [
82+
{
83+
"number": 1,
84+
"column": {
85+
"name": "id",
86+
"data_type": "bigserial",
87+
"not_null": true,
88+
"is_array": false,
89+
"table": "authors"
90+
}
91+
}
92+
]
93+
}
94+
]
95+
```
96+
97+
Pass `--ast` to also include each statement's parsed AST under an `ast` key.

docs/howto/parse.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# `parse` - Parsing SQL into an AST
2+
3+
`sqlc parse` parses SQL from a file or standard input and prints the abstract
4+
syntax tree (AST) as a single JSON document. It does not require a configuration
5+
file or a database connection.
6+
7+
Each statement is reported with its sqlc query name and command (when the
8+
statement carries a [`-- name:`](../reference/query-annotations.md) annotation)
9+
alongside its AST.
10+
11+
## Usage
12+
13+
```sh
14+
sqlc parse --dialect <dialect> [file]
15+
```
16+
17+
The SQL is read from the given file, or from standard input when no file is
18+
provided.
19+
20+
## Flags
21+
22+
- `--dialect`, `-d` - The SQL dialect to use. One of `postgresql`, `mysql`,
23+
`sqlite`, or `clickhouse`. Required.
24+
25+
## Examples
26+
27+
Parse a query file:
28+
29+
```sh
30+
sqlc parse --dialect postgresql query.sql
31+
```
32+
33+
Parse SQL piped via standard input:
34+
35+
```sh
36+
echo "SELECT 1;" | sqlc parse --dialect mysql
37+
```
38+
39+
The output is a JSON array with one object per statement:
40+
41+
```json
42+
[
43+
{
44+
"name": "GetAuthor",
45+
"cmd": ":one",
46+
"ast": {
47+
"Stmt": {
48+
"...": "..."
49+
},
50+
"StmtLocation": 0,
51+
"StmtLen": 42
52+
}
53+
}
54+
]
55+
```
56+
57+
Statements without a `-- name:` annotation (for example schema DDL) omit the
58+
`name` and `cmd` fields.

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ code ever again.
4141
:caption: Commands
4242
:hidden:
4343

44+
howto/analyze.md
4445
howto/generate.md
46+
howto/parse.md
4547
howto/push.md
4648
howto/verify.md
4749
howto/vet.md

docs/reference/cli.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ Usage:
55
sqlc [command]
66

77
Available Commands:
8+
analyze Analyze a query against a schema and output the result columns and parameters
89
compile Statically check SQL for syntax and type errors
910
completion Generate the autocompletion script for the specified shell
1011
createdb Create an ephemeral database
1112
diff Compare the generated files to the existing files
1213
generate Generate source code from SQL
1314
help Help about any command
1415
init Create an empty sqlc.yaml settings file
16+
parse Parse SQL and output the AST as JSON
1517
push Push the schema, queries, and configuration for this project
1618
verify Verify schema, queries, and configuration for this project
1719
version Print the sqlc version number

0 commit comments

Comments
 (0)