@@ -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
1819var 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
170180type 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
0 commit comments