11package compiler
22
33import (
4+ "fmt"
5+
46 "github.com/kyleconroy/sqlc/internal/sql/ast"
57 "github.com/kyleconroy/sqlc/internal/sql/astutils"
68)
79
8- func findParameters (root ast.Node ) []paramRef {
10+ func findParameters (root ast.Node ) ( []paramRef , error ) {
911 refs := make ([]paramRef , 0 )
10- v := paramSearch {seen : make (map [int ]struct {}), refs : & refs }
12+ errors := make ([]error , 0 )
13+ v := paramSearch {seen : make (map [int ]struct {}), refs : & refs , errs : & errors }
1114 astutils .Walk (v , root )
12- return refs
15+ if len (* v .errs ) > 0 {
16+ problems := * v .errs
17+ return nil , problems [0 ]
18+ } else {
19+ return refs , nil
20+ }
1321}
1422
1523type paramRef struct {
@@ -24,6 +32,7 @@ type paramSearch struct {
2432 rangeVar * ast.RangeVar
2533 refs * []paramRef
2634 seen map [int ]struct {}
35+ errs * []error
2736
2837 // XXX: Gross state hack for limit
2938 limitCount ast.Node
@@ -45,6 +54,10 @@ func (l *limitOffset) Pos() int {
4554}
4655
4756func (p paramSearch ) Visit (node ast.Node ) astutils.Visitor {
57+ if len (* p .errs ) > 0 {
58+ return p
59+ }
60+
4861 switch n := node .(type ) {
4962
5063 case * ast.A_Expr :
@@ -64,7 +77,10 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
6477 if ! ok {
6578 continue
6679 }
67- // TODO: Out-of-bounds panic
80+ if len (n .Cols .Items ) <= i {
81+ * p .errs = append (* p .errs , fmt .Errorf ("INSERT has more expressions than target columns" ))
82+ return p
83+ }
6884 * p .refs = append (* p .refs , paramRef {parent : n .Cols .Items [i ], ref : ref , rv : n .Relation })
6985 p .seen [ref .Location ] = struct {}{}
7086 }
@@ -78,7 +94,10 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
7894 if ! ok {
7995 continue
8096 }
81- // TODO: Out-of-bounds panic
97+ if len (n .Cols .Items ) <= i {
98+ * p .errs = append (* p .errs , fmt .Errorf ("INSERT has more expressions than target columns" ))
99+ return p
100+ }
82101 * p .refs = append (* p .refs , paramRef {parent : n .Cols .Items [i ], ref : ref , rv : n .Relation })
83102 p .seen [ref .Location ] = struct {}{}
84103 }
0 commit comments