/
item_update.go
210 lines (191 loc) · 5.15 KB
/
item_update.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
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
"github.com/facebookincubator/ent/entc/integration/ent/item"
"github.com/facebookincubator/ent/entc/integration/ent/predicate"
"github.com/facebookincubator/ent/schema/field"
)
// ItemUpdate is the builder for updating Item entities.
type ItemUpdate struct {
config
hooks []Hook
mutation *ItemMutation
predicates []predicate.Item
}
// Where adds a new predicate for the builder.
func (iu *ItemUpdate) Where(ps ...predicate.Item) *ItemUpdate {
iu.predicates = append(iu.predicates, ps...)
return iu
}
// Mutation returns the ItemMutation object of the builder.
func (iu *ItemUpdate) Mutation() *ItemMutation {
return iu.mutation
}
// Save executes the query and returns the number of rows/vertices matched by this operation.
func (iu *ItemUpdate) Save(ctx context.Context) (int, error) {
var (
err error
affected int
)
if len(iu.hooks) == 0 {
affected, err = iu.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ItemMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
iu.mutation = mutation
affected, err = iu.sqlSave(ctx)
mutation.done = true
return affected, err
})
for i := len(iu.hooks) - 1; i >= 0; i-- {
mut = iu.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, iu.mutation); err != nil {
return 0, err
}
}
return affected, err
}
// SaveX is like Save, but panics if an error occurs.
func (iu *ItemUpdate) SaveX(ctx context.Context) int {
affected, err := iu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (iu *ItemUpdate) Exec(ctx context.Context) error {
_, err := iu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (iu *ItemUpdate) ExecX(ctx context.Context) {
if err := iu.Exec(ctx); err != nil {
panic(err)
}
}
func (iu *ItemUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: item.Table,
Columns: item.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: item.FieldID,
},
},
}
if ps := iu.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if n, err = sqlgraph.UpdateNodes(ctx, iu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{item.Label}
} else if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return 0, err
}
return n, nil
}
// ItemUpdateOne is the builder for updating a single Item entity.
type ItemUpdateOne struct {
config
hooks []Hook
mutation *ItemMutation
}
// Mutation returns the ItemMutation object of the builder.
func (iuo *ItemUpdateOne) Mutation() *ItemMutation {
return iuo.mutation
}
// Save executes the query and returns the updated entity.
func (iuo *ItemUpdateOne) Save(ctx context.Context) (*Item, error) {
var (
err error
node *Item
)
if len(iuo.hooks) == 0 {
node, err = iuo.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ItemMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
iuo.mutation = mutation
node, err = iuo.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(iuo.hooks) - 1; i >= 0; i-- {
mut = iuo.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, iuo.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX is like Save, but panics if an error occurs.
func (iuo *ItemUpdateOne) SaveX(ctx context.Context) *Item {
i, err := iuo.Save(ctx)
if err != nil {
panic(err)
}
return i
}
// Exec executes the query on the entity.
func (iuo *ItemUpdateOne) Exec(ctx context.Context) error {
_, err := iuo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (iuo *ItemUpdateOne) ExecX(ctx context.Context) {
if err := iuo.Exec(ctx); err != nil {
panic(err)
}
}
func (iuo *ItemUpdateOne) sqlSave(ctx context.Context) (i *Item, err error) {
_spec := &sqlgraph.UpdateSpec{
Node: &sqlgraph.NodeSpec{
Table: item.Table,
Columns: item.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: item.FieldID,
},
},
}
id, ok := iuo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing Item.ID for update")}
}
_spec.Node.ID.Value = id
i = &Item{config: iuo.config}
_spec.Assign = i.assignValues
_spec.ScanValues = i.scanValues()
if err = sqlgraph.UpdateNode(ctx, iuo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{item.Label}
} else if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
return i, nil
}