-
Notifications
You must be signed in to change notification settings - Fork 55
/
args.go
264 lines (223 loc) · 5.95 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package zutil
import (
"bytes"
"database/sql"
"fmt"
"sort"
"strconv"
"strings"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/ztype"
)
// Args stores arguments associated
type Args struct {
namedArgs map[string]int
sqlNamedArgs map[string]int
compileHandler ArgsCompileHandler
args []argsArr
onlyNamed bool
}
type argsArr struct {
Fn func(k string) interface{}
Arg interface{}
}
type ArgsOpt func(*Args)
type ArgsCompileHandler func(buf *bytes.Buffer, values []interface{}, arg interface{}) ([]interface{}, bool)
const maxPredefinedArgs = 64
var predefinedArgs []string
func init() {
predefinedArgs = make([]string, 0, maxPredefinedArgs)
for i := 0; i < maxPredefinedArgs; i++ {
predefinedArgs = append(predefinedArgs, fmt.Sprintf("$%v", i))
}
}
func WithOnlyNamed() func(args *Args) {
return func(args *Args) {
args.onlyNamed = true
}
}
func WithCompileHandler(fn ArgsCompileHandler) func(args *Args) {
return func(args *Args) {
args.compileHandler = fn
}
}
// NewArgs returns a new Args
func NewArgs(opt ...ArgsOpt) *Args {
args := &Args{}
for _, o := range opt {
o(args)
}
return args
}
// Map adds an arg to Args and returns a placeholder
func (args *Args) Map(arg interface{}) string {
idx := args.add(arg, nil)
if idx < maxPredefinedArgs {
return predefinedArgs[idx]
}
return fmt.Sprintf("$%v", idx)
}
func (args *Args) add(arg interface{}, fn func(k string) interface{}) int {
idx := len(args.args)
switch a := arg.(type) {
case namedArgs:
if args.namedArgs == nil {
args.namedArgs = map[string]int{}
}
if p, ok := args.namedArgs[a.name]; ok {
arg = args.args[p]
break
}
arg := a.arg
switch v := a.arg.(type) {
default:
idx = args.add(arg, nil)
case func() interface{}:
idx = args.add(arg, func(_ string) interface{} { return v() })
case func(k string) interface{}:
idx = args.add(arg, v)
}
args.namedArgs[a.name] = idx
return idx
case sql.NamedArg:
if args.sqlNamedArgs == nil {
args.sqlNamedArgs = map[string]int{}
}
if p, ok := args.sqlNamedArgs[a.Name]; ok {
arg = args.args[p]
break
}
args.sqlNamedArgs[a.Name] = idx
}
args.args = append(args.args, argsArr{Arg: arg, Fn: fn})
return idx
}
// CompileString returns a string representation of Args
func (args *Args) CompileString(format string, initialValue ...interface{}) string {
old := args.compileHandler
args.compileHandler = func(buf *bytes.Buffer, values []interface{}, arg interface{}) ([]interface{}, bool) {
switch v := arg.(type) {
case string:
buf.WriteString(v)
case sql.NamedArg:
buf.WriteString(ztype.ToString(v.Value))
default:
val := ztype.ToString(v)
buf.WriteString(val)
}
return values, true
}
defer func() {
if old != nil {
args.compileHandler = old
}
}()
query, _ := args.Compile(format, initialValue...)
return query
}
// Compile compiles builder's format to standard sql and returns associated args
func (args *Args) Compile(format string, initialValue ...interface{}) (query string, values []interface{}) {
buf := GetBuff(256)
defer PutBuff(buf)
idx := strings.IndexRune(format, '$')
offset := 0
values = initialValue
for idx >= 0 && len(format) > 0 {
if idx > 0 {
buf.WriteString(format[:idx])
}
format = format[idx+1:]
if len(format) == 0 {
buf.WriteRune('$')
break
}
if r := format[0]; r == '$' {
buf.WriteRune('$')
format = format[1:]
} else if r == '{' {
format, values = args.compileNamed(buf, format, values)
} else if !args.onlyNamed && '0' <= r && r <= '9' {
format, values, offset = args.compileDigits(buf, format, values, offset)
} else if !args.onlyNamed && r == '?' {
format, values, offset = args.compileSuccessive(buf, format[1:], values, offset, "")
} else {
buf.WriteRune('$')
}
idx = strings.IndexRune(format, '$')
}
if len(format) > 0 {
buf.WriteString(format)
}
query = buf.String()
if len(args.sqlNamedArgs) > 0 {
ints := make([]int, 0, len(args.sqlNamedArgs))
for _, p := range args.sqlNamedArgs {
ints = append(ints, p)
}
sort.Ints(ints)
for _, i := range ints {
values = append(values, args.args[i].Arg)
}
}
return
}
func (args *Args) compileNamed(buf *bytes.Buffer, format string, values []interface{}) (string, []interface{}) {
i := 1
for ; i < len(format) && format[i] != '}'; i++ {
}
if i == len(format) {
return format, values
}
name := format[1:i]
format = format[i+1:]
if p, ok := args.namedArgs[name]; ok {
format, values, _ = args.compileSuccessive(buf, format, values, p, "")
} else if strings.IndexRune(name, '.') > 0 {
for n := range args.namedArgs {
if zstring.Match(name, n) {
p := args.namedArgs[n]
format, values, _ = args.compileSuccessive(buf, format, values, p, name)
}
}
}
return format, values
}
func (args *Args) compileDigits(buf *bytes.Buffer, format string, values []interface{}, offset int) (string, []interface{}, int) {
i := 1
for ; i < len(format) && '0' <= format[i] && format[i] <= '9'; i++ {
}
digits := format[:i]
format = format[i:]
if pointer, err := strconv.Atoi(digits); err == nil {
return args.compileSuccessive(buf, format, values, pointer, "")
}
return format, values, offset
}
func (args *Args) compileSuccessive(buf *bytes.Buffer, format string, values []interface{}, offset int, name string) (string, []interface{}, int) {
if offset >= len(args.args) {
return format, values, offset
}
arg := args.args[offset]
if arg.Fn != nil {
values = args.CompileArg(buf, values, arg.Fn(name))
} else {
values = args.CompileArg(buf, values, arg.Arg)
}
return format, values, offset + 1
}
func (args *Args) CompileArg(buf *bytes.Buffer, values []interface{}, arg interface{}) []interface{} {
if args.compileHandler != nil {
if values, ok := args.compileHandler(buf, values, arg); ok {
return values
}
}
switch a := arg.(type) {
case sql.NamedArg:
buf.WriteRune('@')
buf.WriteString(a.Name)
default:
buf.WriteRune('?')
values = append(values, arg)
}
return values
}