forked from samonzeweb/godb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_update.go
108 lines (92 loc) · 3.24 KB
/
struct_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
package godb
import (
"fmt"
"github.com/samonzeweb/godb/adapters"
)
// StructUpdate builds an UPDATE statement for the given object.
//
// Example (book is a struct instance):
//
// count, err := db.Update(&book).Do()
//
type StructUpdate struct {
error error
updateStatement *UpdateStatement
recordDescription *recordDescription
}
// Update initializes an UPDATE sql statement for the given object.
func (db *DB) Update(record interface{}) *StructUpdate {
var err error
su := &StructUpdate{}
su.recordDescription, err = buildRecordDescription(record)
if err != nil {
su.error = err
return su
}
if su.recordDescription.isSlice {
su.error = fmt.Errorf("Update accept only a single instance, got a slice")
return su
}
quotedTableName := db.adapter.Quote(db.defaultTableNamer(su.recordDescription.getTableName()))
su.updateStatement = db.UpdateTable(quotedTableName)
return su
}
// Do executes the UPDATE statement for the struct given to the Update method.
func (su *StructUpdate) Do() error {
if su.error != nil {
return su.error
}
// Which columns to update ?
columnsToUpdate := su.recordDescription.structMapping.GetNonAutoColumnsNames()
values := su.recordDescription.structMapping.GetNonAutoFieldsValues(su.recordDescription.record)
for i, column := range columnsToUpdate {
quotedColumn := su.updateStatement.db.adapter.Quote(column)
su.updateStatement = su.updateStatement.Set(quotedColumn, values[i])
}
// On wich keys
keyColumns := su.recordDescription.structMapping.GetKeyColumnsNames()
keyValues := su.recordDescription.structMapping.GetKeyFieldsValues(su.recordDescription.record)
if len(keyColumns) == 0 {
return fmt.Errorf("The object of type %T has no key : ", su.recordDescription.record)
}
for i, column := range keyColumns {
quotedColumn := su.updateStatement.db.adapter.Quote(column)
su.updateStatement = su.updateStatement.Where(quotedColumn+" = ?", keyValues[i])
}
// Optimistic Locking
opLockColumn := su.recordDescription.structMapping.GetOpLockSQLFieldName()
if opLockColumn != "" {
opLockValue, err := su.recordDescription.structMapping.GetAndUpdateOpLockFieldValue(su.recordDescription.record)
if err != nil {
return err
}
su.updateStatement = su.updateStatement.Where(opLockColumn+" = ?", opLockValue)
}
// Use a RETURNING (or similar) clause ?
returningBuilder, ok := su.updateStatement.db.adapter.(adapters.ReturningBuilder)
if ok {
autoColumns := su.recordDescription.structMapping.GetAutoColumnsNames()
su.updateStatement.Returning(returningBuilder.FormatForNewValues(autoColumns)...)
}
var rowsAffected int64
var err error
if returningBuilder != nil {
// the function which will return the pointers according to the given columns
f := func(record interface{}, columns []string) ([]interface{}, error) {
pointers, err := su.recordDescription.structMapping.GetAutoFieldsPointers(record)
return pointers, err
}
// Case for adapters implenting ReturningSuffix()
rowsAffected, err = su.updateStatement.doWithReturning(su.recordDescription, f)
} else {
// Case for adapters not implenting ReturningSuffix()
rowsAffected, err = su.updateStatement.Do()
if err != nil {
return err
}
}
if opLockColumn != "" && rowsAffected == 0 {
err = ErrOpLock
}
return err
}