forked from ulule/loukoum
/
builder.go
298 lines (257 loc) · 7.06 KB
/
builder.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package builder
import (
"fmt"
"math"
"strconv"
"strings"
"github.com/ulule/loukoum/stmt"
"github.com/ulule/loukoum/types"
)
// Builder defines a generic methods available for Select, Insert, Update and Delete builders.
type Builder interface {
// String returns the underlying query as a raw statement.
// This function should be used for debugging since it doesn't escape anything and is completely
// vulnerable to SQL injection.
// You should use either NamedQuery() or Query()...
String() string
// NamedQuery returns the underlying query as a named statement.
NamedQuery() (string, map[string]interface{})
// Query returns the underlying query as a regular statement.
Query() (string, []interface{})
// Statement returns underlying statement.
Statement() stmt.Statement
}
// IsSelectBuilder returns true if given builder is of type "Select"
func IsSelectBuilder(builder Builder) bool {
_, ok := builder.(*Select)
return ok
}
// IsInsertBuilder returns true if given builder is of type "Insert"
func IsInsertBuilder(builder Builder) bool {
_, ok := builder.(*Insert)
return ok
}
// IsUpdateBuilder returns true if given builder is of type "Update"
func IsUpdateBuilder(builder Builder) bool {
_, ok := builder.(*Update)
return ok
}
// IsDeleteBuilder returns true if given builder is of type "Delete"
func IsDeleteBuilder(builder Builder) bool {
_, ok := builder.(*Delete)
return ok
}
// ToColumn takes an empty interfaces and returns a Column instance.
func ToColumn(arg interface{}) stmt.Column {
column := stmt.Column{}
switch value := arg.(type) {
case string:
column = stmt.NewColumn(strings.TrimSpace(value))
case stmt.Column:
column = value
default:
panic(fmt.Sprintf("loukoum: cannot use %T as column", arg))
}
if column.IsEmpty() {
panic("loukoum: given column is undefined")
}
return column
}
// ToColumns takes a list of empty interfaces and returns a slice of Column instance.
func ToColumns(values []interface{}) []stmt.Column { // nolint: gocyclo
// If values is a slice, we try to use recursion to obtain a slice of Column.
if len(values) == 1 {
switch array := values[0].(type) {
case []stmt.Column:
for i := range array {
if array[i].IsEmpty() {
panic("loukoum: given column is undefined")
}
}
return array
case []string:
list := make([]interface{}, len(array))
for i := range array {
list[i] = array[i]
}
return ToColumns(list)
}
}
columns := make([]stmt.Column, 0, len(values))
for i := range values {
switch value := values[i].(type) {
case string:
array := strings.Split(value, ",")
for y := range array {
column := stmt.NewColumn(strings.TrimSpace(array[y]))
if column.IsEmpty() {
panic("loukoum: given column is undefined")
}
columns = append(columns, column)
}
case stmt.Column:
if value.IsEmpty() {
panic("loukoum: given column is undefined")
}
columns = append(columns, value)
default:
panic(fmt.Sprintf("loukoum: cannot use %T as column", values[i]))
}
}
return columns
}
// ToTable takes an empty interfaces and returns a Table instance.
func ToTable(arg interface{}) stmt.Table {
table := stmt.Table{}
switch value := arg.(type) {
case string:
table = stmt.NewTable(value)
case stmt.Table:
table = value
default:
panic(fmt.Sprintf("loukoum: cannot use %T as table", arg))
}
if table.IsEmpty() {
panic("loukoum: given table is undefined")
}
return table
}
// ToTables takes a list of empty interfaces and returns a slice of Table instance.
func ToTables(values []interface{}) []stmt.Table {
tables := make([]stmt.Table, 0, len(values))
for i := range values {
tables = append(tables, ToTable(values[i]))
}
return tables
}
// ToFrom takes an empty interfaces and returns a From instance.
func ToFrom(arg interface{}) stmt.From {
from := stmt.From{}
switch value := arg.(type) {
case string:
from = stmt.NewFrom(stmt.NewTable(value), false)
case stmt.From:
from = value
case stmt.Table:
from = stmt.NewFrom(value, false)
default:
panic(fmt.Sprintf("loukoum: cannot use %T as from clause", arg))
}
if from.IsEmpty() {
panic("loukoum: given from clause is undefined")
}
return from
}
// ToInto takes an empty interfaces and returns a Into instance.
func ToInto(arg interface{}) stmt.Into {
into := stmt.Into{}
switch value := arg.(type) {
case string:
into = stmt.NewInto(stmt.NewTable(value))
case stmt.Into:
into = value
case stmt.Table:
into = stmt.NewInto(value)
default:
panic(fmt.Sprintf("loukoum: cannot use %T as into clause", arg))
}
if into.IsEmpty() {
panic("loukoum: given into clause is undefined")
}
return into
}
// ToSuffix takes an empty interfaces and returns a Suffix instance.
func ToSuffix(arg interface{}) stmt.Suffix {
suffix := stmt.Suffix{}
switch value := arg.(type) {
case string:
suffix = stmt.NewSuffix(value)
case stmt.Suffix:
suffix = value
default:
panic(fmt.Sprintf("loukoum: cannot use %T as suffix", value))
}
if suffix.IsEmpty() {
panic("loukoum: given suffix is undefined")
}
return suffix
}
// ToPrefix takes an empty interfaces and returns a Prefix instance.
func ToPrefix(arg interface{}) stmt.Prefix {
prefix := stmt.Prefix{}
switch value := arg.(type) {
case string:
prefix = stmt.NewPrefix(value)
case stmt.Prefix:
prefix = value
default:
panic(fmt.Sprintf("loukoum: cannot use %T as prefix", value))
}
if prefix.IsEmpty() {
panic("loukoum: given prefix is undefined")
}
return prefix
}
// ToInt64 takes an empty interfaces and returns a int64.
func ToInt64(value interface{}) (int64, bool) { // nolint: gocyclo
switch cast := value.(type) {
case int64:
return cast, true
case int:
return int64(cast), true
case int8:
return int64(cast), true
case int16:
return int64(cast), true
case int32:
return int64(cast), true
case uint8:
return int64(cast), true
case uint16:
return int64(cast), true
case uint32:
return int64(cast), true
case uint64:
if cast <= math.MaxInt64 {
return int64(cast), true
}
case string:
n, err := strconv.ParseInt(cast, 10, 64)
if err == nil {
return n, true
}
}
return 0, false
}
// MergeSet merges new pairs into existing ones (last write wins).
func MergeSet(set stmt.Set, args []interface{}) stmt.Set {
for i := range args {
switch value := args[i].(type) {
case string, stmt.Column:
columns := ToColumns([]interface{}{value})
for y := range columns {
set.Pairs.Set(columns[y])
}
case map[string]interface{}:
for k, v := range value {
set.Pairs.Add(ToColumn(k), stmt.NewWrapper(stmt.NewExpression(v)))
}
case types.Map:
for k, v := range value {
set.Pairs.Add(ToColumn(k), stmt.NewWrapper(stmt.NewExpression(v)))
}
case types.Pair:
set.Pairs.Add(ToColumn(value.Key), stmt.NewWrapper(stmt.NewExpression(value.Value)))
default:
panic(fmt.Sprintf("loukoum: cannot use %T as pair", value))
}
}
return set
}
// ToSet takes either a types.Map or slice of types.Pair and returns a stmt.Set instance.
func ToSet(args []interface{}) stmt.Set {
set := stmt.NewSet()
set.Pairs.Mode = stmt.PairAssociativeMode
set = MergeSet(set, args)
return set
}