Skip to content

Commit e4792a5

Browse files
committed
Add --ast flag to analyze to include the statement AST
When --ast is passed, each analyzed query includes its raw statement AST under an "ast" key, matching the AST that "sqlc parse" emits. The field is omitted by default.
1 parent d018aaa commit e4792a5

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

internal/cmd/analyze.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/sqlc-dev/sqlc/internal/config"
1414
"github.com/sqlc-dev/sqlc/internal/multierr"
1515
"github.com/sqlc-dev/sqlc/internal/opts"
16+
"github.com/sqlc-dev/sqlc/internal/sql/ast"
1617
)
1718

1819
var analyzeCmd = &cobra.Command{
@@ -37,7 +38,10 @@ Examples:
3738
3839
# Analyze a query piped via stdin
3940
echo "-- name: GetAuthor :one
40-
SELECT * FROM authors WHERE id = $1;" | sqlc analyze --dialect postgresql --schema schema.sql`,
41+
SELECT * FROM authors WHERE id = $1;" | sqlc analyze --dialect postgresql --schema schema.sql
42+
43+
# Include the statement AST in the output
44+
sqlc analyze --dialect postgresql --schema schema.sql --ast query.sql`,
4145
Args: cobra.MaximumNArgs(1),
4246
RunE: func(cmd *cobra.Command, args []string) error {
4347
dialect, err := cmd.Flags().GetString("dialect")
@@ -56,6 +60,11 @@ Examples:
5660
return fmt.Errorf("--schema flag is required")
5761
}
5862

63+
includeAST, err := cmd.Flags().GetBool("ast")
64+
if err != nil {
65+
return err
66+
}
67+
5968
// The query comes from a file argument or, when none is given, from
6069
// stdin. The compiler reads queries from files, so stdin is written to
6170
// a temporary file.
@@ -127,7 +136,7 @@ Examples:
127136

128137
out := make([]analyzedQuery, 0, len(result.Queries))
129138
for _, q := range result.Queries {
130-
out = append(out, newAnalyzedQuery(q))
139+
out = append(out, newAnalyzedQuery(q, includeAST))
131140
}
132141

133142
stdout := cmd.OutOrStdout()
@@ -165,6 +174,7 @@ type analyzedQuery struct {
165174
Cmd string `json:"cmd"`
166175
Columns []analyzedColumn `json:"columns"`
167176
Params []analyzedParam `json:"params"`
177+
AST *ast.RawStmt `json:"ast,omitempty"`
168178
}
169179

170180
type analyzedColumn struct {
@@ -180,7 +190,7 @@ type analyzedParam struct {
180190
Column analyzedColumn `json:"column"`
181191
}
182192

183-
func newAnalyzedQuery(q *compiler.Query) analyzedQuery {
193+
func newAnalyzedQuery(q *compiler.Query, includeAST bool) analyzedQuery {
184194
aq := analyzedQuery{
185195
Name: q.Metadata.Name,
186196
Cmd: q.Metadata.Cmd,
@@ -196,6 +206,9 @@ func newAnalyzedQuery(q *compiler.Query) analyzedQuery {
196206
Column: newAnalyzedColumn(p.Column),
197207
})
198208
}
209+
if includeAST {
210+
aq.AST = q.RawStmt
211+
}
199212
return aq
200213
}
201214

internal/cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func init() {
3535
parseCmd.Flags().StringP("dialect", "d", "", "SQL dialect to use (postgresql, mysql, or sqlite)")
3636
analyzeCmd.Flags().StringP("dialect", "d", "", "SQL dialect to use (postgresql, mysql, or sqlite)")
3737
analyzeCmd.Flags().StringP("schema", "s", "", "path to the schema file")
38+
analyzeCmd.Flags().BoolP("ast", "", false, "include the statement AST in the output")
3839
}
3940

4041
// Do runs the command logic.

0 commit comments

Comments
 (0)