forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutors.go
177 lines (148 loc) · 4.47 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
package pop
import (
"fmt"
"github.com/markbates/pop/columns"
"github.com/markbates/validate"
uuid "github.com/satori/go.uuid"
)
// Reload fetch fresh data for a given model, using its ID
func (c *Connection) Reload(model interface{}) error {
sm := Model{Value: model}
return c.Find(model, sm.ID())
}
// Exec runs the given query
func (q *Query) Exec() error {
return q.Connection.timeFunc("Exec", func() error {
sql, args := q.ToSQL(nil)
Log(sql, args...)
_, err := q.Connection.Store.Exec(sql, args...)
return err
})
}
func (q *Query) ExecWithCount() (int, error) {
count := int64(0)
return int(count), q.Connection.timeFunc("Exec", func() error {
sql, args := q.ToSQL(nil)
Log(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}
id := sm.ID()
if fmt.Sprint(id) == "0" || fmt.Sprint(id) == emptyUUID {
return c.Create(model, excludeColumns...)
}
return c.Update(model, 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) {
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 {
return c.timeFunc("Create", func() error {
var err error
sm := &Model{Value: model}
if err = sm.beforeSave(c); err != nil {
return err
}
if err = sm.beforeCreate(c); err != nil {
return err
}
cols := columns.ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols.Remove(excludeColumns...)
sm.touchCreatedAt()
sm.touchUpdatedAt()
if err = c.Dialect.Create(c.Store, sm, cols); err != nil {
return err
}
if err = sm.afterCreate(c); err != nil {
return err
}
return sm.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 {
return c.timeFunc("Update", func() error {
var err error
sm := &Model{Value: model}
if err = sm.beforeSave(c); err != nil {
return err
}
if err = sm.beforeUpdate(c); err != nil {
return err
}
cols := columns.ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols.Remove("id", "created_at")
cols.Remove(excludeColumns...)
sm.touchUpdatedAt()
if err = c.Dialect.Update(c.Store, sm, cols); err != nil {
return err
}
if err = sm.afterUpdate(c); err != nil {
return err
}
return sm.afterSave(c)
})
}
// Destroy deletes a given entry from the database
func (c *Connection) Destroy(model interface{}) error {
return c.timeFunc("Destroy", func() error {
var err error
sm := &Model{Value: model}
if err = sm.beforeDestroy(c); err != nil {
return err
}
if err = c.Dialect.Destroy(c.Store, sm); err != nil {
return err
}
return sm.afterDestroy(c)
})
}