-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
186 lines (149 loc) · 4.06 KB
/
update.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
package condition
import (
"gorm.io/gorm"
"github.com/FrancoLiberali/cql/model"
"github.com/FrancoLiberali/cql/sql"
)
type Update[T model.Model] struct {
OrderLimitReturning[T]
}
// Set allows updating multiple attributes of the same table.
func (update *Update[T]) Set(sets ...*Set[T]) (int64, error) {
setsAsInterface := []ISet{}
for _, set := range sets {
setsAsInterface = append(setsAsInterface, set)
}
return update.unsafeSet(setsAsInterface, "Set")
}
// SetMultiple allows updating multiple tables in the same query.
//
// available for: mysql
func (update *Update[T]) SetMultiple(sets ...ISet) (int64, error) {
methodName := "SetMultiple"
if update.query.gormQuery.Dialector() != sql.MySQL {
update.query.addError(methodError(ErrUnsupportedByDatabase, methodName))
}
return update.unsafeSet(sets, methodName)
}
func (update *Update[T]) unsafeSet(sets []ISet, methodName string) (int64, error) {
if update.query.err != nil {
return 0, update.query.err
}
updated, err := update.query.gormQuery.Update(sets)
if err != nil {
return 0, methodError(err, methodName)
}
return updated, nil
}
// Ascending specify an ascending order when updating models
//
// available for: mysql
func (update *Update[T]) Ascending(field IField) *Update[T] {
update.OrderLimitReturning.Ascending(field)
return update
}
// Descending specify a descending order when updating models
//
// available for: mysql
func (update *Update[T]) Descending(field IField) *Update[T] {
update.OrderLimitReturning.Descending(field)
return update
}
// Limit specify the number of models to be updated
//
// Limit conditions can be cancelled by using `Limit(-1)`
//
// available for: mysql
func (update *Update[T]) Limit(limit int) *Update[T] {
update.OrderLimitReturning.Limit(limit)
return update
}
// available for: postgres, sqlite, sqlserver
//
// warning: in sqlite preloads are not allowed
func (update *Update[T]) Returning(dest *[]T) *Update[T] {
update.OrderLimitReturning.Returning(dest)
return update
}
// Create a Update to which the conditions are applied inside transaction tx
func NewUpdate[T model.Model](tx *gorm.DB, conditions []Condition[T]) *Update[T] {
var err error
if len(conditions) == 0 {
err = methodError(ErrEmptyConditions, "Update")
}
query := NewQuery(tx, conditions...)
if err != nil {
query.err = err
}
return &Update[T]{
OrderLimitReturning: OrderLimitReturning[T]{
query: query,
orderByCalled: false,
},
}
}
type ISet interface {
getField() IField
getValue() any
}
type Set[T model.Model] struct {
field IField
value any
}
func (set Set[T]) getField() IField {
return set.field
}
func (set Set[T]) getValue() any {
return set.value
}
type FieldSet[TModel model.Model, TAttribute any] struct {
field UpdatableField[TModel, TAttribute]
}
func (set FieldSet[TModel, TAttribute]) Eq(value TAttribute) *Set[TModel] {
return &Set[TModel]{
field: set.field,
value: value,
}
}
func (set FieldSet[TModel, TAttribute]) Dynamic(value ValueOfType[TAttribute]) *Set[TModel] {
return &Set[TModel]{
field: set.field,
value: value,
}
}
func (set FieldSet[TModel, TAttribute]) Unsafe(value any) *Set[TModel] {
return &Set[TModel]{
field: set.field,
value: value,
}
}
type NullableFieldSet[TModel model.Model, TAttribute any] struct {
FieldSet[TModel, TAttribute]
}
func (set NullableFieldSet[TModel, TAttribute]) Null() *Set[TModel] {
return &Set[TModel]{
field: set.field,
value: nil,
}
}
type NumericFieldSet[TModel model.Model, TAttribute int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64] struct {
field NumericField[TModel, TAttribute]
}
func (set NumericFieldSet[TModel, TAttribute]) Eq(value TAttribute) *Set[TModel] {
return &Set[TModel]{
field: set.field,
value: value,
}
}
func (set NumericFieldSet[TModel, TAttribute]) Dynamic(value ValueOfType[numeric]) *Set[TModel] {
return &Set[TModel]{
field: set.field,
value: value,
}
}
func (set NumericFieldSet[TModel, TAttribute]) Unsafe(value any) *Set[TModel] {
return &Set[TModel]{
field: set.field,
value: value,
}
}