forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutors.go
202 lines (171 loc) · 5.09 KB
/
executors.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
package pop
import (
"fmt"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/logging"
"github.com/gobuffalo/uuid"
"github.com/gobuffalo/validate"
)
// Reload fetch fresh data for a given model, using its ID.
func (c *Connection) Reload(model interface{}) error {
sm := Model{Value: model}
return sm.iterate(func(m *Model) error {
return c.Find(m.Value, m.ID())
})
}
// Exec runs the given query.
func (q *Query) Exec() error {
return q.Connection.timeFunc("Exec", func() error {
sql, args := q.ToSQL(nil)
log(logging.SQL, sql, args...)
_, err := q.Connection.Store.Exec(sql, args...)
return err
})
}
// ExecWithCount runs the given query, and returns the amount of
// affected rows.
func (q *Query) ExecWithCount() (int, error) {
count := int64(0)
return int(count), q.Connection.timeFunc("Exec", func() error {
sql, args := q.ToSQL(nil)
log(logging.SQL, sql, args...)
result, err := q.Connection.Store.Exec(sql, args...)
if err != nil {
return err
}
count, err = result.RowsAffected()
return err
})
}
// ValidateAndSave applies validation rules on the given entry, then save it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndSave(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateSave(c)
if err != nil {
return verrs, err
}
if verrs.HasAny() {
return verrs, nil
}
return verrs, c.Save(model, excludeColumns...)
}
var emptyUUID = uuid.Nil.String()
// Save wraps the Create and Update methods. It executes a Create if no ID is provided with the entry;
// or issues an Update otherwise.
func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
sm := &Model{Value: model}
return sm.iterate(func(m *Model) error {
id := m.ID()
if fmt.Sprint(id) == "0" || fmt.Sprint(id) == emptyUUID {
return c.Create(m.Value, excludeColumns...)
}
return c.Update(m.Value, excludeColumns...)
})
}
// ValidateAndCreate applies validation rules on the given entry, then creates it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
if c.eager {
return c.eagerValidateAndCreate(model, excludeColumns...)
}
sm := &Model{Value: model}
verrs, err := sm.validateCreate(c)
if err != nil {
return verrs, err
}
if verrs.HasAny() {
return verrs, nil
}
return verrs, c.Create(model, excludeColumns...)
}
// Create add a new given entry to the database, excluding the given columns.
// It updates `created_at` and `updated_at` columns automatically.
func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
if c.eager {
return c.eagerCreate(model, excludeColumns...)
}
sm := &Model{Value: model}
return sm.iterate(func(m *Model) error {
return c.timeFunc("Create", func() error {
var err error
if err = m.beforeSave(c); err != nil {
return err
}
if err = m.beforeCreate(c); err != nil {
return err
}
cols := columns.ForStructWithAlias(m.Value, m.TableName(), m.As)
if sm.TableName() == m.TableName() {
cols.Remove(excludeColumns...)
}
m.touchCreatedAt()
m.touchUpdatedAt()
if err = c.Dialect.Create(c.Store, m, cols); err != nil {
return err
}
if err = m.afterCreate(c); err != nil {
return err
}
return m.afterSave(c)
})
})
}
// ValidateAndUpdate applies validation rules on the given entry, then update it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndUpdate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateUpdate(c)
if err != nil {
return verrs, err
}
if verrs.HasAny() {
return verrs, nil
}
return verrs, c.Update(model, excludeColumns...)
}
// Update writes changes from an entry to the database, excluding the given columns.
// It updates the `updated_at` column automatically.
func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
sm := &Model{Value: model}
return sm.iterate(func(m *Model) error {
return c.timeFunc("Update", func() error {
var err error
if err = m.beforeSave(c); err != nil {
return err
}
if err = m.beforeUpdate(c); err != nil {
return err
}
cols := columns.ForStructWithAlias(model, m.TableName(), m.As)
cols.Remove("id", "created_at")
if m.TableName() == sm.TableName() {
cols.Remove(excludeColumns...)
}
m.touchUpdatedAt()
if err = c.Dialect.Update(c.Store, m, cols); err != nil {
return err
}
if err = m.afterUpdate(c); err != nil {
return err
}
return m.afterSave(c)
})
})
}
// Destroy deletes a given entry from the database
func (c *Connection) Destroy(model interface{}) error {
sm := &Model{Value: model}
return sm.iterate(func(m *Model) error {
return c.timeFunc("Destroy", func() error {
var err error
if err = m.beforeDestroy(c); err != nil {
return err
}
if err = c.Dialect.Destroy(c.Store, m); err != nil {
return err
}
return m.afterDestroy(c)
})
})
}