generated from ent/bug
/
like_update.go
254 lines (228 loc) · 7.42 KB
/
like_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
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
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/lrstanley/ent-bug-edge-schema/internal/database/ent/like"
"github.com/lrstanley/ent-bug-edge-schema/internal/database/ent/predicate"
)
// LikeUpdate is the builder for updating Like entities.
type LikeUpdate struct {
config
hooks []Hook
mutation *LikeMutation
}
// Where appends a list predicates to the LikeUpdate builder.
func (lu *LikeUpdate) Where(ps ...predicate.Like) *LikeUpdate {
lu.mutation.Where(ps...)
return lu
}
// SetUpdateTime sets the "update_time" field.
func (lu *LikeUpdate) SetUpdateTime(t time.Time) *LikeUpdate {
lu.mutation.SetUpdateTime(t)
return lu
}
// Mutation returns the LikeMutation object of the builder.
func (lu *LikeUpdate) Mutation() *LikeMutation {
return lu.mutation
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (lu *LikeUpdate) Save(ctx context.Context) (int, error) {
if err := lu.defaults(); err != nil {
return 0, err
}
return withHooks(ctx, lu.sqlSave, lu.mutation, lu.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (lu *LikeUpdate) SaveX(ctx context.Context) int {
affected, err := lu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (lu *LikeUpdate) Exec(ctx context.Context) error {
_, err := lu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (lu *LikeUpdate) ExecX(ctx context.Context) {
if err := lu.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (lu *LikeUpdate) defaults() error {
if _, ok := lu.mutation.UpdateTime(); !ok {
if like.UpdateDefaultUpdateTime == nil {
return fmt.Errorf("ent: uninitialized like.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
}
v := like.UpdateDefaultUpdateTime()
lu.mutation.SetUpdateTime(v)
}
return nil
}
// check runs all checks and user-defined validators on the builder.
func (lu *LikeUpdate) check() error {
if _, ok := lu.mutation.UserID(); lu.mutation.UserCleared() && !ok {
return errors.New(`ent: clearing a required unique edge "Like.user"`)
}
if _, ok := lu.mutation.TweetID(); lu.mutation.TweetCleared() && !ok {
return errors.New(`ent: clearing a required unique edge "Like.tweet"`)
}
return nil
}
func (lu *LikeUpdate) sqlSave(ctx context.Context) (n int, err error) {
if err := lu.check(); err != nil {
return n, err
}
_spec := sqlgraph.NewUpdateSpec(like.Table, like.Columns, sqlgraph.NewFieldSpec(like.FieldUserID, field.TypeInt), sqlgraph.NewFieldSpec(like.FieldTweetID, field.TypeInt))
if ps := lu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := lu.mutation.UpdateTime(); ok {
_spec.SetField(like.FieldUpdateTime, field.TypeTime, value)
}
if n, err = sqlgraph.UpdateNodes(ctx, lu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{like.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
lu.mutation.done = true
return n, nil
}
// LikeUpdateOne is the builder for updating a single Like entity.
type LikeUpdateOne struct {
config
fields []string
hooks []Hook
mutation *LikeMutation
}
// SetUpdateTime sets the "update_time" field.
func (luo *LikeUpdateOne) SetUpdateTime(t time.Time) *LikeUpdateOne {
luo.mutation.SetUpdateTime(t)
return luo
}
// Mutation returns the LikeMutation object of the builder.
func (luo *LikeUpdateOne) Mutation() *LikeMutation {
return luo.mutation
}
// Where appends a list predicates to the LikeUpdate builder.
func (luo *LikeUpdateOne) Where(ps ...predicate.Like) *LikeUpdateOne {
luo.mutation.Where(ps...)
return luo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (luo *LikeUpdateOne) Select(field string, fields ...string) *LikeUpdateOne {
luo.fields = append([]string{field}, fields...)
return luo
}
// Save executes the query and returns the updated Like entity.
func (luo *LikeUpdateOne) Save(ctx context.Context) (*Like, error) {
if err := luo.defaults(); err != nil {
return nil, err
}
return withHooks(ctx, luo.sqlSave, luo.mutation, luo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (luo *LikeUpdateOne) SaveX(ctx context.Context) *Like {
node, err := luo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (luo *LikeUpdateOne) Exec(ctx context.Context) error {
_, err := luo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (luo *LikeUpdateOne) ExecX(ctx context.Context) {
if err := luo.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (luo *LikeUpdateOne) defaults() error {
if _, ok := luo.mutation.UpdateTime(); !ok {
if like.UpdateDefaultUpdateTime == nil {
return fmt.Errorf("ent: uninitialized like.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
}
v := like.UpdateDefaultUpdateTime()
luo.mutation.SetUpdateTime(v)
}
return nil
}
// check runs all checks and user-defined validators on the builder.
func (luo *LikeUpdateOne) check() error {
if _, ok := luo.mutation.UserID(); luo.mutation.UserCleared() && !ok {
return errors.New(`ent: clearing a required unique edge "Like.user"`)
}
if _, ok := luo.mutation.TweetID(); luo.mutation.TweetCleared() && !ok {
return errors.New(`ent: clearing a required unique edge "Like.tweet"`)
}
return nil
}
func (luo *LikeUpdateOne) sqlSave(ctx context.Context) (_node *Like, err error) {
if err := luo.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(like.Table, like.Columns, sqlgraph.NewFieldSpec(like.FieldUserID, field.TypeInt), sqlgraph.NewFieldSpec(like.FieldTweetID, field.TypeInt))
if id, ok := luo.mutation.UserID(); !ok {
return nil, &ValidationError{Name: "user_id", err: errors.New(`ent: missing "Like.user_id" for update`)}
} else {
_spec.Node.CompositeID[0].Value = id
}
if id, ok := luo.mutation.TweetID(); !ok {
return nil, &ValidationError{Name: "tweet_id", err: errors.New(`ent: missing "Like.tweet_id" for update`)}
} else {
_spec.Node.CompositeID[1].Value = id
}
if fields := luo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, len(fields))
for i, f := range fields {
if !like.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
_spec.Node.Columns[i] = f
}
}
if ps := luo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := luo.mutation.UpdateTime(); ok {
_spec.SetField(like.FieldUpdateTime, field.TypeTime, value)
}
_node = &Like{config: luo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, luo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{like.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
luo.mutation.done = true
return _node, nil
}