-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
437 lines (424 loc) · 14.2 KB
/
query.go
File metadata and controls
437 lines (424 loc) · 14.2 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Package sqlquery provides a fluent API for building SQL queries with support for MySQL, PostgreSQL, and SQLite.
//
// The package wraps github.com/huandu/go-sqlbuilder and provides simplified query building functions
// with support for advanced filtering, pagination, and row locking.
//
// # Basic Usage
//
// Build a SELECT query with filters:
//
// options := NewFindAllOptions(MySQLFlavor).
// WithFilter("status", "active").
// WithFilter("age.gte", 18).
// WithLimit(10).
// WithOffset(0).
// WithOrderBy("created_at DESC")
// sql, args := FindAllQuery("users", options)
//
// # Filter Syntax
//
// The Filters map supports special operators via dot notation:
//
// "field" - Equality (field = value)
// "field.in" - IN clause (value must be comma-separated string)
// "field.notin" - NOT IN clause (value must be comma-separated string)
// "field.not" - Not equal (field != value)
// "field.gt" - Greater than (field > value)
// "field.gte" - Greater or equal (field >= value)
// "field.lt" - Less than (field < value)
// "field.lte" - Less or equal (field <= value)
// "field.like" - LIKE pattern matching
// "field.null" - IS NULL / IS NOT NULL (value must be bool)
//
// # Supported Database Flavors
//
// Use one of the predefined flavors when creating options:
//
// MySQLFlavor - MySQL and MariaDB
// PostgreSQLFlavor - PostgreSQL
// SQLiteFlavor - SQLite
package sqlquery
import (
"sort"
"strings"
"github.com/huandu/go-sqlbuilder"
)
// parseIn converts a comma-separated string into a slice of interface{} values
// for use in SQL IN clauses. For example, "1,2,3" becomes []interface{}{"1", "2", "3"}.
func parseIn(value string) []interface{} {
values := strings.Split(value, ",")
result := make([]interface{}, len(values))
for i := range values {
result[i] = values[i]
}
return result
}
// parseSelectFilter applies WHERE conditions to a SELECT query builder based on the filter key and value.
// It supports special filter operators via dot notation (e.g., "field.in", "field.gt", "field.like").
// See package documentation for the full list of supported operators.
func parseSelectFilter(sb *sqlbuilder.SelectBuilder, key string, value interface{}) {
if strings.Contains(key, ".") {
split := strings.Split(key, ".")
parsedKey := split[0]
compare := split[1]
switch compare {
case "in":
valueStr, ok := value.(string)
if ok {
values := parseIn(valueStr)
sb.Where(sb.In(parsedKey, values...))
}
case "notin":
valueStr, ok := value.(string)
if ok {
values := parseIn(valueStr)
sb.Where(sb.NotIn(parsedKey, values...))
}
case "not":
sb.Where(sb.NotEqual(parsedKey, value))
case "gt":
sb.Where(sb.GreaterThan(parsedKey, value))
case "gte":
sb.Where(sb.GreaterEqualThan(parsedKey, value))
case "lt":
sb.Where(sb.LessThan(parsedKey, value))
case "lte":
sb.Where(sb.LessEqualThan(parsedKey, value))
case "like":
sb.Where(sb.Like(parsedKey, value))
case "null":
valueBool, ok := value.(bool)
if ok {
if valueBool {
sb.Where(sb.IsNull(key))
} else {
sb.Where(sb.IsNotNull(key))
}
}
}
} else {
switch value.(type) {
case nil:
sb.Where(sb.IsNull(key))
default:
sb.Where(sb.Equal(key, value))
}
}
}
// parseUpdateFilter applies WHERE conditions to an UPDATE query builder based on the filter key and value.
// It supports special filter operators via dot notation (e.g., "field.in", "field.gt", "field.like").
// See package documentation for the full list of supported operators.
func parseUpdateFilter(ub *sqlbuilder.UpdateBuilder, key string, value interface{}) {
if strings.Contains(key, ".") {
split := strings.Split(key, ".")
parsedKey := split[0]
compare := split[1]
switch compare {
case "in":
valueStr, ok := value.(string)
if ok {
values := parseIn(valueStr)
ub.Where(ub.In(parsedKey, values...))
}
case "notin":
valueStr, ok := value.(string)
if ok {
values := parseIn(valueStr)
ub.Where(ub.NotIn(parsedKey, values...))
}
case "not":
ub.Where(ub.NotEqual(parsedKey, value))
case "gt":
ub.Where(ub.GreaterThan(parsedKey, value))
case "gte":
ub.Where(ub.GreaterEqualThan(parsedKey, value))
case "lt":
ub.Where(ub.LessThan(parsedKey, value))
case "lte":
ub.Where(ub.LessEqualThan(parsedKey, value))
case "like":
ub.Where(ub.Like(parsedKey, value))
case "null":
valueBool, ok := value.(bool)
if ok {
if valueBool {
ub.Where(ub.IsNull(key))
} else {
ub.Where(ub.IsNotNull(key))
}
}
}
} else {
switch value.(type) {
case nil:
ub.Where(ub.IsNull(key))
default:
ub.Where(ub.Equal(key, value))
}
}
}
// parseDeleteFilter applies WHERE conditions to a DELETE query builder based on the filter key and value.
// It supports special filter operators via dot notation (e.g., "field.in", "field.gt", "field.like").
// See package documentation for the full list of supported operators.
func parseDeleteFilter(db *sqlbuilder.DeleteBuilder, key string, value interface{}) {
if strings.Contains(key, ".") {
split := strings.Split(key, ".")
parsedKey := split[0]
compare := split[1]
switch compare {
case "in":
valueStr, ok := value.(string)
if ok {
values := parseIn(valueStr)
db.Where(db.In(parsedKey, values...))
}
case "notin":
valueStr, ok := value.(string)
if ok {
values := parseIn(valueStr)
db.Where(db.NotIn(parsedKey, values...))
}
case "not":
db.Where(db.NotEqual(parsedKey, value))
case "gt":
db.Where(db.GreaterThan(parsedKey, value))
case "gte":
db.Where(db.GreaterEqualThan(parsedKey, value))
case "lt":
db.Where(db.LessThan(parsedKey, value))
case "lte":
db.Where(db.LessEqualThan(parsedKey, value))
case "like":
db.Where(db.Like(parsedKey, value))
case "null":
valueBool, ok := value.(bool)
if ok {
if valueBool {
db.Where(db.IsNull(key))
} else {
db.Where(db.IsNotNull(key))
}
}
}
} else {
switch value.(type) {
case nil:
db.Where(db.IsNull(key))
default:
db.Where(db.Equal(key, value))
}
}
}
// FindQuery builds a SELECT query and returns the compiled SQL string and arguments.
//
// The function supports filtering with special operators, field selection, and row locking (FOR UPDATE).
//
// Parameters:
// - tableName: The name of the table to query
// - options: Configuration including flavor, fields, filters, and FOR UPDATE settings
//
// Returns the compiled SQL string and a slice of arguments for parameterized queries.
//
// Example:
//
// options := NewFindOptions(MySQLFlavor).
// WithFields([]string{"id", "name", "email"}).
// WithFilter("status", "active").
// WithFilter("age.gte", 18)
// sql, args := FindQuery("users", options)
// // SELECT id, name, email FROM users WHERE status = ? AND age >= ?
func FindQuery(tableName string, options *FindOptions) (string, []interface{}) {
sb := sqlbuilder.NewSelectBuilder()
sb.SetFlavor(sqlbuilder.Flavor(options.Flavor))
sb.Select(options.Fields...).From(tableName)
for key, value := range options.Filters {
parseSelectFilter(sb, key, value)
}
if options.ForUpdate {
sb.ForUpdate()
if options.ForUpdateMode != "" {
sb.SQL(options.ForUpdateMode)
}
}
return sb.Build()
}
// FindAllQuery builds a SELECT query with pagination and returns the compiled SQL string and arguments.
//
// This function extends FindQuery with support for LIMIT, OFFSET, and ORDER BY clauses.
//
// Parameters:
// - tableName: The name of the table to query
// - options: Configuration including flavor, fields, filters, limit, offset, ordering, and FOR UPDATE settings
//
// Returns the compiled SQL string and a slice of arguments for parameterized queries.
//
// Example:
//
// options := NewFindAllOptions(MySQLFlavor).
// WithFilter("status", "active").
// WithLimit(10).
// WithOffset(20).
// WithOrderBy("created_at DESC")
// sql, args := FindAllQuery("users", options)
// // SELECT * FROM users WHERE status = ? ORDER BY created_at DESC LIMIT 10 OFFSET 20
func FindAllQuery(tableName string, options *FindAllOptions) (string, []interface{}) {
sb := sqlbuilder.NewSelectBuilder()
sb.SetFlavor(sqlbuilder.Flavor(options.Flavor))
sb.Select(options.Fields...).From(tableName).Limit(options.Limit).Offset(options.Offset)
for key, value := range options.Filters {
parseSelectFilter(sb, key, value)
}
if options.OrderBy != "" {
sb.OrderBy(options.OrderBy)
}
if options.ForUpdate {
sb.ForUpdate()
if options.ForUpdateMode != "" {
sb.SQL(options.ForUpdateMode)
}
}
return sb.Build()
}
// InsertQuery builds an INSERT query from a struct and returns the compiled SQL string and arguments.
//
// The function uses struct tags to map struct fields to database columns. Fields are extracted
// based on the specified tag name (e.g., "db", "sql", or custom tags).
//
// Parameters:
// - flavor: The SQL dialect (MySQLFlavor, PostgreSQLFlavor, or SQLiteFlavor)
// - tag: The struct tag name to use for field mapping (e.g., "db", "sql")
// - tableName: The name of the table to insert into
// - structValue: The struct instance containing values to insert
//
// Returns the compiled SQL string and a slice of arguments for parameterized queries.
//
// Example:
//
// type User struct {
// Name string `db:"name"`
// Email string `db:"email"`
// Age int `db:"age"`
// }
// user := User{Name: "John", Email: "john@example.com", Age: 30}
// sql, args := InsertQuery(MySQLFlavor, "db", "users", user)
// // INSERT INTO users (name, email, age) VALUES (?, ?, ?)
func InsertQuery(flavor Flavor, tag, tableName string, structValue interface{}) (string, []interface{}) {
theStruct := sqlbuilder.NewStruct(structValue).For(sqlbuilder.Flavor(flavor))
ib := theStruct.WithTag(tag).InsertInto(tableName, structValue)
return ib.Build()
}
// UpdateQuery builds an UPDATE query from a struct and returns the compiled SQL string and arguments.
//
// The function updates a record identified by its ID. Struct tags determine which fields to update.
//
// Parameters:
// - flavor: The SQL dialect (MySQLFlavor, PostgreSQLFlavor, or SQLiteFlavor)
// - tag: The struct tag name to use for field mapping (e.g., "db", "sql")
// - tableName: The name of the table to update
// - id: The ID value for the WHERE id = ? condition
// - structValue: The struct instance containing updated values
//
// Returns the compiled SQL string and a slice of arguments for parameterized queries.
//
// Example:
//
// type User struct {
// Name string `db:"name"`
// Email string `db:"email"`
// Age int `db:"age"`
// }
// user := User{Name: "John", Email: "john@example.com", Age: 31}
// sql, args := UpdateQuery(MySQLFlavor, "db", "users", 123, user)
// // UPDATE users SET name = ?, email = ?, age = ? WHERE id = ?
func UpdateQuery(flavor Flavor, tag, tableName string, id interface{}, structValue interface{}) (string, []interface{}) {
theStruct := sqlbuilder.NewStruct(structValue).For(sqlbuilder.Flavor(flavor))
ub := theStruct.WithTag(tag).Update(tableName, structValue)
ub.Where(ub.Equal("id", id))
return ub.Build()
}
// DeleteQuery builds a DELETE query and returns the compiled SQL string and arguments.
//
// The function deletes a record identified by its ID.
//
// Parameters:
// - flavor: The SQL dialect (MySQLFlavor, PostgreSQLFlavor, or SQLiteFlavor)
// - tableName: The name of the table to delete from
// - id: The ID value for the WHERE id = ? condition
//
// Returns the compiled SQL string and a slice of arguments for parameterized queries.
//
// Example:
//
// sql, args := DeleteQuery(MySQLFlavor, "users", 123)
// // DELETE FROM users WHERE id = ?
func DeleteQuery(flavor Flavor, tableName string, id interface{}) (string, []interface{}) {
db := sqlbuilder.NewDeleteBuilder()
db.SetFlavor(sqlbuilder.Flavor(flavor))
db.DeleteFrom(tableName)
db.Where(db.Equal("id", id))
return db.Build()
}
// UpdateWithOptionsQuery builds an UPDATE query with custom field assignments and filters.
//
// This function provides more flexibility than UpdateQuery by allowing:
// - Custom field assignments (not limited to struct fields)
// - Multiple WHERE conditions with filter operators
// - Updating multiple records at once
//
// Parameters:
// - tableName: The name of the table to update
// - options: Configuration including flavor, field assignments, and filter conditions
//
// Returns the compiled SQL string and a slice of arguments for parameterized queries.
//
// Example:
//
// options := NewUpdateOptions(MySQLFlavor).
// WithAssignment("status", "inactive").
// WithAssignment("updated_at", time.Now()).
// WithFilter("last_login.lt", time.Now().AddDate(0, -6, 0))
// sql, args := UpdateWithOptionsQuery("users", options)
// // UPDATE users SET status = ?, updated_at = ? WHERE last_login < ?
func UpdateWithOptionsQuery(tableName string, options *UpdateOptions) (string, []interface{}) {
ub := sqlbuilder.NewUpdateBuilder()
ub.SetFlavor(sqlbuilder.Flavor(options.Flavor))
ub.Update(tableName)
var assignments []string
for key, value := range options.Assignments {
assignments = append(assignments, ub.Assign(key, value))
}
sort.Strings(assignments)
ub = ub.Set(assignments...)
for key, value := range options.Filters {
parseUpdateFilter(ub, key, value)
}
return ub.Build()
}
// DeleteWithOptionsQuery builds a DELETE query with custom filter conditions.
//
// This function provides more flexibility than DeleteQuery by allowing:
// - Multiple WHERE conditions with filter operators
// - Deleting multiple records matching complex criteria
//
// Parameters:
// - tableName: The name of the table to delete from
// - options: Configuration including flavor and filter conditions
//
// Returns the compiled SQL string and a slice of arguments for parameterized queries.
//
// Example:
//
// options := NewDeleteOptions(MySQLFlavor).
// WithFilter("status", "inactive").
// WithFilter("created_at.lt", time.Now().AddDate(-1, 0, 0))
// sql, args := DeleteWithOptionsQuery("users", options)
// // DELETE FROM users WHERE status = ? AND created_at < ?
func DeleteWithOptionsQuery(tableName string, options *DeleteOptions) (string, []interface{}) {
db := sqlbuilder.NewDeleteBuilder()
db.SetFlavor(sqlbuilder.Flavor(options.Flavor))
db.DeleteFrom(tableName)
for key, value := range options.Filters {
parseDeleteFilter(db, key, value)
}
return db.Build()
}