@@ -17,6 +17,7 @@ import (
1717 core "github.com/kyleconroy/sqlc/internal/pg"
1818 "github.com/kyleconroy/sqlc/internal/postgres"
1919 "github.com/kyleconroy/sqlc/internal/postgresql/ast"
20+ "github.com/kyleconroy/sqlc/internal/postgresql/validate"
2021 "github.com/kyleconroy/sqlc/internal/sql/sqlpath"
2122
2223 "github.com/davecgh/go-spew/spew"
@@ -49,7 +50,7 @@ func ParseCatalog(schemas []string) (core.Catalog, error) {
4950 continue
5051 }
5152 for _ , stmt := range tree .Statements {
52- if err := validateFuncCall (& c , stmt ); err != nil {
53+ if err := validate . FuncCall (& c , stmt ); err != nil {
5354 merr .Add (filename , contents , location (stmt ), err )
5455 continue
5556 }
@@ -72,7 +73,7 @@ func ParseCatalog(schemas []string) (core.Catalog, error) {
7273
7374func updateCatalog (c * core.Catalog , tree pg.ParsetreeList ) error {
7475 for _ , stmt := range tree .Statements {
75- if err := validateFuncCall (c , stmt ); err != nil {
76+ if err := validate . FuncCall (c , stmt ); err != nil {
7677 return err
7778 }
7879 if err := catalog .Update (c , stmt ); err != nil {
@@ -301,10 +302,10 @@ func validateCmd(n nodes.Node, name, cmd string) error {
301302var errUnsupportedStatementType = errors .New ("parseQuery: unsupported statement type" )
302303
303304func parseQuery (c core.Catalog , stmt nodes.Node , source string , rewriteParameters bool ) (* Query , error ) {
304- if err := validateParamStyle (stmt ); err != nil {
305+ if err := validate . ParamStyle (stmt ); err != nil {
305306 return nil , err
306307 }
307- if err := validateParamRef (stmt ); err != nil {
308+ if err := validate . ParamRef (stmt ); err != nil {
308309 return nil , err
309310 }
310311 raw , ok := stmt .(nodes.RawStmt )
@@ -315,7 +316,7 @@ func parseQuery(c core.Catalog, stmt nodes.Node, source string, rewriteParameter
315316 case nodes.SelectStmt :
316317 case nodes.DeleteStmt :
317318 case nodes.InsertStmt :
318- if err := validateInsertStmt (n ); err != nil {
319+ if err := validate . InsertStmt (n ); err != nil {
319320 return nil , err
320321 }
321322 case nodes.TruncateStmt :
@@ -331,7 +332,7 @@ func parseQuery(c core.Catalog, stmt nodes.Node, source string, rewriteParameter
331332 if rawSQL == "" {
332333 return nil , errors .New ("missing semicolon at end of file" )
333334 }
334- if err := validateFuncCall (& c , raw ); err != nil {
335+ if err := validate . FuncCall (& c , raw ); err != nil {
335336 return nil , err
336337 }
337338 name , cmd , err := ParseMetadata (strings .TrimSpace (rawSQL ), CommentSyntaxDash )
@@ -437,7 +438,7 @@ type edit struct {
437438}
438439
439440func expand (qc * QueryCatalog , raw nodes.RawStmt ) ([]edit , error ) {
440- list := search (raw , func (node nodes.Node ) bool {
441+ list := ast . Search (raw , func (node nodes.Node ) bool {
441442 switch node .(type ) {
442443 case nodes.DeleteStmt :
443444 case nodes.InsertStmt :
@@ -655,7 +656,7 @@ func sourceTables(qc *QueryCatalog, node nodes.Node) ([]core.Table, error) {
655656 Items : []nodes.Node {* n .Relation },
656657 }
657658 case nodes.SelectStmt :
658- list = search (n .FromClause , func (node nodes.Node ) bool {
659+ list = ast . Search (n .FromClause , func (node nodes.Node ) bool {
659660 switch node .(type ) {
660661 case nodes.RangeVar , nodes.RangeSubselect :
661662 return true
@@ -664,7 +665,7 @@ func sourceTables(qc *QueryCatalog, node nodes.Node) ([]core.Table, error) {
664665 }
665666 })
666667 case nodes.TruncateStmt :
667- list = search (n .Relations , func (node nodes.Node ) bool {
668+ list = ast . Search (n .Relations , func (node nodes.Node ) bool {
668669 _ , ok := node .(nodes.RangeVar )
669670 return ok
670671 })
@@ -1095,24 +1096,6 @@ func findParameters(root nodes.Node) []paramRef {
10951096 return refs
10961097}
10971098
1098- type nodeSearch struct {
1099- list nodes.List
1100- check func (nodes.Node ) bool
1101- }
1102-
1103- func (s * nodeSearch ) Visit (node nodes.Node ) ast.Visitor {
1104- if s .check (node ) {
1105- s .list .Items = append (s .list .Items , node )
1106- }
1107- return s
1108- }
1109-
1110- func search (root nodes.Node , f func (nodes.Node ) bool ) nodes.List {
1111- ns := & nodeSearch {check : f }
1112- ast .Walk (ns , root )
1113- return ns .list
1114- }
1115-
11161099func resolveCatalogRefs (c core.Catalog , rvs []nodes.RangeVar , args []paramRef , names map [int ]string ) ([]Parameter , error ) {
11171100 aliasMap := map [string ]core.FQN {}
11181101 // TODO: Deprecate defaultTable
@@ -1194,7 +1177,7 @@ func resolveCatalogRefs(c core.Catalog, rvs []nodes.RangeVar, args []paramRef, n
11941177 case nodes.A_Expr :
11951178 // TODO: While this works for a wide range of simple expressions,
11961179 // more complicated expressions will cause this logic to fail.
1197- list := search (n .Lexpr , func (node nodes.Node ) bool {
1180+ list := ast . Search (n .Lexpr , func (node nodes.Node ) bool {
11981181 _ , ok := node .(nodes.ColumnRef )
11991182 return ok
12001183 })
0 commit comments