forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executors.go
101 lines (84 loc) · 2.38 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
package pop
import (
"github.com/markbates/validate"
. "github.com/markbates/pop/columns"
)
func (c *Connection) Reload(model interface{}) error {
sm := Model{Value: model}
return c.Find(model, sm.ID())
}
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 (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...)
}
func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
sm := &Model{Value: model}
if sm.ID() == 0 {
return c.Create(model, excludeColumns...)
} else {
return c.Update(model, excludeColumns...)
}
}
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...)
}
func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
return c.timeFunc("Create", func() error {
sm := &Model{Value: model}
cols := ColumnsForStruct(model, sm.TableName())
cols.Remove(excludeColumns...)
sm.touchCreatedAt()
sm.touchUpdatedAt()
return c.Dialect.Create(c.Store, sm, cols)
})
}
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...)
}
func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
return c.timeFunc("Update", func() error {
sm := &Model{Value: model}
cols := ColumnsForStruct(model, sm.TableName())
cols.Remove("id", "created_at")
cols.Remove(excludeColumns...)
sm.touchUpdatedAt()
return c.Dialect.Update(c.Store, sm, cols)
})
}
func (c *Connection) Destroy(model interface{}) error {
return c.timeFunc("Destroy", func() error {
sm := &Model{Value: model}
return c.Dialect.Destroy(c.Store, sm)
})
}