-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_funcs.go
220 lines (194 loc) · 4.42 KB
/
worker_funcs.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
package query
import (
"reflect"
"sort"
"strconv"
"time"
)
//withMap returns a string composed of values from mapper
//if parenthesize is true, each value in mapper is parenthesized before
//adding it to qry, prefix allows for different statements(like WHERE or VALUES) to be specified
func withMap(prefix string, mapper map[int]interface{}, parenthesize bool) string {
if mapper == nil {
return ""
}
qry := ""
if prefix == "," {
qry = prefix
} else {
qry = " " + prefix
}
keys := make([]int, 0, len(mapper))
for k := range mapper {
keys = append(keys, k)
}
sort.Ints(keys)
l := len(keys) - 1
if parenthesize {
for ix, key := range keys {
if ix == l {
qry += "(" + stringifyNoQuote(mapper[key]) + ")"
break
}
qry += "(" + stringifyNoQuote(mapper[key]) + "),"
}
return qry
}
for ix, key := range keys {
if ix == l {
qry += " " + stringifyNoQuote(mapper[key])
break
}
qry += " " + stringifyNoQuote(mapper[key])
}
return qry
}
func withSetMap(prefix string, mapper map[int]interface{}) string {
if mapper == nil {
return ""
}
qry := " SET "
keys := make([]int, 0, len(mapper))
for k := range mapper {
keys = append(keys, k)
}
sort.Ints(keys)
l := len(keys) - 1
for ix, key := range keys {
if ix == l {
qry += stringifyNoQuote(mapper[key])
break
}
qry += stringifyNoQuote(mapper[key]) + ","
}
return qry
}
// values mutates all values in mapper to a one string,
// with commas seperating each value.
// obsolete keys will be deleted and the only the first index
// will remain
// mapper only supports pointers to int,uint types and the string data type.
func values(mapper map[int]interface{}) string {
if mapper == nil {
return ""
}
qry := ""
keys := make([]int, 0, len(mapper))
for k := range mapper {
keys = append(keys, k)
}
sort.Ints(keys)
l := len(keys) - 1
for ix, key := range keys {
if ix == l {
qry += stringifyQuote(mapper[key])
break
}
qry += stringifyQuote(mapper[key]) + ","
}
return qry
}
//whereIn adds a WHERE clause along with IN keyword with values derived from
//the values parameter
func whereIn(field string, values ...interface{}) string {
if values == nil {
return ""
}
qry := " WHERE " + field + " IN("
l := len(values) - 1
for ix, v := range values {
if ix == l {
qry += stringifyQuote(v)
break
}
qry += stringifyQuote(v) + ","
}
qry += ")"
return qry
}
func addFields(prefix string, parenthesize bool, fields ...string) string {
qry := prefix + " "
if parenthesize {
qry += "("
for i, field := range fields {
if i == len(fields)-1 {
qry += field
break
}
qry += field + ","
}
qry += ")"
return qry
}
for i, field := range fields {
if i == len(fields)-1 {
qry += field
break
}
qry += field + ","
}
return qry
}
//offset adds an OFFSET clause,
//only if num is a number
func offset(num uint64) string {
return " OFFSET " + strconv.FormatUint(num, 10)
}
func limit(num uint64) string {
return " LIMIT " + strconv.FormatUint(num, 10)
}
func where(cond string) string {
qry := " WHERE " + cond
return qry
}
func and(cond string) string {
return " AND " + cond
}
func or(cond string) string {
return " OR " + cond
}
// stringer allows us to avoid importing fmt
type stringer interface {
String() string
}
func stringifyNoQuote(i interface{}) string {
switch i.(type) {
case string:
return i.(string)
case *string:
return *i.(*string)
case *time.Time:
return "'" + i.(*time.Time).UTC().Format(time.RFC3339) + "'"
case time.Time:
return "'" + i.(time.Time).UTC().Format(time.RFC3339) + "'"
case stringer:
return i.(stringer).String()
}
return valueString(reflect.ValueOf(i))
}
func stringifyQuote(i interface{}) string {
switch i.(type) {
case string:
return "'" + i.(string) + "'"
case *string:
return "'" + *i.(*string) + "'"
case *time.Time:
return "'" + i.(*time.Time).UTC().Format(time.RFC3339) + "'"
case time.Time:
return "'" + i.(time.Time).UTC().Format(time.RFC3339) + "'"
case stringer:
return "'" + i.(stringer).String() + "'"
}
return valueString(reflect.ValueOf(i))
}
func valueString(v reflect.Value) string {
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
return valueString(v.Elem())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(v.Uint(), 10)
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10)
}
return ""
}