-
Notifications
You must be signed in to change notification settings - Fork 27
/
struct_update.go
166 lines (143 loc) · 4.66 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
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
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
whiteList []string
blackList []string
}
// 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.quote(db.defaultTableNamer(su.recordDescription.getTableName()))
su.updateStatement = db.UpdateTable(quotedTableName)
return su
}
// Whitelist saves columns to be updated from struct
//
// whitelist should not include auto key tagged columns
func (su *StructUpdate) Whitelist(columns ...string) *StructUpdate {
su.whiteList = append(su.whiteList, columns...)
return su
}
// WhitelistReset resets whiteList
func (su *StructUpdate) WhitelistReset() *StructUpdate {
su.whiteList = nil
return su
}
// GetWhitelist returns whiteList
func (su *StructUpdate) GetWhitelist() []string {
return su.whiteList
}
// Blacklist saves columns not to be updated from struct
// It adds columns to list each time it is called. If a column defined in whitelist is
// also given in black list than that column will be blacklisted.
func (su *StructUpdate) Blacklist(columns ...string) *StructUpdate {
su.blackList = append(su.blackList, columns...)
return su
}
// BlacklistReset resets blacklist
func (su *StructUpdate) BlacklistReset() *StructUpdate {
su.blackList = nil
return su
}
// GetBlacklist returns blackList
func (su *StructUpdate) GetBlacklist() []string {
return su.blackList
}
// 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 ?
var columnsToUpdate []string
if len(su.whiteList) > 0 {
columnsToUpdate = su.whiteList
} else {
columnsToUpdate = su.recordDescription.structMapping.GetNonAutoColumnsNames()
}
// Filter black listed columns
i := 0
for _, c := range su.blackList {
i = 0
for _, a := range columnsToUpdate {
if a != c {
columnsToUpdate[i] = a
i++
}
}
columnsToUpdate = columnsToUpdate[:i]
}
columns, values := su.recordDescription.structMapping.GetNonAutoFieldsValuesFiltered(su.recordDescription.record, columnsToUpdate, false)
for i, column := range columns {
quotedColumn := su.updateStatement.db.quote(column)
su.updateStatement = su.updateStatement.Set(quotedColumn, values[i])
}
// On which 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.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
}