-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patheditable_entity.go
330 lines (287 loc) · 6.56 KB
/
editable_entity.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
package beeorm
import (
"reflect"
)
type FlushType int
const (
Insert FlushType = iota
Update
Delete
)
type EntityFlush interface {
ID() uint64
Schema() *entitySchema
flushType() FlushType
}
type entityFlushInsert interface {
EntityFlush
getBind() (Bind, error)
getEntity() any
getValue() reflect.Value
}
type entityFlushDelete interface {
EntityFlush
getOldBind() (Bind, error)
getValue() reflect.Value
}
type entityFlushUpdate interface {
EntityFlush
getBind() (new, old, newForced, oldForced Bind, err error)
getValue() reflect.Value
getSourceValue() reflect.Value
getEntity() any
}
type EntityFlushedEvent interface {
FlushType() FlushType
}
type writableEntity struct {
orm ORM
schema *entitySchema
}
func (w *writableEntity) Schema() *entitySchema {
return w.schema
}
type insertableEntity struct {
writableEntity
entity any
id uint64
value reflect.Value
}
func (m *insertableEntity) ID() uint64 {
return m.id
}
func (m *insertableEntity) getEntity() any {
return m.entity
}
func (m *insertableEntity) flushType() FlushType {
return Insert
}
func (m *insertableEntity) getValue() reflect.Value {
return m.value
}
func (e *editableEntity) flushType() FlushType {
return Update
}
func (e *editableEntity) getValue() reflect.Value {
return e.value
}
func (e *editableEntity) getEntity() any {
return e.entity
}
func (e *editableEntity) getSourceValue() reflect.Value {
return e.sourceValue
}
type removableEntity struct {
writableEntity
id uint64
value reflect.Value
source any
}
func (r *removableEntity) flushType() FlushType {
return Delete
}
func (r *removableEntity) SourceEntity() any {
return r.source
}
func (r *removableEntity) getValue() reflect.Value {
return r.value
}
type editableEntity struct {
writableEntity
entity any
id uint64
value reflect.Value
sourceValue reflect.Value
source any
}
type editableFields struct {
writableEntity
id uint64
value reflect.Value
newBind Bind
oldBind Bind
}
func (f *editableFields) ID() uint64 {
return f.id
}
func (f *editableFields) flushType() FlushType {
return Update
}
func (f *editableFields) getBind() (new, old, forcedNew, forcedOld Bind, err error) {
forcedNew = Bind{}
forcedOld = Bind{}
uniqueIndexes := f.schema.GetUniqueIndexes()
if len(uniqueIndexes) > 0 {
for _, indexColumns := range uniqueIndexes {
if len(indexColumns) == 1 {
continue
}
indexChanged := false
for _, column := range indexColumns {
_, changed := f.newBind[column]
if changed {
indexChanged = true
break
}
}
if !indexChanged {
continue
}
for _, column := range indexColumns {
_, changed := f.newBind[column]
getter := f.schema.fieldGetters[column]
val := getter(f.value.Elem())
setter := f.schema.fieldBindSetters[column]
val, err = setter(val)
if err != nil {
return nil, nil, nil, nil, err
}
if !changed {
forcedNew[column] = val
}
forcedOld[column] = val
}
}
}
for _, def := range f.schema.cachedIndexes {
if len(def.Columns) == 1 {
continue
}
indexChanged := false
for _, indexColumn := range def.Columns {
_, has := f.newBind[indexColumn]
if has {
indexChanged = true
break
}
}
if !indexChanged {
continue
}
for _, column := range def.Columns {
_, changed := f.newBind[column]
getter := f.schema.fieldGetters[column]
val := getter(f.value.Elem())
setter := f.schema.fieldBindSetters[column]
val, err = setter(val)
if err != nil {
return nil, nil, nil, nil, err
}
if !changed {
forcedNew[column] = val
}
forcedOld[column] = val
}
}
return f.newBind, f.oldBind, forcedNew, forcedOld, nil
}
func (f *editableFields) getEntity() any {
return nil
}
func (f *editableFields) getSourceValue() reflect.Value {
return f.value
}
func (f *editableFields) getValue() reflect.Value {
return f.value
}
func (e *editableEntity) ID() uint64 {
return e.id
}
func (r *removableEntity) ID() uint64 {
return r.id
}
func (e *editableEntity) TrackedEntity() any {
return e.entity
}
func (e *editableEntity) SourceEntity() any {
return e.source
}
func NewEntity[E any](orm ORM) *E {
return newEntity(orm, getEntitySchema[E](orm)).(*E)
}
func newEntityInsertable(orm ORM, schema *entitySchema) *insertableEntity {
entity := &insertableEntity{}
entity.orm = orm
entity.schema = schema
value := reflect.New(schema.t)
elem := value.Elem()
initNewEntity(elem, schema.fields)
entity.entity = value.Interface()
id := schema.uuid(orm)
entity.id = id
elem.Field(0).SetUint(id)
entity.value = value
orm.trackEntity(entity)
return entity
}
func newEntity(orm ORM, schema *entitySchema) any {
return newEntityInsertable(orm, schema).entity
}
func DeleteEntity[E any](orm ORM, source E) {
toRemove := &removableEntity{}
toRemove.orm = orm
toRemove.source = source
toRemove.value = reflect.ValueOf(source).Elem()
toRemove.id = toRemove.value.Field(0).Uint()
schema := getEntitySchema[E](orm)
toRemove.schema = schema
orm.trackEntity(toRemove)
}
func EditEntity[E any](orm ORM, source E) E {
writable := copyToEdit(orm, source)
writable.id = writable.value.Elem().Field(0).Uint()
writable.source = source
orm.trackEntity(writable)
return writable.entity.(E)
}
func initNewEntity(elem reflect.Value, fields *tableFields) {
for k, i := range fields.stringsEnums {
def := fields.enums[k]
if def.required {
elem.Field(i).SetString(def.defaultValue)
}
}
for k, i := range fields.sliceStringsSets {
def := fields.enums[k]
if def.required {
f := elem.Field(i)
setValues := reflect.MakeSlice(f.Type(), 1, 1)
setValues.Index(0).SetString(def.defaultValue)
f.Set(setValues)
}
}
}
func IsDirty[E any, I ID](orm ORM, id I) (oldValues, newValues Bind, hasChanges bool) {
return isDirty(orm, getEntitySchema[E](orm), uint64(id))
}
func isDirty(orm ORM, schema *entitySchema, id uint64) (oldValues, newValues Bind, hasChanges bool) {
tracked := orm.(*ormImplementation).trackedEntities
if tracked == nil {
return nil, nil, false
}
if schema == nil {
return nil, nil, false
}
values, has := tracked.Load(schema.index)
if !has {
return nil, nil, false
}
row, has := values.Load(id)
if !has {
return nil, nil, false
}
editable, isUpdate := row.(entityFlushUpdate)
if !isUpdate {
insertable, isInsert := row.(entityFlushInsert)
if isInsert {
bind, _ := insertable.getBind()
return nil, bind, true
}
return nil, nil, false
}
oldValues, newValues, _, _, _ = editable.getBind()
if len(oldValues) == 0 && len(newValues) == 0 {
return nil, nil, false
}
return oldValues, newValues, true
}