oceanbase
490 строк · 13.4 Кб
1// Code generated by entc, DO NOT EDIT.
2
3package ent
4
5import (
6"context"
7"errors"
8"fmt"
9"time"
10
11"entgo.io/ent/dialect/sql"
12"entgo.io/ent/dialect/sql/sqlgraph"
13"entgo.io/ent/schema/field"
14"github.com/oceanbase/configserver/ent/obcluster"
15"github.com/oceanbase/configserver/ent/predicate"
16)
17
18// ObClusterUpdate is the builder for updating ObCluster entities.
19type ObClusterUpdate struct {
20config
21hooks []Hook
22mutation *ObClusterMutation
23}
24
25// Where appends a list predicates to the ObClusterUpdate builder.
26func (ocu *ObClusterUpdate) Where(ps ...predicate.ObCluster) *ObClusterUpdate {
27ocu.mutation.Where(ps...)
28return ocu
29}
30
31// SetCreateTime sets the "create_time" field.
32func (ocu *ObClusterUpdate) SetCreateTime(t time.Time) *ObClusterUpdate {
33ocu.mutation.SetCreateTime(t)
34return ocu
35}
36
37// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
38func (ocu *ObClusterUpdate) SetNillableCreateTime(t *time.Time) *ObClusterUpdate {
39if t != nil {
40ocu.SetCreateTime(*t)
41}
42return ocu
43}
44
45// SetUpdateTime sets the "update_time" field.
46func (ocu *ObClusterUpdate) SetUpdateTime(t time.Time) *ObClusterUpdate {
47ocu.mutation.SetUpdateTime(t)
48return ocu
49}
50
51// SetName sets the "name" field.
52func (ocu *ObClusterUpdate) SetName(s string) *ObClusterUpdate {
53ocu.mutation.SetName(s)
54return ocu
55}
56
57// SetObClusterID sets the "ob_cluster_id" field.
58func (ocu *ObClusterUpdate) SetObClusterID(i int64) *ObClusterUpdate {
59ocu.mutation.ResetObClusterID()
60ocu.mutation.SetObClusterID(i)
61return ocu
62}
63
64// AddObClusterID adds i to the "ob_cluster_id" field.
65func (ocu *ObClusterUpdate) AddObClusterID(i int64) *ObClusterUpdate {
66ocu.mutation.AddObClusterID(i)
67return ocu
68}
69
70// SetType sets the "type" field.
71func (ocu *ObClusterUpdate) SetType(s string) *ObClusterUpdate {
72ocu.mutation.SetType(s)
73return ocu
74}
75
76// SetRootserviceJSON sets the "rootservice_json" field.
77func (ocu *ObClusterUpdate) SetRootserviceJSON(s string) *ObClusterUpdate {
78ocu.mutation.SetRootserviceJSON(s)
79return ocu
80}
81
82// Mutation returns the ObClusterMutation object of the builder.
83func (ocu *ObClusterUpdate) Mutation() *ObClusterMutation {
84return ocu.mutation
85}
86
87// Save executes the query and returns the number of nodes affected by the update operation.
88func (ocu *ObClusterUpdate) Save(ctx context.Context) (int, error) {
89var (
90err error
91affected int
92)
93ocu.defaults()
94if len(ocu.hooks) == 0 {
95if err = ocu.check(); err != nil {
96return 0, err
97}
98affected, err = ocu.sqlSave(ctx)
99} else {
100var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
101mutation, ok := m.(*ObClusterMutation)
102if !ok {
103return nil, fmt.Errorf("unexpected mutation type %T", m)
104}
105if err = ocu.check(); err != nil {
106return 0, err
107}
108ocu.mutation = mutation
109affected, err = ocu.sqlSave(ctx)
110mutation.done = true
111return affected, err
112})
113for i := len(ocu.hooks) - 1; i >= 0; i-- {
114if ocu.hooks[i] == nil {
115return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
116}
117mut = ocu.hooks[i](mut)
118}
119if _, err := mut.Mutate(ctx, ocu.mutation); err != nil {
120return 0, err
121}
122}
123return affected, err
124}
125
126// SaveX is like Save, but panics if an error occurs.
127func (ocu *ObClusterUpdate) SaveX(ctx context.Context) int {
128affected, err := ocu.Save(ctx)
129if err != nil {
130panic(err)
131}
132return affected
133}
134
135// Exec executes the query.
136func (ocu *ObClusterUpdate) Exec(ctx context.Context) error {
137_, err := ocu.Save(ctx)
138return err
139}
140
141// ExecX is like Exec, but panics if an error occurs.
142func (ocu *ObClusterUpdate) ExecX(ctx context.Context) {
143if err := ocu.Exec(ctx); err != nil {
144panic(err)
145}
146}
147
148// defaults sets the default values of the builder before save.
149func (ocu *ObClusterUpdate) defaults() {
150if _, ok := ocu.mutation.UpdateTime(); !ok {
151v := obcluster.UpdateDefaultUpdateTime()
152ocu.mutation.SetUpdateTime(v)
153}
154}
155
156// check runs all checks and user-defined validators on the builder.
157func (ocu *ObClusterUpdate) check() error {
158if v, ok := ocu.mutation.ObClusterID(); ok {
159if err := obcluster.ObClusterIDValidator(v); err != nil {
160return &ValidationError{Name: "ob_cluster_id", err: fmt.Errorf(`ent: validator failed for field "ObCluster.ob_cluster_id": %w`, err)}
161}
162}
163return nil
164}
165
166func (ocu *ObClusterUpdate) sqlSave(ctx context.Context) (n int, err error) {
167_spec := &sqlgraph.UpdateSpec{
168Node: &sqlgraph.NodeSpec{
169Table: obcluster.Table,
170Columns: obcluster.Columns,
171ID: &sqlgraph.FieldSpec{
172Type: field.TypeInt,
173Column: obcluster.FieldID,
174},
175},
176}
177if ps := ocu.mutation.predicates; len(ps) > 0 {
178_spec.Predicate = func(selector *sql.Selector) {
179for i := range ps {
180ps[i](selector)
181}
182}
183}
184if value, ok := ocu.mutation.CreateTime(); ok {
185_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
186Type: field.TypeTime,
187Value: value,
188Column: obcluster.FieldCreateTime,
189})
190}
191if value, ok := ocu.mutation.UpdateTime(); ok {
192_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
193Type: field.TypeTime,
194Value: value,
195Column: obcluster.FieldUpdateTime,
196})
197}
198if value, ok := ocu.mutation.Name(); ok {
199_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
200Type: field.TypeString,
201Value: value,
202Column: obcluster.FieldName,
203})
204}
205if value, ok := ocu.mutation.ObClusterID(); ok {
206_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
207Type: field.TypeInt64,
208Value: value,
209Column: obcluster.FieldObClusterID,
210})
211}
212if value, ok := ocu.mutation.AddedObClusterID(); ok {
213_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
214Type: field.TypeInt64,
215Value: value,
216Column: obcluster.FieldObClusterID,
217})
218}
219if value, ok := ocu.mutation.GetType(); ok {
220_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
221Type: field.TypeString,
222Value: value,
223Column: obcluster.FieldType,
224})
225}
226if value, ok := ocu.mutation.RootserviceJSON(); ok {
227_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
228Type: field.TypeString,
229Value: value,
230Column: obcluster.FieldRootserviceJSON,
231})
232}
233if n, err = sqlgraph.UpdateNodes(ctx, ocu.driver, _spec); err != nil {
234if _, ok := err.(*sqlgraph.NotFoundError); ok {
235err = &NotFoundError{obcluster.Label}
236} else if sqlgraph.IsConstraintError(err) {
237err = &ConstraintError{err.Error(), err}
238}
239return 0, err
240}
241return n, nil
242}
243
244// ObClusterUpdateOne is the builder for updating a single ObCluster entity.
245type ObClusterUpdateOne struct {
246config
247fields []string
248hooks []Hook
249mutation *ObClusterMutation
250}
251
252// SetCreateTime sets the "create_time" field.
253func (ocuo *ObClusterUpdateOne) SetCreateTime(t time.Time) *ObClusterUpdateOne {
254ocuo.mutation.SetCreateTime(t)
255return ocuo
256}
257
258// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
259func (ocuo *ObClusterUpdateOne) SetNillableCreateTime(t *time.Time) *ObClusterUpdateOne {
260if t != nil {
261ocuo.SetCreateTime(*t)
262}
263return ocuo
264}
265
266// SetUpdateTime sets the "update_time" field.
267func (ocuo *ObClusterUpdateOne) SetUpdateTime(t time.Time) *ObClusterUpdateOne {
268ocuo.mutation.SetUpdateTime(t)
269return ocuo
270}
271
272// SetName sets the "name" field.
273func (ocuo *ObClusterUpdateOne) SetName(s string) *ObClusterUpdateOne {
274ocuo.mutation.SetName(s)
275return ocuo
276}
277
278// SetObClusterID sets the "ob_cluster_id" field.
279func (ocuo *ObClusterUpdateOne) SetObClusterID(i int64) *ObClusterUpdateOne {
280ocuo.mutation.ResetObClusterID()
281ocuo.mutation.SetObClusterID(i)
282return ocuo
283}
284
285// AddObClusterID adds i to the "ob_cluster_id" field.
286func (ocuo *ObClusterUpdateOne) AddObClusterID(i int64) *ObClusterUpdateOne {
287ocuo.mutation.AddObClusterID(i)
288return ocuo
289}
290
291// SetType sets the "type" field.
292func (ocuo *ObClusterUpdateOne) SetType(s string) *ObClusterUpdateOne {
293ocuo.mutation.SetType(s)
294return ocuo
295}
296
297// SetRootserviceJSON sets the "rootservice_json" field.
298func (ocuo *ObClusterUpdateOne) SetRootserviceJSON(s string) *ObClusterUpdateOne {
299ocuo.mutation.SetRootserviceJSON(s)
300return ocuo
301}
302
303// Mutation returns the ObClusterMutation object of the builder.
304func (ocuo *ObClusterUpdateOne) Mutation() *ObClusterMutation {
305return ocuo.mutation
306}
307
308// Select allows selecting one or more fields (columns) of the returned entity.
309// The default is selecting all fields defined in the entity schema.
310func (ocuo *ObClusterUpdateOne) Select(field string, fields ...string) *ObClusterUpdateOne {
311ocuo.fields = append([]string{field}, fields...)
312return ocuo
313}
314
315// Save executes the query and returns the updated ObCluster entity.
316func (ocuo *ObClusterUpdateOne) Save(ctx context.Context) (*ObCluster, error) {
317var (
318err error
319node *ObCluster
320)
321ocuo.defaults()
322if len(ocuo.hooks) == 0 {
323if err = ocuo.check(); err != nil {
324return nil, err
325}
326node, err = ocuo.sqlSave(ctx)
327} else {
328var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
329mutation, ok := m.(*ObClusterMutation)
330if !ok {
331return nil, fmt.Errorf("unexpected mutation type %T", m)
332}
333if err = ocuo.check(); err != nil {
334return nil, err
335}
336ocuo.mutation = mutation
337node, err = ocuo.sqlSave(ctx)
338mutation.done = true
339return node, err
340})
341for i := len(ocuo.hooks) - 1; i >= 0; i-- {
342if ocuo.hooks[i] == nil {
343return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
344}
345mut = ocuo.hooks[i](mut)
346}
347if _, err := mut.Mutate(ctx, ocuo.mutation); err != nil {
348return nil, err
349}
350}
351return node, err
352}
353
354// SaveX is like Save, but panics if an error occurs.
355func (ocuo *ObClusterUpdateOne) SaveX(ctx context.Context) *ObCluster {
356node, err := ocuo.Save(ctx)
357if err != nil {
358panic(err)
359}
360return node
361}
362
363// Exec executes the query on the entity.
364func (ocuo *ObClusterUpdateOne) Exec(ctx context.Context) error {
365_, err := ocuo.Save(ctx)
366return err
367}
368
369// ExecX is like Exec, but panics if an error occurs.
370func (ocuo *ObClusterUpdateOne) ExecX(ctx context.Context) {
371if err := ocuo.Exec(ctx); err != nil {
372panic(err)
373}
374}
375
376// defaults sets the default values of the builder before save.
377func (ocuo *ObClusterUpdateOne) defaults() {
378if _, ok := ocuo.mutation.UpdateTime(); !ok {
379v := obcluster.UpdateDefaultUpdateTime()
380ocuo.mutation.SetUpdateTime(v)
381}
382}
383
384// check runs all checks and user-defined validators on the builder.
385func (ocuo *ObClusterUpdateOne) check() error {
386if v, ok := ocuo.mutation.ObClusterID(); ok {
387if err := obcluster.ObClusterIDValidator(v); err != nil {
388return &ValidationError{Name: "ob_cluster_id", err: fmt.Errorf(`ent: validator failed for field "ObCluster.ob_cluster_id": %w`, err)}
389}
390}
391return nil
392}
393
394func (ocuo *ObClusterUpdateOne) sqlSave(ctx context.Context) (_node *ObCluster, err error) {
395_spec := &sqlgraph.UpdateSpec{
396Node: &sqlgraph.NodeSpec{
397Table: obcluster.Table,
398Columns: obcluster.Columns,
399ID: &sqlgraph.FieldSpec{
400Type: field.TypeInt,
401Column: obcluster.FieldID,
402},
403},
404}
405id, ok := ocuo.mutation.ID()
406if !ok {
407return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ObCluster.id" for update`)}
408}
409_spec.Node.ID.Value = id
410if fields := ocuo.fields; len(fields) > 0 {
411_spec.Node.Columns = make([]string, 0, len(fields))
412_spec.Node.Columns = append(_spec.Node.Columns, obcluster.FieldID)
413for _, f := range fields {
414if !obcluster.ValidColumn(f) {
415return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
416}
417if f != obcluster.FieldID {
418_spec.Node.Columns = append(_spec.Node.Columns, f)
419}
420}
421}
422if ps := ocuo.mutation.predicates; len(ps) > 0 {
423_spec.Predicate = func(selector *sql.Selector) {
424for i := range ps {
425ps[i](selector)
426}
427}
428}
429if value, ok := ocuo.mutation.CreateTime(); ok {
430_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
431Type: field.TypeTime,
432Value: value,
433Column: obcluster.FieldCreateTime,
434})
435}
436if value, ok := ocuo.mutation.UpdateTime(); ok {
437_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
438Type: field.TypeTime,
439Value: value,
440Column: obcluster.FieldUpdateTime,
441})
442}
443if value, ok := ocuo.mutation.Name(); ok {
444_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
445Type: field.TypeString,
446Value: value,
447Column: obcluster.FieldName,
448})
449}
450if value, ok := ocuo.mutation.ObClusterID(); ok {
451_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
452Type: field.TypeInt64,
453Value: value,
454Column: obcluster.FieldObClusterID,
455})
456}
457if value, ok := ocuo.mutation.AddedObClusterID(); ok {
458_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
459Type: field.TypeInt64,
460Value: value,
461Column: obcluster.FieldObClusterID,
462})
463}
464if value, ok := ocuo.mutation.GetType(); ok {
465_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
466Type: field.TypeString,
467Value: value,
468Column: obcluster.FieldType,
469})
470}
471if value, ok := ocuo.mutation.RootserviceJSON(); ok {
472_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
473Type: field.TypeString,
474Value: value,
475Column: obcluster.FieldRootserviceJSON,
476})
477}
478_node = &ObCluster{config: ocuo.config}
479_spec.Assign = _node.assignValues
480_spec.ScanValues = _node.scanValues
481if err = sqlgraph.UpdateNode(ctx, ocuo.driver, _spec); err != nil {
482if _, ok := err.(*sqlgraph.NotFoundError); ok {
483err = &NotFoundError{obcluster.Label}
484} else if sqlgraph.IsConstraintError(err) {
485err = &ConstraintError{err.Error(), err}
486}
487return nil, err
488}
489return _node, nil
490}
491