|
| 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. |
0 commit comments