Repository navigation
Expand file tree
/
Copy pathslice.go
More file actions
70 lines (60 loc) · 1.58 KB
/
Copy pathslice.go
File metadata and controls
70 lines (60 loc) · 1.58 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
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/sqlerr"
)
type sliceVisitor struct {
// slices maps the location of a placeholder in the query text to the name
// of the sqlc.slice() parameter it was rewritten from.
slices map[int]string
err error
}
func (v *sliceVisitor) Visit(node ast.Node) astutils.Visitor {
if v.err != nil {
return nil
}
in, ok := node.(*ast.In)
if !ok {
return v
}
// A slice expands to a comma-separated list at query time, so it has to be
// the only element of the IN expression:
// 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 {
ref, ok := n.(*ast.ParamRef)
if !ok {
continue
}
name, ok := v.slices[ref.Location]
if !ok {
continue
}
inExpr := "..."
if col, ok := in.Expr.(*ast.ColumnRef); ok {
inExpr = col.Name
}
v.err = &sqlerr.Error{
Message: fmt.Sprintf("expected '%s IN' expr to consist only of sqlc.slice(%s); eg ", inExpr, name),
Location: ref.Location,
}
return nil
}
return v
}
// Slice reports whether every sqlc.slice() parameter is the only element of the
// IN expression that contains it. The slices map is keyed by the location the
// preprocessor recorded for each rewritten slice placeholder.
func Slice(n ast.Node, slices map[int]string) error {
if len(slices) == 0 {
return nil
}
visitor := sliceVisitor{slices: slices}
astutils.Walk(&visitor, n)
return visitor.err
}