-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_args.go
65 lines (53 loc) · 1.2 KB
/
context_args.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sqlf
import (
"strconv"
)
// Args returns the built args of the context.
func (c *Context) Args() []any {
return c.root().argStore.Args()
}
// CommitArg commits an built arg to the context and returns the built bindvar.
//
// It's used usually in the implementation of a FragmentBuilder,
// most users don't need to care about it.
func (c *Context) CommitArg(arg any) string {
return c.root().argStore.CommitArg(arg)
}
type argStore interface {
Args() []any
CommitArg(arg any) string
}
type questionArgStore struct {
args []any
}
func newQuestionArgStore() *questionArgStore {
return &questionArgStore{}
}
func (s *questionArgStore) Args() []any {
return s.args
}
func (s *questionArgStore) CommitArg(arg any) string {
s.args = append(s.args, arg)
return "?"
}
type dollarArgStore struct {
args []any
dict map[any]int
}
func newDollarArgStore() *dollarArgStore {
return &dollarArgStore{
dict: make(map[any]int),
}
}
func (s *dollarArgStore) Args() []any {
return s.args
}
func (s *dollarArgStore) CommitArg(arg any) string {
if i, ok := s.dict[arg]; ok {
return "$" + strconv.Itoa(i)
}
i := len(s.args) + 1
s.dict[arg] = i
s.args = append(s.args, arg)
return "$" + strconv.Itoa(i)
}