-
Notifications
You must be signed in to change notification settings - Fork 3
/
store.go
205 lines (167 loc) · 3.87 KB
/
store.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
package bond
import (
"reflect"
"upper.io/db.v3"
"upper.io/db.v3/lib/reflectx"
)
var mapper = reflectx.NewMapper("db")
type hasPrimaryKeys interface {
PrimaryKeys() []string
}
type Store interface {
db.Collection
Session() Session
WithSession(sess Session) Store
Save(interface{}) error
Delete(interface{}) error
Create(interface{}) error
Update(interface{}) error
}
type store struct {
db.Collection
session Session
}
func (s *store) getPrimaryKeyFields(item interface{}) ([]string, []interface{}) {
pKeys := s.Collection.(hasPrimaryKeys).PrimaryKeys()
fields := mapper.FieldsByName(reflect.ValueOf(item), pKeys)
values := make([]interface{}, 0, len(fields))
for i := range fields {
if fields[i].IsValid() {
values = append(values, fields[i].Interface())
}
}
return pKeys, values
}
// WithSession returns a copy of the store that runs in the context of the given
// transaction.
func (s *store) WithSession(sess Session) Store {
return &store{
Collection: sess.Collection(s.Collection.Name()),
session: sess,
}
}
func (s *store) Save(item interface{}) error {
if saver, ok := item.(HasSave); ok {
return s.Session().SessionTx(nil, func(tx Session) error {
return saver.Save(tx)
})
}
if s.Collection == nil {
return ErrInvalidCollection
}
if reflect.TypeOf(item).Kind() != reflect.Ptr {
return ErrExpectingPointerToStruct
}
_, fields := s.getPrimaryKeyFields(item)
isCreate := true
for i := range fields {
if fields[i] != reflect.Zero(reflect.TypeOf(fields[i])).Interface() {
isCreate = false
}
}
if isCreate {
return s.Create(item)
}
return s.Update(item)
}
func (s *store) Create(item interface{}) error {
if s.Collection == nil {
return ErrInvalidCollection
}
if validator, ok := item.(HasValidate); ok {
if err := validator.Validate(); err != nil {
return err
}
}
if m, ok := item.(HasBeforeCreate); ok {
if err := m.BeforeCreate(s.session); err != nil {
return err
}
}
if reflect.TypeOf(item).Kind() == reflect.Ptr {
if err := s.Collection.InsertReturning(item); err != nil {
return err
}
} else {
if _, err := s.Collection.Insert(item); err != nil {
return err
}
}
if m, ok := item.(HasAfterCreate); ok {
if err := m.AfterCreate(s.session); err != nil {
return err
}
}
return nil
}
func (s *store) Update(item interface{}) error {
if s.Collection == nil {
return ErrInvalidCollection
}
if validator, ok := item.(HasValidate); ok {
if err := validator.Validate(); err != nil {
return err
}
}
if m, ok := item.(HasBeforeUpdate); ok {
if err := m.BeforeUpdate(s.session); err != nil {
return err
}
}
cond := db.And()
pKeys, fields := s.getPrimaryKeyFields(item)
for i := range pKeys {
cond = cond.And(db.Cond{pKeys[i]: fields[i]})
}
if cond.Empty() {
return ErrZeroItemID
}
if reflect.TypeOf(item).Kind() == reflect.Ptr {
if err := s.Collection.UpdateReturning(item); err != nil {
return err
}
} else {
if err := s.Collection.Find(cond).Update(item); err != nil {
return err
}
}
if m, ok := item.(HasAfterUpdate); ok {
if err := m.AfterUpdate(s.session); err != nil {
return err
}
}
return nil
}
func (s *store) Delete(item interface{}) error {
if s.Collection == nil {
return ErrInvalidCollection
}
if reflect.TypeOf(item).Kind() != reflect.Ptr {
return ErrExpectingPointerToStruct
}
cond := db.And()
pKeys, fields := s.getPrimaryKeyFields(item)
for i := range pKeys {
cond = cond.And(db.Cond{pKeys[i]: fields[i]})
}
if cond.Empty() {
return ErrZeroItemID
}
if m, ok := item.(HasBeforeDelete); ok {
if err := m.BeforeDelete(s.session); err != nil {
return err
}
}
if err := s.Collection.Find(cond).Delete(); err != nil {
return err
}
if m, ok := item.(HasAfterDelete); ok {
if err := m.AfterDelete(s.session); err != nil {
return err
}
}
return nil
}
func (s *store) Session() Session {
return s.session
}