forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathin.go
More file actions
86 lines (73 loc) · 1.69 KB
/
in.go
File metadata and controls
86 lines (73 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package validate
import (
"fmt"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
)
type inVisitor struct {
catalog *catalog.Catalog
err error
}
func (v *inVisitor) Visit(node ast.Node) astutils.Visitor {
if v.err != nil {
return nil
}
in, ok := node.(*ast.In)
if !ok {
return v
}
// Validate that sqlc.slice in an IN statement is the only arg, eg:
// id IN (sqlc.slice("ids")) -- GOOD
// id in (0, 1, sqlc.slice("ids")) -- BAD
if len(in.List) <= 1 {
return v
}
for _, n := range in.List {
call, ok := n.(*ast.FuncCall)
if !ok {
continue
}
fn := call.Func
if fn == nil {
continue
}
if fn.Schema == "sqlc" && fn.Name == "slice" {
var inExpr, sliceArg string
// determine inExpr
switch n := in.Expr.(type) {
case *ast.ColumnRef:
inExpr = n.Name
default:
inExpr = "..."
}
// determine sliceArg
if len(call.Args.Items) == 1 {
switch n := call.Args.Items[0].(type) {
case *ast.A_Const:
if str, ok := n.Val.(*ast.String); ok {
sliceArg = "\"" + str.Str + "\""
} else {
sliceArg = "?"
}
case *ast.ColumnRef:
sliceArg = n.Name
default:
// impossible, validate.FuncCall should have caught this
sliceArg = "..."
}
}
v.err = &sqlerr.Error{
Message: fmt.Sprintf("expected '%s IN' expr to consist only of sqlc.slice(%s); eg ", inExpr, sliceArg),
Location: call.Pos(),
}
}
}
return v
}
func In(c *catalog.Catalog, n ast.Node) error {
visitor := inVisitor{catalog: c}
astutils.Walk(&visitor, n)
return visitor.err
}