-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
170 lines (151 loc) · 3.38 KB
/
util.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
package tyr
import (
"database/sql/driver"
"reflect"
"strings"
)
var NameMapping = camelCaseToSnakeCase
func isUpper(b byte) bool {
return b >= 'A' && b <= 'Z'
}
func isLower(b byte) bool {
return b >= 'a' && b <= 'z'
}
func isDigit(b byte) bool {
return b >= '0' && b <= '9'
}
func toLower(b byte) byte {
if isUpper(b) {
return b - 'A' + 'a'
}
return b
}
func camelCaseToSnakeCase(name string) string {
var buf strings.Builder
buf.Grow(len(name) * 2)
for i := 0; i < len(name); i++ {
if err := buf.WriteByte(toLower(name[i])); err == nil {
if i != len(name)-1 && isUpper(name[i+1]) &&
(isLower(name[i]) || isDigit(name[i]) ||
(i != len(name)-2 && isLower(name[i+2]))) {
_ = buf.WriteByte('_')
}
}
}
return buf.String()
}
var (
typeValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
)
type tagStore struct {
m map[reflect.Type][]string
}
func newTagStore() *tagStore {
return &tagStore{
m: make(map[reflect.Type][]string),
}
}
func (s *tagStore) get(t reflect.Type) []string {
if t.Kind() != reflect.Struct {
return nil
}
if _, ok := s.m[t]; !ok {
l := make([]string, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.PkgPath != "" && !field.Anonymous {
// unexported
continue
}
tag := field.Tag.Get(sqlTag)
if tag == "-" {
// ignore
continue
}
if tag == "" {
// no tag, but we can record the field name
tag = NameMapping(field.Name)
}
l[i] = tag
}
s.m[t] = l
}
return s.m[t]
}
func (s *tagStore) findPtr(value reflect.Value, name []string, ptr []interface{}) error {
if value.CanAddr() && value.Addr().Type().Implements(typeScanner) {
ptr[0] = value.Addr().Interface()
return nil
}
switch value.Kind() {
case reflect.Struct:
s.findValueByName(value, name, ptr, true)
return nil
case reflect.Ptr:
if value.IsNil() {
value.Set(reflect.New(value.Type().Elem()))
}
return s.findPtr(value.Elem(), name, ptr)
default:
ptr[0] = value.Addr().Interface()
return nil
}
}
func (s *tagStore) findValueByName(value reflect.Value, name []string, ret []interface{}, retPtr bool) {
if value.Type().Implements(typeValuer) {
return
}
switch value.Kind() {
case reflect.Ptr:
if value.IsNil() {
return
}
s.findValueByName(value.Elem(), name, ret, retPtr)
case reflect.Struct:
l := s.get(value.Type())
for i := 0; i < value.NumField(); i++ {
tag := l[i]
if tag == "" {
continue
}
fieldValue := value.Field(i)
for i, want := range name {
if want != tag {
continue
}
if ret[i] == nil {
if retPtr {
ret[i] = fieldValue.Addr().Interface()
} else {
ret[i] = fieldValue
}
}
}
s.findValueByName(fieldValue, name, ret, retPtr)
}
}
}
func interpolateSql(d Dialect, i Buffer, query string, value []interface{}) error {
valueIndex := 0
N := 0
for {
index := strings.Index(query, placeholder)
if index == -1 {
break
}
// escape placeholder by repeating it twice
if strings.HasPrefix(query[index:], escapedPlaceholder) {
_, _ = i.WriteString(query[:index+1]) // Write placeholder once, not twice
query = query[index+len(escapedPlaceholder):]
continue
}
_, _ = i.WriteString(query[:index])
_, _ = i.WriteString(d.Placeholder(N))
N++
_ = i.WriteValue(value[valueIndex])
query = query[index+len(placeholder):]
valueIndex++
}
_, _ = i.WriteString(query)
return nil
}