/
droom_create.go
335 lines (307 loc) · 8.24 KB
/
droom_create.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/orestonce/ChessGame/ent/droom"
)
// DRoomCreate is the builder for creating a DRoom entity.
type DRoomCreate struct {
config
mutation *DRoomMutation
hooks []Hook
}
// SetIsGameRunning sets the "is_game_running" field.
func (dc *DRoomCreate) SetIsGameRunning(b bool) *DRoomCreate {
dc.mutation.SetIsGameRunning(b)
return dc
}
// SetNillableIsGameRunning sets the "is_game_running" field if the given value is not nil.
func (dc *DRoomCreate) SetNillableIsGameRunning(b *bool) *DRoomCreate {
if b != nil {
dc.SetIsGameRunning(*b)
}
return dc
}
// SetPanel sets the "panel" field.
func (dc *DRoomCreate) SetPanel(s string) *DRoomCreate {
dc.mutation.SetPanel(s)
return dc
}
// SetNillablePanel sets the "panel" field if the given value is not nil.
func (dc *DRoomCreate) SetNillablePanel(s *string) *DRoomCreate {
if s != nil {
dc.SetPanel(*s)
}
return dc
}
// SetWUserID sets the "w_user_id" field.
func (dc *DRoomCreate) SetWUserID(s string) *DRoomCreate {
dc.mutation.SetWUserID(s)
return dc
}
// SetNillableWUserID sets the "w_user_id" field if the given value is not nil.
func (dc *DRoomCreate) SetNillableWUserID(s *string) *DRoomCreate {
if s != nil {
dc.SetWUserID(*s)
}
return dc
}
// SetBUserID sets the "b_user_id" field.
func (dc *DRoomCreate) SetBUserID(s string) *DRoomCreate {
dc.mutation.SetBUserID(s)
return dc
}
// SetNillableBUserID sets the "b_user_id" field if the given value is not nil.
func (dc *DRoomCreate) SetNillableBUserID(s *string) *DRoomCreate {
if s != nil {
dc.SetBUserID(*s)
}
return dc
}
// SetCreateTime sets the "create_time" field.
func (dc *DRoomCreate) SetCreateTime(t time.Time) *DRoomCreate {
dc.mutation.SetCreateTime(t)
return dc
}
// SetID sets the "id" field.
func (dc *DRoomCreate) SetID(s string) *DRoomCreate {
dc.mutation.SetID(s)
return dc
}
// Mutation returns the DRoomMutation object of the builder.
func (dc *DRoomCreate) Mutation() *DRoomMutation {
return dc.mutation
}
// Save creates the DRoom in the database.
func (dc *DRoomCreate) Save(ctx context.Context) (*DRoom, error) {
var (
err error
node *DRoom
)
dc.defaults()
if len(dc.hooks) == 0 {
if err = dc.check(); err != nil {
return nil, err
}
node, err = dc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*DRoomMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = dc.check(); err != nil {
return nil, err
}
dc.mutation = mutation
if node, err = dc.sqlSave(ctx); err != nil {
return nil, err
}
mutation.id = &node.ID
mutation.done = true
return node, err
})
for i := len(dc.hooks) - 1; i >= 0; i-- {
if dc.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = dc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, dc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (dc *DRoomCreate) SaveX(ctx context.Context) *DRoom {
v, err := dc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dc *DRoomCreate) Exec(ctx context.Context) error {
_, err := dc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dc *DRoomCreate) ExecX(ctx context.Context) {
if err := dc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (dc *DRoomCreate) defaults() {
if _, ok := dc.mutation.IsGameRunning(); !ok {
v := droom.DefaultIsGameRunning
dc.mutation.SetIsGameRunning(v)
}
if _, ok := dc.mutation.Panel(); !ok {
v := droom.DefaultPanel
dc.mutation.SetPanel(v)
}
if _, ok := dc.mutation.WUserID(); !ok {
v := droom.DefaultWUserID
dc.mutation.SetWUserID(v)
}
if _, ok := dc.mutation.BUserID(); !ok {
v := droom.DefaultBUserID
dc.mutation.SetBUserID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (dc *DRoomCreate) check() error {
if _, ok := dc.mutation.CreateTime(); !ok {
return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "create_time"`)}
}
return nil
}
func (dc *DRoomCreate) sqlSave(ctx context.Context) (*DRoom, error) {
_node, _spec := dc.createSpec()
if err := sqlgraph.CreateNode(ctx, dc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{err.Error(), err}
}
return nil, err
}
return _node, nil
}
func (dc *DRoomCreate) createSpec() (*DRoom, *sqlgraph.CreateSpec) {
var (
_node = &DRoom{config: dc.config}
_spec = &sqlgraph.CreateSpec{
Table: droom.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: droom.FieldID,
},
}
)
if id, ok := dc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := dc.mutation.IsGameRunning(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBool,
Value: value,
Column: droom.FieldIsGameRunning,
})
_node.IsGameRunning = value
}
if value, ok := dc.mutation.Panel(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: droom.FieldPanel,
})
_node.Panel = value
}
if value, ok := dc.mutation.WUserID(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: droom.FieldWUserID,
})
_node.WUserID = value
}
if value, ok := dc.mutation.BUserID(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: droom.FieldBUserID,
})
_node.BUserID = value
}
if value, ok := dc.mutation.CreateTime(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: droom.FieldCreateTime,
})
_node.CreateTime = value
}
return _node, _spec
}
// DRoomCreateBulk is the builder for creating many DRoom entities in bulk.
type DRoomCreateBulk struct {
config
builders []*DRoomCreate
}
// Save creates the DRoom entities in the database.
func (dcb *DRoomCreateBulk) Save(ctx context.Context) ([]*DRoom, error) {
specs := make([]*sqlgraph.CreateSpec, len(dcb.builders))
nodes := make([]*DRoom, len(dcb.builders))
mutators := make([]Mutator, len(dcb.builders))
for i := range dcb.builders {
func(i int, root context.Context) {
builder := dcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*DRoomMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
nodes[i], specs[i] = builder.createSpec()
var err error
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, dcb.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, dcb.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{err.Error(), err}
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].ID
mutation.done = true
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {
mut = builder.hooks[i](mut)
}
mutators[i] = mut
}(i, ctx)
}
if len(mutators) > 0 {
if _, err := mutators[0].Mutate(ctx, dcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (dcb *DRoomCreateBulk) SaveX(ctx context.Context) []*DRoom {
v, err := dcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dcb *DRoomCreateBulk) Exec(ctx context.Context) error {
_, err := dcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dcb *DRoomCreateBulk) ExecX(ctx context.Context) {
if err := dcb.Exec(ctx); err != nil {
panic(err)
}
}