forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executors_eager.go
130 lines (108 loc) · 2.46 KB
/
executors_eager.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
package pop
import (
"github.com/gobuffalo/pop/associations"
"github.com/gobuffalo/validate"
)
func (c *Connection) eagerCreate(model interface{}, excludeColumns ...string) error {
asos, err := associations.ForStruct(model, c.eagerFields...)
if err != nil {
return err
}
c.disableEager()
if len(asos) == 0 {
return c.Create(model, excludeColumns...)
}
before := asos.AssociationsBeforeCreatable()
for index := range before {
i := before[index].BeforeInterface()
if i == nil {
continue
}
err = c.Create(i)
if err != nil {
return err
}
err = before[index].BeforeSetup()
if err != nil {
return err
}
}
err = c.Create(model, excludeColumns...)
if err != nil {
return err
}
after := asos.AssociationsAfterCreatable()
for index := range after {
err = after[index].AfterSetup()
if err != nil {
return err
}
i := after[index].AfterInterface()
if i == nil {
continue
}
err = c.Create(i)
if err != nil {
return err
}
}
stms := asos.AssociationsCreatableStatement()
for index := range stms {
statements := stms[index].Statements()
for _, stm := range statements {
if c.TX != nil {
_, err := c.TX.Exec(c.Dialect.TranslateSQL(stm.Statement), stm.Args...)
if err != nil {
return err
}
continue
}
_, err = c.Store.Exec(c.Dialect.TranslateSQL(stm.Statement), stm.Args...)
if err != nil {
return err
}
}
}
return err
}
func (c *Connection) eagerValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
asos, err := associations.ForStruct(model, c.eagerFields...)
verrs := validate.NewErrors()
if err != nil {
return verrs, err
}
if len(asos) == 0 {
c.disableEager()
return c.ValidateAndCreate(model, excludeColumns...)
}
before := asos.AssociationsBeforeCreatable()
for index := range before {
i := before[index].BeforeInterface()
if i == nil {
continue
}
sm := &Model{Value: i}
verrs, err := sm.validateCreate(c)
if err != nil || verrs.HasAny() {
return verrs, err
}
}
after := asos.AssociationsAfterCreatable()
for index := range after {
i := after[index].AfterInterface()
if i == nil {
continue
}
sm := &Model{Value: i}
verrs, err := sm.validateCreate(c)
if err != nil || verrs.HasAny() {
return verrs, err
}
}
sm := &Model{Value: model}
verrs, err = sm.validateCreate(c)
if err != nil || verrs.HasAny() {
return verrs, err
}
return verrs, c.eagerCreate(model, excludeColumns...)
}