forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 3
/
query.go
317 lines (264 loc) · 7.09 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
package queries
import (
"context"
"database/sql"
"fmt"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/drivers"
)
// joinKind is the type of join
type joinKind int
// Join type constants
const (
JoinInner joinKind = iota
JoinOuterLeft
JoinOuterRight
JoinNatural
)
// Query holds the state for the built up query
type Query struct {
dialect *drivers.Dialect
rawSQL rawSQL
load []string
loadMods map[string]Applicator
delete bool
update map[string]interface{}
selectCols []string
count bool
from []string
joins []join
where []where
in []in
groupBy []string
orderBy []string
having []having
limit int
offset int
forlock string
}
// Applicator exists only to allow
// query mods into the query struct around
// eager loaded relationships.
type Applicator interface {
Apply(*Query)
}
type where struct {
clause string
orSeparator bool
args []interface{}
}
type in struct {
clause string
orSeparator bool
args []interface{}
}
type having struct {
clause string
args []interface{}
}
type rawSQL struct {
sql string
args []interface{}
}
type join struct {
kind joinKind
clause string
args []interface{}
}
// Raw makes a raw query, usually for use with bind
func Raw(query string, args ...interface{}) *Query {
return &Query{
rawSQL: rawSQL{
sql: query,
args: args,
},
}
}
// RawG makes a raw query using the global boil.Executor, usually for use with bind
func RawG(query string, args ...interface{}) *Query {
return Raw(query, args...)
}
// Exec executes a query that does not need a row returned
func (q *Query) Exec(exec boil.Executor) (sql.Result, error) {
qs, args := BuildQuery(q)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, qs)
fmt.Fprintln(boil.DebugWriter, args)
}
return exec.Exec(qs, args...)
}
// QueryRow executes the query for the One finisher and returns a row
func (q *Query) QueryRow(exec boil.Executor) *sql.Row {
qs, args := BuildQuery(q)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, qs)
fmt.Fprintln(boil.DebugWriter, args)
}
return exec.QueryRow(qs, args...)
}
// Query executes the query for the All finisher and returns multiple rows
func (q *Query) Query(exec boil.Executor) (*sql.Rows, error) {
qs, args := BuildQuery(q)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, qs)
fmt.Fprintln(boil.DebugWriter, args)
}
return exec.Query(qs, args...)
}
// ExecContext executes a query that does not need a row returned
func (q *Query) ExecContext(ctx context.Context, exec boil.ContextExecutor) (sql.Result, error) {
qs, args := BuildQuery(q)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, qs)
fmt.Fprintln(boil.DebugWriter, args)
}
return exec.ExecContext(ctx, qs, args...)
}
// QueryRowContext executes the query for the One finisher and returns a row
func (q *Query) QueryRowContext(ctx context.Context, exec boil.ContextExecutor) *sql.Row {
qs, args := BuildQuery(q)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, qs)
fmt.Fprintln(boil.DebugWriter, args)
}
return exec.QueryRowContext(ctx, qs, args...)
}
// QueryContext executes the query for the All finisher and returns multiple rows
func (q *Query) QueryContext(ctx context.Context, exec boil.ContextExecutor) (*sql.Rows, error) {
qs, args := BuildQuery(q)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, qs)
fmt.Fprintln(boil.DebugWriter, args)
}
return exec.QueryContext(ctx, qs, args...)
}
// ExecP executes a query that does not need a row returned
// It will panic on error
func (q *Query) ExecP(exec boil.Executor) sql.Result {
res, err := q.Exec(exec)
if err != nil {
panic(boil.WrapErr(err))
}
return res
}
// QueryP executes the query for the All finisher and returns multiple rows
// It will panic on error
func (q *Query) QueryP(exec boil.Executor) *sql.Rows {
rows, err := q.Query(exec)
if err != nil {
panic(boil.WrapErr(err))
}
return rows
}
// SetDialect on the query.
func SetDialect(q *Query, dialect *drivers.Dialect) {
q.dialect = dialect
}
// SetSQL on the query.
func SetSQL(q *Query, sql string, args ...interface{}) {
q.rawSQL = rawSQL{sql: sql, args: args}
}
// SetArgs is primarily for re-use of a query so that the
// query text does not need to be re-generated, useful
// if you're performing the same query with different arguments
// over and over.
func SetArgs(q *Query, args ...interface{}) {
q.rawSQL.args = args
}
// SetLoad on the query.
func SetLoad(q *Query, relationships ...string) {
q.load = append([]string(nil), relationships...)
}
// AppendLoad on the query.
func AppendLoad(q *Query, relationships string) {
q.load = append(q.load, relationships)
}
// SetLoadMods on the query.
func SetLoadMods(q *Query, rel string, appl Applicator) {
if q.loadMods == nil {
q.loadMods = make(map[string]Applicator)
}
q.loadMods[rel] = appl
}
// SetSelect on the query.
func SetSelect(q *Query, sel []string) {
q.selectCols = sel
}
// GetSelect from the query
func GetSelect(q *Query) []string {
return q.selectCols
}
// SetCount on the query.
func SetCount(q *Query) {
q.count = true
}
// SetDelete on the query.
func SetDelete(q *Query) {
q.delete = true
}
// SetLimit on the query.
func SetLimit(q *Query, limit int) {
q.limit = limit
}
// SetOffset on the query.
func SetOffset(q *Query, offset int) {
q.offset = offset
}
// SetFor on the query.
func SetFor(q *Query, clause string) {
q.forlock = clause
}
// SetUpdate on the query.
func SetUpdate(q *Query, cols map[string]interface{}) {
q.update = cols
}
// AppendSelect on the query.
func AppendSelect(q *Query, columns ...string) {
q.selectCols = append(q.selectCols, columns...)
}
// AppendFrom on the query.
func AppendFrom(q *Query, from ...string) {
q.from = append(q.from, from...)
}
// SetFrom replaces the current from statements.
func SetFrom(q *Query, from ...string) {
q.from = append([]string(nil), from...)
}
// AppendInnerJoin on the query.
func AppendInnerJoin(q *Query, clause string, args ...interface{}) {
q.joins = append(q.joins, join{clause: clause, kind: JoinInner, args: args})
}
// AppendHaving on the query.
func AppendHaving(q *Query, clause string, args ...interface{}) {
q.having = append(q.having, having{clause: clause, args: args})
}
// AppendWhere on the query.
func AppendWhere(q *Query, clause string, args ...interface{}) {
q.where = append(q.where, where{clause: clause, args: args})
}
// AppendIn on the query.
func AppendIn(q *Query, clause string, args ...interface{}) {
q.in = append(q.in, in{clause: clause, args: args})
}
// SetLastWhereAsOr sets the or separator for the tail "WHERE" in the slice
func SetLastWhereAsOr(q *Query) {
if len(q.where) == 0 {
return
}
q.where[len(q.where)-1].orSeparator = true
}
// SetLastInAsOr sets the or separator for the tail "IN" in the slice
func SetLastInAsOr(q *Query) {
if len(q.in) == 0 {
return
}
q.in[len(q.in)-1].orSeparator = true
}
// AppendGroupBy on the query.
func AppendGroupBy(q *Query, clause string) {
q.groupBy = append(q.groupBy, clause)
}
// AppendOrderBy on the query.
func AppendOrderBy(q *Query, clause string) {
q.orderBy = append(q.orderBy, clause)
}