forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_ref.go
48 lines (44 loc) · 1.11 KB
/
param_ref.go
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
package validate
import (
"errors"
"fmt"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/sql/ast"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/sql/astutils"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/sql/sqlerr"
)
func ParamRef(n ast.Node) (map[int]bool, bool, error) {
var allrefs []*ast.ParamRef
var dollar bool
var nodollar bool
// Find all parameter references
astutils.Walk(astutils.VisitorFunc(func(node ast.Node) {
switch n := node.(type) {
case *ast.ParamRef:
ref := node.(*ast.ParamRef)
if ref.Dollar {
dollar = true
} else {
nodollar = true
}
allrefs = append(allrefs, n)
}
}), n)
if dollar && nodollar {
return nil, false, errors.New("can not mix $1 format with ? format")
}
seen := map[int]bool{}
for _, r := range allrefs {
if r.Number > 0 {
seen[r.Number] = true
}
}
for i := 1; i <= len(seen); i += 1 {
if _, ok := seen[i]; !ok {
return nil, false, &sqlerr.Error{
Code: "42P18",
Message: fmt.Sprintf("could not determine data type of parameter $%d", i),
}
}
}
return seen, !nodollar, nil
}