/
entity_put.go
454 lines (409 loc) · 9.08 KB
/
entity_put.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
446
447
448
449
450
451
452
453
454
package gorb
import (
"database/sql"
"fmt"
"reflect"
"sort"
"time"
)
type childRows []rowData
type childRowStatus uint32
const (
RowRead childRowStatus = iota
RowInserted
RowUpdated
RowDeleted
RowNotModified
)
type (
entityInfo interface {
hasRow(tableNo int32, rowId int64) bool
rowUpdated(tableNo int32, rowId int64)
rowInserted(tableNo int32, rowId int64)
rowDeleted(tableNo int32, rowId int64)
rowSkipped(tableNo int32, rowId int64)
}
entityData struct {
pk int64
token uint32
updated int
inserted int
skipped int
deleted int
missed int
children childRows
}
rowData struct {
tableNo int32
pk int64
status childRowStatus
}
)
func (data *entityData) findRow(tableNo int32, rowId int64) (index int, exact bool) {
exact = false
index = sort.Search(data.children.Len(), func(i int) bool {
if data.children[i].tableNo > tableNo {
return true
}
if data.children[i].tableNo == tableNo {
return data.children[i].pk >= rowId
}
return false
})
if index >= 0 && index < data.children.Len() {
if data.children[index].tableNo == tableNo && data.children[index].pk == rowId {
exact = true
}
}
return
}
func (data *entityData) hasRow(tableNo int32, rowId int64) bool {
_, xct := data.findRow(tableNo, rowId)
return xct
}
func (data *entityData) rowUpdated(tableNo int32, rowId int64) {
idx, xct := data.findRow(tableNo, rowId)
if xct {
data.children[idx].status = RowUpdated
data.updated++
} else {
data.missed++
}
}
func (data *entityData) rowInserted(tableNo int32, rowId int64) {
if tableNo == 0 {
data.pk = rowId
}
idx, xct := data.findRow(tableNo, rowId)
if xct {
data.children[idx].status = RowInserted
data.inserted++
} else {
data.missed++
}
}
func (data *entityData) rowDeleted(tableNo int32, rowId int64) {
idx, xct := data.findRow(tableNo, rowId)
if xct {
data.children[idx].status = RowDeleted
data.deleted++
} else {
data.missed++
}
}
func (data *entityData) rowSkipped(tableNo int32, rowId int64) {
idx, xct := data.findRow(tableNo, rowId)
if xct {
data.children[idx].status = RowNotModified
data.skipped++
} else {
data.missed++
}
}
func (s childRows) Len() int {
return len(s)
}
func (s childRows) Less(i, j int) bool {
if s[i].tableNo < s[j].tableNo {
return true
}
if s[i].tableNo == s[j].tableNo {
return s[i].pk < s[j].pk
}
return false
}
func (s childRows) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (ch *ChildTable) populateData(data *entityData, pk int64) error {
var e error = nil
var rows *sql.Rows
rows, e = ch.stmts.stmtInfo.Query(pk)
if e == nil {
var rd rowData
rd.tableNo = ch.tableNo
defer rows.Close()
for rows.Next() {
e = rows.Scan(&(rd.pk))
if e != nil {
break
}
data.children = append(data.children, rd)
}
}
return e
}
func (ent *Entity) populateData(data *entityData, pk int64) error {
var e error = nil
e = ent.stmts.stmtInfo.QueryRow(pk).Scan(&((*data).pk), &((*data).token))
if e != nil {
return e
}
data.children = append(data.children, rowData{tableNo: ent.tableNo, pk: pk})
for _, ch := range ent.FlattenChildren() {
ch.populateData(data, pk)
}
return e
}
func (t *Table) storeRow(txn *sql.Tx, row reflect.Value, logger entityInfo) error {
var res sql.Result
var stmt *sql.Stmt
var e error
var pk int64
pkValue := row.FieldByIndex(t.PrimaryKey.ClassIdx)
switch pkValue.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
pk = pkValue.Int()
case reflect.Uint, reflect.Uint32, reflect.Uint64:
pk = int64(pkValue.Uint())
default:
return fmt.Errorf("Unsupported Primary Key type")
}
var isUpdate bool = pk != 0
if !t.IsPkSerial {
isUpdate = logger.hasRow(t.tableNo, pk)
}
var flds []interface{} = make([]interface{}, 0, len(t.Fields))
for _, f := range t.Fields {
if f != t.PrimaryKey {
fv := row.FieldByIndex(f.ClassIdx)
if fv.Kind() == reflect.Ptr {
if !fv.IsNil() {
fv = fv.Elem()
}
}
fvi := fv.Interface()
switch p := fvi.(type) {
case time.Time:
fvi = p.UTC()
case nil:
}
flds = append(flds, fvi)
}
}
if isUpdate {
flds = append(flds, pk)
stmt = t.stmts.stmtUpdate
if txn != nil {
stmt = txn.Stmt(stmt)
}
} else {
if !t.IsPkSerial {
flds = append([]interface{}{pk}, flds...)
}
stmt = t.stmts.stmtInsert
if txn != nil {
stmt = txn.Stmt(stmt)
}
}
res, e = stmt.Exec(flds...)
if e != nil {
return e
}
var rowsAffected int64
rowsAffected, e = res.RowsAffected()
if e != nil {
return e
}
if rowsAffected > 1 {
fmt.Errorf("Insert/Update: expected 0 or 1 row to be affected: %d", rowsAffected)
}
if isUpdate {
if rowsAffected == 0 {
logger.rowSkipped(t.tableNo, pk)
} else {
logger.rowUpdated(t.tableNo, pk)
}
} else {
pk, e = res.LastInsertId()
if e == nil {
switch pkValue.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
pkValue.SetInt(pk)
case reflect.Uint, reflect.Uint32, reflect.Uint64:
pkValue.SetUint(uint64(pk))
}
logger.rowInserted(t.tableNo, pk)
}
}
if e != nil {
return e
}
for _, child := range t.Children {
childStorage := row.FieldByIndex(child.ClassIdx)
if childStorage.IsNil() {
continue
}
var childRow reflect.Value
switch child.ChildClass.Kind() {
case reflect.Ptr:
{
childRow = childStorage.Elem()
e = child.storeChildRow(txn, childRow, pk, logger)
}
case reflect.Slice:
{
for i := 0; i < childStorage.Len(); i++ {
childRow = childStorage.Index(i)
if !childRow.IsValid() {
continue
}
if childRow.Kind() == reflect.Ptr {
if childRow.IsNil() {
continue
}
childRow = childRow.Elem()
}
e = child.storeChildRow(txn, childRow, pk, logger)
if e != nil {
break
}
}
}
case reflect.Map:
{
for _, key := range childStorage.MapKeys() {
childRow = childStorage.MapIndex(key)
if childRow.Kind() == reflect.Ptr {
childRow = childRow.Elem()
}
e = child.storeChildRow(txn, childRow, pk, logger)
if e != nil {
break
}
}
}
}
if e != nil {
break
}
}
return e
}
func (c *ChildTable) storeChildRow(txn *sql.Tx, row reflect.Value, parentId int64, logger entityInfo) error {
fkValue := row.FieldByIndex(c.ParentKey.ClassIdx)
fkKind := fkValue.Type().Kind()
if fkKind == reflect.Ptr {
if fkValue.IsNil() {
v := reflect.New(fkValue.Type().Elem())
fkValue.Set(v)
fkValue = v
}
}
switch fkKind {
case reflect.Int, reflect.Int32, reflect.Int64:
fkValue.SetInt(parentId)
case reflect.Uint, reflect.Uint32, reflect.Uint64:
fkValue.SetUint(uint64(parentId))
default:
return fmt.Errorf("Unsupported Primary Key type")
}
return c.storeRow(txn, row, logger)
}
func (conn *GorbManager) EntityPut(entity interface{}) error {
if conn.db == nil {
return fmt.Errorf("Database connection is not set")
}
var ent *Entity
eType := reflect.TypeOf(entity)
var isPtr = eType.Kind() == reflect.Ptr
if isPtr {
eType = eType.Elem()
}
ent = conn.LookupEntity(eType)
if ent == nil {
return fmt.Errorf("Unsupported entity %s", eType.Name())
}
var e error
initf, ok := entity.(interface {
OnEntitySave() (bool, error)
})
if ok {
ok, e = initf.OnEntitySave()
if !ok {
return e
}
}
var eData entityData
eValue := reflect.ValueOf(entity)
if isPtr {
eValue = eValue.Elem()
}
pkValue := eValue.FieldByIndex(ent.PrimaryKey.ClassIdx)
switch pkValue.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
eData.pk = pkValue.Int()
case reflect.Uint, reflect.Uint32, reflect.Uint64:
eData.pk = int64(pkValue.Uint())
default:
return fmt.Errorf("Unsupported Primary Key type")
}
if eData.pk != 0 {
eData.children = make([]rowData, 0, 16)
e := ent.populateData(&eData, eData.pk)
if e != nil {
return e
}
if len(eData.children) > 0 {
sort.Sort(eData.children)
}
if ent.TokenField != nil {
var token int64 = eValue.FieldByIndex(ent.TokenField.ClassIdx).Int()
if token != int64(eData.token) {
return fmt.Errorf("Invalid Edit Token")
}
}
}
var txn *sql.Tx = nil
if len(ent.Children) > 0 {
txn, e = conn.db.Begin()
if e != nil {
return e
}
}
var t *Table = &((*ent).Table)
e = t.storeRow(txn, eValue, &eData)
if e == nil {
if len(eData.children) > eData.updated+eData.skipped {
chlds := ent.FlattenChildren()
var lastTableNo int32 = 0
var stmt *sql.Stmt = nil
var res sql.Result
var rowsAffected int64
for i := len(eData.children) - 1; i >= 0; i-- {
rd := eData.children[i]
if rd.tableNo > 0 && rd.status == RowRead {
child := chlds[rd.tableNo-1]
if child.tableNo == rd.tableNo {
if lastTableNo != child.tableNo {
stmt = child.stmts.stmtRemove
if txn != nil {
stmt = txn.Stmt(stmt)
}
lastTableNo = child.tableNo
}
res, e = stmt.Exec(rd.pk)
if e != nil {
break
}
rowsAffected, e = res.RowsAffected()
if e != nil {
break
}
if rowsAffected == 1 {
(&eData).rowDeleted(rd.tableNo, rd.pk)
}
}
}
}
}
}
if e == nil && txn != nil {
if e == nil {
e = txn.Commit()
} else {
txn.Rollback()
}
}
return e
}