forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 3
/
query_mods.go
145 lines (125 loc) · 4.09 KB
/
query_mods.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
package qm
import "github.com/volatiletech/sqlboiler/queries"
// QueryMod to modify the query object
type QueryMod func(q *queries.Query)
// Apply the query mods to the Query object
func Apply(q *queries.Query, mods ...QueryMod) {
for _, mod := range mods {
mod(q)
}
}
// SQL allows you to execute a plain SQL statement
func SQL(sql string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.SetSQL(q, sql, args...)
}
}
// Load allows you to specify foreign key relationships to eager load
// for your query. Passed in relationships need to be in the format
// MyThing or MyThings.
// Relationship name plurality is important, if your relationship is
// singular, you need to specify the singular form and vice versa.
func Load(relationships ...string) QueryMod {
return func(q *queries.Query) {
queries.AppendLoad(q, relationships...)
}
}
// InnerJoin on another table
func InnerJoin(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendInnerJoin(q, clause, args...)
}
}
// Select specific columns opposed to all columns
func Select(columns ...string) QueryMod {
return func(q *queries.Query) {
queries.AppendSelect(q, columns...)
}
}
// Where allows you to specify a where clause for your statement
func Where(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendWhere(q, clause, args...)
}
}
// And allows you to specify a where clause separated by an AND for your statement
// And is a duplicate of the Where function, but allows for more natural looking
// query mod chains, for example: (Where("a=?"), And("b=?"), Or("c=?")))
func And(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendWhere(q, clause, args...)
}
}
// Or allows you to specify a where clause separated by an OR for your statement
func Or(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendWhere(q, clause, args...)
queries.SetLastWhereAsOr(q)
}
}
// WhereIn allows you to specify a "x IN (set)" clause for your where statement
// Example clauses: "column in ?", "(column1,column2) in ?"
func WhereIn(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendIn(q, clause, args...)
}
}
// AndIn allows you to specify a "x IN (set)" clause separated by an AndIn
// for your where statement. AndIn is a duplicate of the WhereIn function, but
// allows for more natural looking query mod chains, for example:
// (WhereIn("column1 in ?"), AndIn("column2 in ?"), OrIn("column3 in ?"))
func AndIn(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendIn(q, clause, args...)
}
}
// OrIn allows you to specify an IN clause separated by
// an OR for your where statement
func OrIn(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendIn(q, clause, args...)
queries.SetLastInAsOr(q)
}
}
// GroupBy allows you to specify a group by clause for your statement
func GroupBy(clause string) QueryMod {
return func(q *queries.Query) {
queries.AppendGroupBy(q, clause)
}
}
// OrderBy allows you to specify a order by clause for your statement
func OrderBy(clause string) QueryMod {
return func(q *queries.Query) {
queries.AppendOrderBy(q, clause)
}
}
// Having allows you to specify a having clause for your statement
func Having(clause string, args ...interface{}) QueryMod {
return func(q *queries.Query) {
queries.AppendHaving(q, clause, args...)
}
}
// From allows to specify the table for your statement
func From(from string) QueryMod {
return func(q *queries.Query) {
queries.AppendFrom(q, from)
}
}
// Limit the number of returned rows
func Limit(limit int) QueryMod {
return func(q *queries.Query) {
queries.SetLimit(q, limit)
}
}
// Offset into the results
func Offset(offset int) QueryMod {
return func(q *queries.Query) {
queries.SetOffset(q, offset)
}
}
// For inserts a concurrency locking clause at the end of your statement
func For(clause string) QueryMod {
return func(q *queries.Query) {
queries.SetFor(q, clause)
}
}