This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
333 lines (296 loc) · 9.04 KB
/
query.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package rushia
import (
"reflect"
)
// Copy creates a copy of the current query,
// so you are able to make changes and it won't modify the original query.
func (q *Query) Copy() *Query {
a := *q
b := a
//
b.wheres = make([]condition, len(a.wheres))
copy(b.wheres, a.wheres)
//
b.havings = make([]condition, len(a.havings))
copy(b.havings, a.havings)
//
b.queryOptions = make([]string, len(a.queryOptions))
copy(b.queryOptions, a.queryOptions)
//
b.unions = make([]union, len(a.unions))
copy(b.unions, a.unions)
//
b.selects = make([]interface{}, len(a.selects))
copy(b.selects, a.selects)
//
b.joins = make([]join, len(a.joins))
copy(b.joins, a.joins)
//
b.orders = make([]order, len(a.orders))
copy(b.orders, a.orders)
//
b.groups = make([]string, len(a.groups))
copy(b.groups, a.groups)
//
b.params = make([]interface{}, len(a.params))
copy(b.params, a.params)
//
b.omits = make([]string, len(a.omits))
copy(b.omits, a.omits)
return &b
}
// Insert creates a `INSERT INTO` query with specified data.
// It inserts a data to the database.
func (q *Query) Insert(v interface{}) *Query {
q.typ = queryTypeInsert
q.data = v
return q
}
// Replace creates a `REPLACE INTO` query with specified data.
// It deletes the original data and creates a new one instead, prettry dangerous if the data contains a foreign key.
func (q *Query) Replace(v interface{}) *Query {
q.typ = queryTypeReplace
q.data = v
return q
}
// Update creates a `UPDATE` query with specified data.
// It updates the data with new data, normally use with `WHERE` condition.
func (q *Query) Update(v interface{}) *Query {
q.typ = queryTypeUpdate
q.data = v
return q
}
// Select creates a `SELECT` query with specified columns, can be empty for select everything (`*`).
// It fetches the data from database.
func (q *Query) Select(columns ...interface{}) *Query {
q.typ = queryTypeSelect
q.selects = columns
return q
}
// SelectOne works the same as `Select` but returns only one row as result.
// It's the combination of `.Limit(1).Select()`.
func (q *Query) SelectOne(columns ...interface{}) *Query {
q.Limit(1)
q.typ = queryTypeSelect
q.selects = columns
return q
}
// Patch works the same as `Update` but ignores the zero value.
// The zero value fields won't be updated unless it's in exclude list, to define the list, call `Exclude`.
func (q *Query) Patch(v interface{}) *Query {
q.typ = queryTypePatch
q.data = v
return q
}
// Exists creates a `SELECT EXISTS` query, returns a result if the query does match a row.
func (q *Query) Exists() *Query {
q.typ = queryTypeExists
return q
}
// InsertSelect creates a `INSERT SELECT` query, it works a bit like table copy.
// The insert data is from another selection, pass a `SELECT` query to the first argument.
func (q *Query) InsertSelect(qu *Query, columns ...interface{}) *Query {
q.typ = queryTypeInsertSelect
q.subQuery = qu
q.selects = columns
return q
}
// Delete creates a `DELETE` query to delete the data.
// Make sure you are using it with `WHERE` condition to not delete all the data.
func (q *Query) Delete() *Query {
q.typ = queryTypeDelete
return q
}
// Omit omits specified fields in the data so it won't be insert/update into the database.
func (q *Query) Omit(fields ...string) *Query {
q.omits = append(q.omits, fields...)
return q
}
// OnDuplicate creates `ON DUPLICATE KEY UPDATE` query, works when inserting a duplicated data,
// the data will be automatically updated to the new value.
func (q *Query) OnDuplicate(v H) *Query {
if q.duplicate == nil {
q.duplicate = make(H)
}
for k, j := range v {
q.duplicate[k] = j
}
return q
}
// Exclude excludes the specified fields, data types while patching with `Patch` method.
// Pass string values as field names, and `reflect.Kind` as data types to exclude.
// While patching, all the zero values will be ignored unless it's in the exclude list.
func (q *Query) Exclude(fields ...interface{}) *Query {
for _, v := range fields {
switch j := v.(type) {
case reflect.Kind:
q.exclude.kinds = append(q.exclude.kinds, j)
case string:
q.exclude.fields = append(q.exclude.fields, j)
}
}
return q
}
// Limit creates `LIMIT` option to the query.
func (q *Query) Limit(from int, count ...int) *Query {
q.limit.from = from
if len(count) > 0 {
q.limit.count = count[0]
}
return q
}
// Paginate is a human-friendly `LIMIT` option.
func (q *Query) Paginate(page int, limit int) *Query {
var from int
if page == 1 {
from = 0
} else {
from = limit * (page - 1)
}
return q.Limit(from, limit)
}
// As creates an alias for current query.
func (q *Query) As(alias string) *Query {
q.alias = alias
return q
}
// Offset creates `LIMIT OFFSET` option to the query.
func (q *Query) Offset(count int, offset int) *Query {
q.offset.count = count
q.offset.offset = offset
return q
}
func (q *Query) ClearLimit() *Query {
q.offset = offset{}
q.limit = limit{}
return q
}
// Having creates a `HAVING` condition.
func (q *Query) Having(query string, args ...interface{}) *Query {
query, args = q.processEscaped(query, args...)
q.havings = append(q.havings, condition{
query: query,
args: args,
connector: connectorTypeAnd,
})
return q
}
// OrHaving creates a `HAVING OR` condition.
func (q *Query) OrHaving(query string, args ...interface{}) *Query {
query, args = q.processEscaped(query, args...)
q.havings = append(q.havings, condition{
query: query,
args: args,
connector: connectorTypeOr,
})
return q
}
// Where creates a `WHERE` condition.
func (q *Query) Where(query string, args ...interface{}) *Query {
query, args = q.processEscaped(query, args...)
q.wheres = append(q.wheres, condition{
query: query,
args: args,
connector: connectorTypeAnd,
})
return q
}
// OrWhere creates a `WHERE OR` condition.
func (q *Query) OrWhere(query string, args ...interface{}) *Query {
query, args = q.processEscaped(query, args...)
q.wheres = append(q.wheres, condition{
query: query,
args: args,
connector: connectorTypeOr,
})
return q
}
// JoinWhere creates the `AND` joining condition for latest table join.
func (q *Query) JoinWhere(query string, args ...interface{}) *Query {
query, args = q.processEscaped(query, args...)
q.joins[len(q.joins)-1].conditions = append(q.joins[len(q.joins)-1].conditions, condition{
query: query,
args: args,
connector: connectorTypeAnd,
})
return q
}
// OrJoinWhere creates the `OR` joining condition for latest table join.
func (q *Query) OrJoinWhere(query string, args ...interface{}) *Query {
query, args = q.processEscaped(query, args...)
q.joins[len(q.joins)-1].conditions = append(q.joins[len(q.joins)-1].conditions, condition{
query: query,
args: args,
connector: connectorTypeOr,
})
return q
}
// Distinct adds the `DISTINCT` option to the query.
func (q *Query) Distinct() *Query {
q.SetQueryOption("DISTINCT")
return q
}
// Union creates a `UNION` query that connects two tables.
// It groups the result from multiple tables but eliminates the duplicates.
func (q *Query) Union(qu *Query) *Query {
q.unions = append(q.unions, union{
query: qu,
})
return q
}
// UnionAll creates a `UNION ALL` query that connects two tables.
// It groups the result from multiple tables and it keeps the duplicates.
func (q *Query) UnionAll(qu *Query) *Query {
q.unions = append(q.unions, union{
query: qu,
all: true,
})
return q
}
// OrderBy creates a `ORDER BY` option to the query.
func (q *Query) OrderBy(columns ...string) *Query {
for _, v := range columns {
q.orders = append(q.orders, order{
column: v,
})
}
return q
}
// OrderByField creates a `ORDER BY FIELD` option to the query.
func (q *Query) OrderByField(field string, values ...interface{}) *Query {
q.orders = append(q.orders, order{
field: field,
values: values,
})
return q
}
// GroupBy creates a `GROUP BY` option to the query.
func (q *Query) GroupBy(columns ...string) *Query {
q.groups = append(q.groups, columns...)
return q
}
// CrossJoin creates a `CROSS JOIN` to join a table.
func (q *Query) CrossJoin(table interface{}, conditions ...interface{}) *Query {
return q.putJoin(joinTypeCross, table, conditions...)
}
// LeftJoin creates a `LEFT JOIN` to join a table.
func (q *Query) LeftJoin(table interface{}, conditions ...interface{}) *Query {
return q.putJoin(joinTypeLeft, table, conditions...)
}
// RightJoin creates a `RIGHT JOIN` to join a table.
func (q *Query) RightJoin(table interface{}, conditions ...interface{}) *Query {
return q.putJoin(joinTypeRight, table, conditions...)
}
// InnerJoin creates a `INNER JOIN` to join a table.
func (q *Query) InnerJoin(table interface{}, conditions ...interface{}) *Query {
return q.putJoin(joinTypeInner, table, conditions...)
}
// NaturalJoin creates a `NATURAL JOIN` to join a table.
func (q *Query) NaturalJoin(table interface{}, conditions ...interface{}) *Query {
return q.putJoin(joinTypeNatural, table, conditions...)
}
// SetQueryOption sets the query options, and it will be automatically be appended after or before the query.
func (q *Query) SetQueryOption(option string) *Query {
q.queryOptions = append(q.queryOptions, option)
return q
}