-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
221 lines (194 loc) · 4.8 KB
/
context.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
package sqlt
import (
"database/sql"
"fmt"
"reflect"
"strconv"
"strings"
)
const (
escapeChar = '\\'
escapeClause = " ESCAPE '" + string(escapeChar) + "'"
)
type param struct {
name string
value interface{}
}
func newParam(name string, value interface{}) *param {
return ¶m{
name: name,
value: value,
}
}
type context struct {
named bool
dialect Dialect
params []*param
args []*param
timer *timer
config *config
err error
}
func newContext(named bool, dialect Dialect, m map[string]interface{}, conf *config) *context {
params := make([]*param, len(m))
i := 0
for k, v := range m {
params[i] = newParam(k, v)
i++
}
return &context{
named: named,
dialect: dialect,
params: params,
args: make([]*param, 0),
timer: newTimer(conf.timeFunc),
config: conf,
}
}
func (c *context) Get(name string) (*param, error) {
if strings.Contains(name, ".") {
return c.Dig(strings.Split(name, "."))
}
for _, p := range c.params {
if p.name == name {
return p, nil
}
}
return nil, fmt.Errorf("%q is unknown param", name)
}
func (c *context) Dig(names []string) (*param, error) {
p, err := c.Get(names[0])
if err != nil {
return nil, err
}
qname := names[0]
if p.value == nil {
return nil, fmt.Errorf("%q is nil value", qname)
}
v := reflect.ValueOf(p.value)
for _, name := range names[1:] {
v, err = findValue(v, name, qname)
if err != nil {
return nil, err
}
qname = qname + "." + name
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return nil, fmt.Errorf("%q is nil value", qname)
}
}
}
return newParam(strings.Join(names, Connector), v.Interface()), nil
}
func findValue(val reflect.Value, name string, prefix string) (reflect.Value, error) {
if i, err := strconv.Atoi(name); err == nil {
return findIndexValue(val, i, prefix)
}
// cannot find field from pointer or interface, but can find method from those.
// so at first search method.
v, err := findMethodValue(val, name, prefix)
if err != nil {
return v, err
}
if v.IsValid() {
return v, nil
}
return findFieldValue(val, name, prefix)
}
func findMethodValue(val reflect.Value, name string, prefix string) (reflect.Value, error) {
// Finding method raises panic when Interface and nil.
if val.Kind() == reflect.Interface && val.IsNil() {
return val, fmt.Errorf("%q is nil value", prefix)
}
v := val.MethodByName(name)
qname := prefix + "." + name
if !v.IsValid() {
return v, nil
}
t := v.Type()
if t.NumIn() != 0 || t.NumOut() != 1 {
return v, fmt.Errorf("%q is invalid method", qname)
}
v = v.Call([]reflect.Value{})[0]
return v, nil
}
func findFieldValue(val reflect.Value, name string, prefix string) (reflect.Value, error) {
// When value is pointer or interface, must call `Elem()` recursively.
if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
return findFieldValue(val.Elem(), name, prefix)
}
// Finding field raises panic when not struct
if val.Kind() != reflect.Struct {
return val, fmt.Errorf("%q is not struct", prefix)
}
v := val.FieldByName(name)
qname := prefix + "." + name
if !v.IsValid() {
return v, fmt.Errorf("%q is unknown param", qname)
}
return v, nil
}
func findIndexValue(val reflect.Value, i int, prefix string) (reflect.Value, error) {
if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
return findIndexValue(val.Elem(), i, prefix)
}
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return val, fmt.Errorf("%q is not slice or array", prefix)
}
if val.Len() <= i {
return val, fmt.Errorf("%q is too short for index %d", prefix, i)
}
return val.Index(i), nil
}
func (c *context) AddArg(name string, value interface{}) {
c.args = append(c.args, newParam(name, value))
}
func (c *context) MergeArg(name string, value interface{}) {
for _, arg := range c.args {
if arg.name == name {
return
}
}
c.AddArg(name, value)
}
func (c *context) ArgIndex(name string) int {
for i, arg := range c.args {
if arg.name == name {
return i + 1
}
}
return 0
}
func (c *context) Args() []interface{} {
v := make([]interface{}, len(c.args))
for i, arg := range c.args {
v[i] = arg.value
}
return v
}
func (c *context) NamedArgs() []sql.NamedArg {
v := make([]sql.NamedArg, len(c.args))
for i, arg := range c.args {
v[i] = sql.Named(arg.name, arg.value)
}
return v
}
func (c *context) Placeholder(name string) string {
if c.named {
return c.dialect.NamedPlaceholderPrefix() + name
}
if c.dialect.IsOrdinalPlaceholderSupported() {
return c.dialect.OrdinalPlaceholderPrefix() + strconv.Itoa(c.ArgIndex(name))
}
return c.dialect.Placeholder()
}
func (c *context) annotation(s string) string {
if c.config.annotative {
return "/*# " + s + " */"
}
return ""
}
func (c *context) errorOutput(err error) string {
c.err = err
return c.annotation("error: " + err.Error())
}