44 "fmt"
55 "io"
66 "io/ioutil"
7+ "path/filepath"
78 "regexp"
89 "sort"
910 "strings"
@@ -85,7 +86,9 @@ func parseCatalog(p Parser, c *catalog.Catalog, schemas []string) error {
8586}
8687
8788func parseQueries (p Parser , c * catalog.Catalog , queries []string ) (* Result , error ) {
89+ var q []* Query
8890 merr := multierr .New ()
91+ set := map [string ]struct {}{}
8992 files , err := sqlpath .Glob (queries )
9093 if err != nil {
9194 return nil , err
@@ -103,21 +106,41 @@ func parseQueries(p Parser, c *catalog.Catalog, queries []string) (*Result, erro
103106 continue
104107 }
105108 for _ , stmt := range stmts {
106- _ , err := parseQuery (p , c , stmt .Raw , src , false )
109+ query , err := parseQuery (p , c , stmt .Raw , src , false )
110+ if err == ErrUnsupportedStatementType {
111+ continue
112+ }
107113 if err != nil {
108114 merr .Add (filename , src , stmt .Raw .Pos (), err )
109115 continue
110116 }
117+ if query .Name != "" {
118+ if _ , exists := set [query .Name ]; exists {
119+ merr .Add (filename , src , 0 , fmt .Errorf ("duplicate query name: %s" , query .Name ))
120+ continue
121+ }
122+ set [query .Name ] = struct {}{}
123+ }
124+ query .Filename = filepath .Base (filename )
125+ if query != nil {
126+ q = append (q , query )
127+ }
111128 }
112129 }
113130 if len (merr .Errs ()) > 0 {
114131 return nil , merr
115132 }
116- return & Result {}, nil
133+ if len (q ) == 0 {
134+ return nil , fmt .Errorf ("no queries contained in paths %s" , strings .Join (queries , "," ))
135+ }
136+ return & Result {
137+ Catalog : c ,
138+ Queries : q ,
139+ }, nil
117140}
118141
119142// Deprecated.
120- func buildResult (c * catalog.Catalog ) (* Result , error ) {
143+ func buildResult (c * catalog.Catalog ) (* BuildResult , error ) {
121144 var structs []golang.Struct
122145 var enums []golang.Enum
123146 for _ , schema := range c .Schemas {
@@ -167,10 +190,10 @@ func buildResult(c *catalog.Catalog) (*Result, error) {
167190 if len (enums ) > 0 {
168191 sort .Slice (enums , func (i , j int ) bool { return enums [i ].Name < enums [j ].Name })
169192 }
170- return & Result {structs : structs , enums : enums }, nil
193+ return & BuildResult {structs : structs , enums : enums }, nil
171194}
172195
173- func Run (conf config.SQL , combo config.CombinedSettings ) (* Result , error ) {
196+ func Run (conf config.SQL , combo config.CombinedSettings ) (* BuildResult , error ) {
174197 var c * catalog.Catalog
175198 var p Parser
176199
0 commit comments