forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executors.go
62 lines (48 loc) · 1.43 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
package pop
import . "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) 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) 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) 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)
})
}