-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm_query.go
445 lines (357 loc) · 11 KB
/
gorm_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
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
438
439
440
441
442
443
444
445
package condition
import (
"fmt"
"reflect"
"strings"
"sync"
"time"
"github.com/elliotchance/pie/v2"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"github.com/FrancoLiberali/cql/model"
"github.com/FrancoLiberali/cql/sql"
)
type GormQuery struct {
GormDB *gorm.DB
ConcernedModels map[reflect.Type][]Table
initialTable Table
}
// Order specify order when retrieving models from database.
//
// if descending is true, the ordering is in descending direction.
func (query *GormQuery) Order(field IField, descending bool) error {
table, err := query.GetModelTable(field)
if err != nil {
return err
}
switch query.Dialector() {
case sql.Postgres:
// postgres supports only order by selected fields
query.AddSelect(table, field)
query.GormDB = query.GormDB.Order(
clause.OrderByColumn{
Column: clause.Column{
Name: query.getSelectAlias(table, field),
},
Desc: descending,
},
)
return nil
case sql.SQLServer, sql.SQLite, sql.MySQL:
query.GormDB = query.GormDB.Order(
clause.OrderByColumn{
Column: clause.Column{
Name: field.columnSQL(
query,
table,
),
},
Desc: descending,
},
)
return nil
}
return nil
}
// Offset specify the number of records to skip before starting to return the records
//
// Offset conditions can be cancelled by using `Offset(-1)`.
func (query *GormQuery) Offset(offset int) {
query.GormDB = query.GormDB.Offset(offset)
}
// Limit specify the number of records to be retrieved
//
// Limit conditions can be cancelled by using `Limit(-1)`
func (query *GormQuery) Limit(limit int) {
query.GormDB = query.GormDB.Limit(limit)
}
// Count returns the amount of models that fulfill the conditions
func (query *GormQuery) Count() (int64, error) {
query.cleanSelects()
var count int64
return count, query.GormDB.Count(&count).Error
}
// First finds the first record ordered by primary key, matching given conditions
func (query *GormQuery) First(dest any) error {
return query.GormDB.First(dest).Error
}
// Take finds the first record returned by the database in no specified order, matching given conditions
func (query *GormQuery) Take(dest any) error {
return query.GormDB.Take(dest).Error
}
// Last finds the last record ordered by primary key, matching given conditions
func (query *GormQuery) Last(dest any) error {
return query.GormDB.Last(dest).Error
}
// Find finds all models matching given conditions
func (query *GormQuery) Find(dest any) error {
return query.GormDB.Find(dest).Error
}
func (query *GormQuery) AddSelect(table Table, fieldID IField) {
query.GormDB.Statement.Selects = append(
query.GormDB.Statement.Selects,
fmt.Sprintf(
"%s.%s AS %s",
table.Alias,
fieldID.columnName(query, table),
query.getSelectAlias(table, fieldID),
),
)
}
func (query *GormQuery) getSelectAlias(table Table, fieldID IField) string {
return fmt.Sprintf(
"\"%[1]s__%[2]s\"", // name used by gorm to load the fields inside the models
table.Alias,
fieldID.columnName(query, table),
)
}
func (query *GormQuery) Preload(preloadQuery string, args ...interface{}) {
query.GormDB = query.GormDB.Preload(preloadQuery, args...)
}
func (query *GormQuery) Unscoped() {
query.GormDB = query.GormDB.Unscoped()
}
func (query *GormQuery) Where(whereQuery interface{}, args ...interface{}) {
query.GormDB = query.GormDB.Where(whereQuery, args...)
}
func (query *GormQuery) Joins(joinQuery string, isLeftJoin bool, args ...interface{}) {
if isLeftJoin {
query.GormDB = query.GormDB.Joins("LEFT JOIN "+joinQuery, args...)
} else {
query.GormDB = query.GormDB.InnerJoins("INNER JOIN "+joinQuery, args...)
}
}
func (query *GormQuery) AddConcernedModel(model model.Model, table Table) {
tableList, isPresent := query.ConcernedModels[reflect.TypeOf(model)]
if !isPresent {
query.ConcernedModels[reflect.TypeOf(model)] = []Table{table}
} else {
tableList = append(tableList, table)
query.ConcernedModels[reflect.TypeOf(model)] = tableList
}
}
func (query *GormQuery) GetTables(modelType reflect.Type) []Table {
tableList, isPresent := query.ConcernedModels[modelType]
if !isPresent {
return nil
}
return tableList
}
const undefinedAppearance = -1
func (query *GormQuery) GetModelTable(field IField) (Table, error) {
modelTables := query.GetTables(field.getModelType())
if modelTables == nil {
return Table{}, fieldModelNotConcernedError(field)
}
if len(modelTables) == 1 {
return modelTables[0], nil
}
appearance := field.getAppearance()
if appearance == undefinedAppearance {
return Table{}, appearanceMustBeSelectedError(field)
}
if appearance > len(modelTables)-1 {
return Table{}, appearanceOutOfRangeError(field)
}
return modelTables[appearance], nil
}
func (query GormQuery) ColumnName(table Table, fieldName string) string {
return query.GormDB.NamingStrategy.ColumnName(table.Name, fieldName)
}
func (query GormQuery) Dialector() sql.Dialector {
return sql.Dialector(query.GormDB.Dialector.Name())
}
func NewGormQuery(db *gorm.DB, initialModel model.Model, initialTable Table) *GormQuery {
query := &GormQuery{
GormDB: db.Model(&initialModel).Select(initialTable.Name + ".*"),
ConcernedModels: map[reflect.Type][]Table{},
initialTable: initialTable,
}
query.AddConcernedModel(initialModel, initialTable)
return query
}
// Get the name of the table in "db" in which the data for "entity" is saved
// returns error is table name can not be found by gorm,
// probably because the type of "entity" is not registered using AddModel
func getTableName(db *gorm.DB, entity any) (string, error) {
schemaName, err := schema.Parse(entity, &sync.Map{}, db.NamingStrategy)
if err != nil {
return "", err
}
return schemaName.Table, nil
}
// available for: postgres, sqlite, sqlserver
//
// warning: in sqlite, sqlserver preloads are not allowed
func (query *GormQuery) Returning(dest any) error {
query.GormDB.Model(dest)
switch query.Dialector() {
case sql.Postgres: // support RETURNING from any table
columns := []clause.Column{}
for _, selectClause := range query.GormDB.Statement.Selects {
selectSplit := strings.Split(selectClause, ".")
columns = append(columns, clause.Column{
Table: selectSplit[0],
Name: selectSplit[1],
Raw: true,
})
}
query.GormDB.Clauses(clause.Returning{Columns: columns})
case sql.SQLite, sql.SQLServer: // supports RETURNING only from main table
if len(query.GormDB.Statement.Selects) > 1 {
return preloadsInReturningNotAllowed(query.Dialector())
}
query.GormDB.Clauses(clause.Returning{})
case sql.MySQL: // RETURNING not supported
return ErrUnsupportedByDatabase
}
return nil
}
func (query *GormQuery) cleanSelects() {
query.GormDB.Statement.Selects = []string{}
}
// Find finds all models matching given conditions
func (query *GormQuery) Update(sets []ISet) (int64, error) {
tablesAndValues, err := getUpdateTablesAndValues(query, sets)
if err != nil {
return 0, err
}
updateMap := map[string]any{}
query.cleanSelects()
switch query.Dialector() {
case sql.Postgres, sql.SQLServer, sql.SQLite: // support UPDATE SET FROM
for field, tableAndValue := range tablesAndValues {
updateMap[field.columnName(query, tableAndValue.table)] = tableAndValue.value
}
query.joinsToFrom()
case sql.MySQL: // support UPDATE JOIN SET
// if at least one join is done,
// allow UPDATE without WHERE as the condition can be the join
if len(query.GormDB.Statement.Joins) > 0 {
query.GormDB.AllowGlobalUpdate = true
}
sets := clause.Set{}
updatedTables := []Table{}
for field, tableAndValue := range tablesAndValues {
sets = append(sets, clause.Assignment{
Column: clause.Column{
Name: field.columnName(query, tableAndValue.table),
Table: tableAndValue.table.SQLName(),
},
Value: tableAndValue.value,
})
updatedTables = append(updatedTables, tableAndValue.table)
}
now := time.Now()
for _, table := range pie.Unique(updatedTables) {
sets = append(sets, clause.Assignment{
Column: clause.Column{
Name: "updated_at",
Table: table.SQLName(),
},
Value: now,
})
}
query.GormDB.Clauses(sets)
}
update := query.GormDB.Updates(updateMap)
return update.RowsAffected, update.Error
}
func (query *GormQuery) joinsToFrom() {
joinTables := []clause.Table{}
for _, join := range query.GormDB.Statement.Joins {
tableName, tableAlias, onStatement := splitJoin(join.Name)
joinTables = append(joinTables, clause.Table{
Name: tableName,
Alias: tableAlias,
Raw: true, // prevent gorm from putting the alias in quotes
})
query.GormDB = query.GormDB.Where(onStatement, join.Conds...)
}
if len(joinTables) > 0 {
query.GormDB.Clauses(
clause.From{
Tables: joinTables,
},
)
}
query.GormDB.Statement.Joins = nil
}
type TableAndValue struct {
table Table
value any
}
func getUpdateTablesAndValues(query *GormQuery, sets []ISet) (map[IField]TableAndValue, error) {
tables := map[IField]TableAndValue{}
for _, set := range sets {
field := set.getField()
table, err := query.GetModelTable(field)
if err != nil {
return nil, err
}
updateValue, err := getUpdateValue(query, set)
if err != nil {
return nil, err
}
_, fieldAlreadyPresent := tables[field]
if fieldAlreadyPresent {
return nil, fieldIsRepeatedError(field)
}
tables[field] = TableAndValue{
table: table,
value: updateValue,
}
}
return tables, nil
}
func getUpdateValue(query *GormQuery, set ISet) (any, error) {
value := set.getValue()
if iValue, isIValue := value.(IValue); isIValue {
table, err := query.GetModelTable(iValue.getField())
if err != nil {
return nil, err
}
valueSQL, valueValues, err := iValue.toSQL(query, table)
if err != nil {
return nil, err
}
return gorm.Expr(valueSQL, valueValues...), nil
}
return value, nil
}
// Splits a JOIN statement into the table name, table alias and ON statement
func splitJoin(joinStatement string) (string, string, string) {
// remove INNER JOIN and LEFT JOIN
joinStatement = strings.ReplaceAll(joinStatement, "INNER JOIN ", "")
joinStatement = strings.ReplaceAll(joinStatement, "LEFT JOIN ", "")
// divide table and on statement
joinStatementSplit := strings.Split(joinStatement, " ON ")
table := joinStatementSplit[0]
onStatement := joinStatementSplit[1]
// divide table name and alias
tableSplit := strings.Split(table, " ")
return tableSplit[0], tableSplit[1], onStatement
}
func (query *GormQuery) Delete() (int64, error) {
switch query.Dialector() {
case sql.Postgres, sql.SQLServer, sql.SQLite: // support UPDATE SET FROM
query.joinsToFrom()
case sql.MySQL:
// if at least one join is done,
// allow UPDATE without WHERE as the condition can be the join
if len(query.GormDB.Statement.Joins) > 0 {
query.GormDB.AllowGlobalUpdate = true
}
query.GormDB.Clauses(clause.Set{clause.Assignment{
Column: clause.Column{
Name: "deleted_at",
Table: query.initialTable.SQLName(),
},
Value: time.Now(),
}})
}
query.GormDB.Statement.Selects = []string{}
deleteTx := query.GormDB.Delete(query.GormDB.Statement.Model)
return deleteTx.RowsAffected, deleteTx.Error
}