-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
311 lines (280 loc) · 10.5 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
package pan
import (
"errors"
"fmt"
"strconv"
"strings"
)
const (
// FlagFull returns columns in their absolute table.column format.
FlagFull Flag = iota
// FlagTicked returns columns using ticks to quote the column name, like `column`.
FlagTicked
// FlagDoubleQuoted returns columns using double quotes to quote the column name, like "column".
FlagDoubleQuoted
)
var (
// ErrNeedsFlush is returned when a Query is used while it has expressions left in its buffer
// that haven’t been flushed using the Query’s Flush method.
ErrNeedsFlush = errors.New("Query has dangling buffer, its Flush method needs to be called")
)
// Query represents an SQL query that is being built. It can be used from its empty value,
// or it can be instantiated with the New method.
//
// Query instances are used to build SQL query string and argument lists, and consist of an
// SQL string and a buffer. The Flush method must be called before the Query is used, or you
// may leave expressions dangling in the buffer.
//
// The Query type is not meant to be concurrency-safe; if you need to modify it from multiple
// goroutines, you need to coordinate that access yourself.
type Query struct {
sql string
args []any
expressions []string
includesWhere bool
includesOrder bool
parent *Query
}
// ColumnList represents a set of columns.
type ColumnList []string
// String returns the columns in the ColumnList, joined by ", ", often used to create an
// SQL-formatted list of column names.
func (c ColumnList) String() string {
return strings.Join(c, ", ")
}
// Flag represents a modification to the returned values from our Column or Columns functions.
// See the constants defined in this package for valid values.
type Flag int
// New returns a new Query instance, primed for use.
func New(query string) *Query {
return &Query{
sql: query,
args: []any{},
}
}
// Insert returns a Query instance containing SQL that will insert the passed `values` into
// the database.
func Insert[Type SQLTableNamer](values ...Type) *Query {
columns := Columns(values[0])
query := New("INSERT INTO " + Table(values[0]) + " (" + columns.String() + ") VALUES")
for _, v := range values {
columnValues := ColumnValues(v)
query.Expression("("+Placeholders(len(columnValues))+")", columnValues...)
}
return query.Flush(", ")
}
// ErrWrongNumberArgs is returned when you’ve generated a Query with a certain number of
// placeholders, but supplied a different number of arguments. The NumExpected property
// holds the number of placeholders in the Query, and the NumFound property holds the
// number of arguments supplied.
type ErrWrongNumberArgs struct {
NumExpected int
NumFound int
}
// Error fills the error interface.
func (e ErrWrongNumberArgs) Error() string {
return fmt.Sprintf("Expected %d arguments, got %d.", e.NumExpected, e.NumFound)
}
func (q *Query) checkCounts() error {
placeholders := strings.Count(q.sql, "?")
args := len(q.args)
if placeholders != args {
return ErrWrongNumberArgs{NumExpected: placeholders, NumFound: args}
}
return nil
}
// String returns a version of your Query with all the arguments in the place of their
// placeholders. It does not do any sanitization, and is vulnerable to SQL injection.
// It is meant as a debugging aid, not to be executed. The string will almost certainly
// not be valid SQL.
func (q *Query) String() string {
var argPos int
var res string
toCheck := q.sql
for i := strings.Index(toCheck, "?"); i >= 0; argPos++ {
var arg any
arg = "!{MISSING}"
if len(q.args) > argPos {
arg = q.args[argPos]
}
res += toCheck[:i]
res += fmt.Sprintf("%v", arg)
toCheck = toCheck[i+1:]
i = strings.Index(toCheck, "?")
}
res += toCheck
return res
}
// MySQLString returns a SQL string that can be passed to MySQL to execute your query.
// If the number of placeholders do not match the number of arguments provided to your
// Query, an ErrWrongNumberArgs error will be returned. If there are still expressions
// left in the buffer (meaning the Flush method wasn't called) an ErrNeedsFlush error
// will be returned.
func (q *Query) MySQLString() (string, error) {
if len(q.expressions) != 0 {
return "", ErrNeedsFlush
}
if err := q.checkCounts(); err != nil {
return "", err
}
return q.sql + ";", nil
}
// SQLiteString returns a SQL string that can be passed to SQLite to execute
// your query. If the number of placeholders do not match the number of
// arguments provided to your Query, an ErrWrongNumberArgs error will be
// returned. If there are still expressions left in the buffer (meaning the
// Flush method wasn't called) an ErrNeedsFlush error will be returned.
func (q *Query) SQLiteString() (string, error) {
if len(q.expressions) != 0 {
return "", ErrNeedsFlush
}
if err := q.checkCounts(); err != nil {
return "", err
}
return q.sql + ";", nil
}
// PostgreSQLString returns an SQL string that can be passed to PostgreSQL to execute
// your query. If the number of placeholders do not match the number of arguments
// provided to your Query, an ErrWrongNumberArgs error will be returned. If there are
// still expressions left in the buffer (meaning the Flush method wasn't called) an
// ErrNeedsFlush error will be returned.
func (q *Query) PostgreSQLString() (string, error) {
if len(q.expressions) != 0 {
return "", ErrNeedsFlush
}
if err := q.checkCounts(); err != nil {
return "", err
}
count := 1
var res string
toCheck := q.sql
for i := strings.Index(toCheck, "?"); i >= 0; count++ {
res += toCheck[:i]
res += "$" + strconv.Itoa(count)
toCheck = toCheck[i+1:]
i = strings.Index(toCheck, "?")
}
res += toCheck
return res + ";", nil
}
// ComplexExpression starts a Query with a new buffer, so it can be flushed
// without affecting the outer Query's buffer of expressions.
//
// Once a ComplexExpression has been flushed, it should have its AppendToParent
// method called, which sets the entire ComplexExpression as a single
// expression on its parent Query.
func (q *Query) ComplexExpression(query string) *Query {
return &Query{
sql: query,
args: []any{},
parent: q,
}
}
// AppendToParent sets the entire Query as a single expression on its parent
// Query. It should only be called on Querys created by calling
// ComplexExpression; calling it on a Query that isn't a ComplexExpression will
// panic.
//
// It returns the parent of the Query.
func (q *Query) AppendToParent() *Query {
if q.parent == nil {
panic("can't call AppendToParent on a Query that wasn't created using Query.ComplexExpression")
}
if len(q.expressions) > 0 {
panic(ErrNeedsFlush)
}
if err := q.checkCounts(); err != nil {
panic(err)
}
return q.parent.Expression(q.sql, q.args...)
}
// Flush flushes the expressions in the Query’s buffer, adding them to the SQL string
// being built. It must be called before a Query can be used. Any pending expressions
// (anything since the last Flush or since the Query was instantiated) are joined using
// `join`, then added onto the Query’s SQL string, with a space between the SQL string
// and the expressions.
func (q *Query) Flush(join string) *Query {
if len(q.expressions) < 1 {
return q
}
q.sql = strings.TrimSpace(q.sql) + " "
q.sql += strings.TrimSpace(strings.Join(q.expressions, join))
q.expressions = q.expressions[0:0]
return q
}
// Expression adds a raw string and optional values to the Query’s buffer.
func (q *Query) Expression(key string, values ...any) *Query {
q.expressions = append(q.expressions, key)
q.args = append(q.args, values...)
return q
}
// Where adds a WHERE keyword to the Query’s buffer, then calls Flush on the Query,
// using a space as the join parameter.
//
// Where can only be called once per Query; calling it multiple times on the same Query
// will be no-ops after the first.
func (q *Query) Where() *Query {
if q.includesWhere {
return q
}
q.Expression("WHERE")
q.Flush(" ")
q.includesWhere = true
return q
}
// Comparison adds a comparison expression to the Query’s buffer. A comparison takes the
// form of `column operator ?`, with `value` added as an argument to the Query. Column is
// determined by finding the column name for the passed property on the passed SQLTableNamer.
// The passed property must be a string that matches, identically, the property name; if it
// does not, it will panic.
func (q *Query) Comparison(obj SQLTableNamer, property, operator string, value any) *Query {
return q.Expression(Column(obj, property)+" "+operator+" ?", value)
}
// In adds an expression to the Query’s buffer in the form of "column IN (value, value, value)".
// `values` are the variables to match against, and `obj` and `property` are used to determine
// the column. `property` must exactly match the name of a property on `obj`, or the call will
// panic.
func (q *Query) In(obj SQLTableNamer, property string, values ...any) *Query {
return q.Expression(Column(obj, property)+" IN("+Placeholders(len(values))+")", values...)
}
// Assign adds an expression to the Query’s buffer in the form of "column = ?", and adds `value`
// to the arguments for this query. `obj` and `property` are used to determine the column.
// `property` must exactly match the name of a property on `obj`, or the call will panic.
func (q *Query) Assign(obj SQLTableNamer, property string, value any) *Query {
return q.Expression(Column(obj, property)+" = ?", value)
}
func (q *Query) orderBy(orderClause, dir string) *Query {
exp := ", "
if !q.includesOrder {
exp = "ORDER BY "
q.includesOrder = true
}
q.Expression(exp + orderClause + dir)
return q
}
// OrderBy adds an expression to the Query’s buffer in the form of "ORDER BY column".
func (q *Query) OrderBy(column string) *Query {
return q.orderBy(column, "")
}
// OrderByDesc adds an expression to the Query’s buffer in the form of "ORDER BY column DESC".
func (q *Query) OrderByDesc(column string) *Query {
return q.orderBy(column, " DESC")
}
// Limit adds an expression to the Query’s buffer in the form of "LIMIT ?", and adds `limit` as
// an argument to the Query.
func (q *Query) Limit(limit int64) *Query {
return q.Expression("LIMIT ?", limit)
}
// Offset adds an expression to the Query’s buffer in the form of "OFFSET ?", and adds `offset`
// as an argument to the Query.
func (q *Query) Offset(offset int64) *Query {
return q.Expression("OFFSET ?", offset)
}
// Args returns a slice of the arguments attached to the Query, which should be used when executing
// your SQL to fill the placeholders.
//
// Note that Args returns its internal slice; you should copy the returned slice over before modifying
// it.
func (q *Query) Args() []any {
return q.args
}