-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_limit_returning.go
62 lines (52 loc) · 1.56 KB
/
order_limit_returning.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
package condition
import (
"github.com/FrancoLiberali/cql/model"
"github.com/FrancoLiberali/cql/sql"
)
type OrderLimitReturning[T model.Model] struct {
query *Query[T]
orderByCalled bool
}
// Ascending specify an ascending order when updating models
//
// available for: mysql
func (olr *OrderLimitReturning[T]) Ascending(field IField) {
if olr.query.gormQuery.Dialector() != sql.MySQL {
olr.query.addError(methodError(ErrUnsupportedByDatabase, "Ascending"))
}
olr.orderByCalled = true
olr.query.order(field, false)
}
// Descending specify a descending order when updating models
//
// available for: mysql
func (olr *OrderLimitReturning[T]) Descending(field IField) {
if olr.query.gormQuery.Dialector() != sql.MySQL {
olr.query.addError(methodError(ErrUnsupportedByDatabase, "Descending"))
}
olr.orderByCalled = true
olr.query.order(field, true)
}
// Limit specify the number of models to be updated
//
// Limit conditions can be cancelled by using `Limit(-1)`
//
// available for: mysql
func (olr *OrderLimitReturning[T]) Limit(limit int) {
if olr.query.gormQuery.Dialector() != sql.MySQL {
olr.query.addError(methodError(ErrUnsupportedByDatabase, "Limit"))
}
if !olr.orderByCalled {
olr.query.addError(methodError(ErrOrderByMustBeCalled, "Limit"))
}
olr.query.gormQuery.Limit(limit)
}
// available for: postgres, sqlite, sqlserver
//
// warning: in sqlite preloads are not allowed
func (olr OrderLimitReturning[T]) Returning(dest *[]T) {
err := olr.query.gormQuery.Returning(dest)
if err != nil {
olr.query.addError(methodError(err, "Returning"))
}
}