oceanbase

Форк
0
/
obcluster_update.go 
490 строк · 13.4 Кб
1
// Code generated by entc, DO NOT EDIT.
2

3
package ent
4

5
import (
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.
19
type ObClusterUpdate struct {
20
	config
21
	hooks    []Hook
22
	mutation *ObClusterMutation
23
}
24

25
// Where appends a list predicates to the ObClusterUpdate builder.
26
func (ocu *ObClusterUpdate) Where(ps ...predicate.ObCluster) *ObClusterUpdate {
27
	ocu.mutation.Where(ps...)
28
	return ocu
29
}
30

31
// SetCreateTime sets the "create_time" field.
32
func (ocu *ObClusterUpdate) SetCreateTime(t time.Time) *ObClusterUpdate {
33
	ocu.mutation.SetCreateTime(t)
34
	return ocu
35
}
36

37
// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
38
func (ocu *ObClusterUpdate) SetNillableCreateTime(t *time.Time) *ObClusterUpdate {
39
	if t != nil {
40
		ocu.SetCreateTime(*t)
41
	}
42
	return ocu
43
}
44

45
// SetUpdateTime sets the "update_time" field.
46
func (ocu *ObClusterUpdate) SetUpdateTime(t time.Time) *ObClusterUpdate {
47
	ocu.mutation.SetUpdateTime(t)
48
	return ocu
49
}
50

51
// SetName sets the "name" field.
52
func (ocu *ObClusterUpdate) SetName(s string) *ObClusterUpdate {
53
	ocu.mutation.SetName(s)
54
	return ocu
55
}
56

57
// SetObClusterID sets the "ob_cluster_id" field.
58
func (ocu *ObClusterUpdate) SetObClusterID(i int64) *ObClusterUpdate {
59
	ocu.mutation.ResetObClusterID()
60
	ocu.mutation.SetObClusterID(i)
61
	return ocu
62
}
63

64
// AddObClusterID adds i to the "ob_cluster_id" field.
65
func (ocu *ObClusterUpdate) AddObClusterID(i int64) *ObClusterUpdate {
66
	ocu.mutation.AddObClusterID(i)
67
	return ocu
68
}
69

70
// SetType sets the "type" field.
71
func (ocu *ObClusterUpdate) SetType(s string) *ObClusterUpdate {
72
	ocu.mutation.SetType(s)
73
	return ocu
74
}
75

76
// SetRootserviceJSON sets the "rootservice_json" field.
77
func (ocu *ObClusterUpdate) SetRootserviceJSON(s string) *ObClusterUpdate {
78
	ocu.mutation.SetRootserviceJSON(s)
79
	return ocu
80
}
81

82
// Mutation returns the ObClusterMutation object of the builder.
83
func (ocu *ObClusterUpdate) Mutation() *ObClusterMutation {
84
	return ocu.mutation
85
}
86

87
// Save executes the query and returns the number of nodes affected by the update operation.
88
func (ocu *ObClusterUpdate) Save(ctx context.Context) (int, error) {
89
	var (
90
		err      error
91
		affected int
92
	)
93
	ocu.defaults()
94
	if len(ocu.hooks) == 0 {
95
		if err = ocu.check(); err != nil {
96
			return 0, err
97
		}
98
		affected, err = ocu.sqlSave(ctx)
99
	} else {
100
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
101
			mutation, ok := m.(*ObClusterMutation)
102
			if !ok {
103
				return nil, fmt.Errorf("unexpected mutation type %T", m)
104
			}
105
			if err = ocu.check(); err != nil {
106
				return 0, err
107
			}
108
			ocu.mutation = mutation
109
			affected, err = ocu.sqlSave(ctx)
110
			mutation.done = true
111
			return affected, err
112
		})
113
		for i := len(ocu.hooks) - 1; i >= 0; i-- {
114
			if ocu.hooks[i] == nil {
115
				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
116
			}
117
			mut = ocu.hooks[i](mut)
118
		}
119
		if _, err := mut.Mutate(ctx, ocu.mutation); err != nil {
120
			return 0, err
121
		}
122
	}
123
	return affected, err
124
}
125

126
// SaveX is like Save, but panics if an error occurs.
127
func (ocu *ObClusterUpdate) SaveX(ctx context.Context) int {
128
	affected, err := ocu.Save(ctx)
129
	if err != nil {
130
		panic(err)
131
	}
132
	return affected
133
}
134

135
// Exec executes the query.
136
func (ocu *ObClusterUpdate) Exec(ctx context.Context) error {
137
	_, err := ocu.Save(ctx)
138
	return err
139
}
140

141
// ExecX is like Exec, but panics if an error occurs.
142
func (ocu *ObClusterUpdate) ExecX(ctx context.Context) {
143
	if err := ocu.Exec(ctx); err != nil {
144
		panic(err)
145
	}
146
}
147

148
// defaults sets the default values of the builder before save.
149
func (ocu *ObClusterUpdate) defaults() {
150
	if _, ok := ocu.mutation.UpdateTime(); !ok {
151
		v := obcluster.UpdateDefaultUpdateTime()
152
		ocu.mutation.SetUpdateTime(v)
153
	}
154
}
155

156
// check runs all checks and user-defined validators on the builder.
157
func (ocu *ObClusterUpdate) check() error {
158
	if v, ok := ocu.mutation.ObClusterID(); ok {
159
		if err := obcluster.ObClusterIDValidator(v); err != nil {
160
			return &ValidationError{Name: "ob_cluster_id", err: fmt.Errorf(`ent: validator failed for field "ObCluster.ob_cluster_id": %w`, err)}
161
		}
162
	}
163
	return nil
164
}
165

166
func (ocu *ObClusterUpdate) sqlSave(ctx context.Context) (n int, err error) {
167
	_spec := &sqlgraph.UpdateSpec{
168
		Node: &sqlgraph.NodeSpec{
169
			Table:   obcluster.Table,
170
			Columns: obcluster.Columns,
171
			ID: &sqlgraph.FieldSpec{
172
				Type:   field.TypeInt,
173
				Column: obcluster.FieldID,
174
			},
175
		},
176
	}
177
	if ps := ocu.mutation.predicates; len(ps) > 0 {
178
		_spec.Predicate = func(selector *sql.Selector) {
179
			for i := range ps {
180
				ps[i](selector)
181
			}
182
		}
183
	}
184
	if value, ok := ocu.mutation.CreateTime(); ok {
185
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
186
			Type:   field.TypeTime,
187
			Value:  value,
188
			Column: obcluster.FieldCreateTime,
189
		})
190
	}
191
	if value, ok := ocu.mutation.UpdateTime(); ok {
192
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
193
			Type:   field.TypeTime,
194
			Value:  value,
195
			Column: obcluster.FieldUpdateTime,
196
		})
197
	}
198
	if value, ok := ocu.mutation.Name(); ok {
199
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
200
			Type:   field.TypeString,
201
			Value:  value,
202
			Column: obcluster.FieldName,
203
		})
204
	}
205
	if value, ok := ocu.mutation.ObClusterID(); ok {
206
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
207
			Type:   field.TypeInt64,
208
			Value:  value,
209
			Column: obcluster.FieldObClusterID,
210
		})
211
	}
212
	if value, ok := ocu.mutation.AddedObClusterID(); ok {
213
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
214
			Type:   field.TypeInt64,
215
			Value:  value,
216
			Column: obcluster.FieldObClusterID,
217
		})
218
	}
219
	if value, ok := ocu.mutation.GetType(); ok {
220
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
221
			Type:   field.TypeString,
222
			Value:  value,
223
			Column: obcluster.FieldType,
224
		})
225
	}
226
	if value, ok := ocu.mutation.RootserviceJSON(); ok {
227
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
228
			Type:   field.TypeString,
229
			Value:  value,
230
			Column: obcluster.FieldRootserviceJSON,
231
		})
232
	}
233
	if n, err = sqlgraph.UpdateNodes(ctx, ocu.driver, _spec); err != nil {
234
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
235
			err = &NotFoundError{obcluster.Label}
236
		} else if sqlgraph.IsConstraintError(err) {
237
			err = &ConstraintError{err.Error(), err}
238
		}
239
		return 0, err
240
	}
241
	return n, nil
242
}
243

244
// ObClusterUpdateOne is the builder for updating a single ObCluster entity.
245
type ObClusterUpdateOne struct {
246
	config
247
	fields   []string
248
	hooks    []Hook
249
	mutation *ObClusterMutation
250
}
251

252
// SetCreateTime sets the "create_time" field.
253
func (ocuo *ObClusterUpdateOne) SetCreateTime(t time.Time) *ObClusterUpdateOne {
254
	ocuo.mutation.SetCreateTime(t)
255
	return ocuo
256
}
257

258
// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
259
func (ocuo *ObClusterUpdateOne) SetNillableCreateTime(t *time.Time) *ObClusterUpdateOne {
260
	if t != nil {
261
		ocuo.SetCreateTime(*t)
262
	}
263
	return ocuo
264
}
265

266
// SetUpdateTime sets the "update_time" field.
267
func (ocuo *ObClusterUpdateOne) SetUpdateTime(t time.Time) *ObClusterUpdateOne {
268
	ocuo.mutation.SetUpdateTime(t)
269
	return ocuo
270
}
271

272
// SetName sets the "name" field.
273
func (ocuo *ObClusterUpdateOne) SetName(s string) *ObClusterUpdateOne {
274
	ocuo.mutation.SetName(s)
275
	return ocuo
276
}
277

278
// SetObClusterID sets the "ob_cluster_id" field.
279
func (ocuo *ObClusterUpdateOne) SetObClusterID(i int64) *ObClusterUpdateOne {
280
	ocuo.mutation.ResetObClusterID()
281
	ocuo.mutation.SetObClusterID(i)
282
	return ocuo
283
}
284

285
// AddObClusterID adds i to the "ob_cluster_id" field.
286
func (ocuo *ObClusterUpdateOne) AddObClusterID(i int64) *ObClusterUpdateOne {
287
	ocuo.mutation.AddObClusterID(i)
288
	return ocuo
289
}
290

291
// SetType sets the "type" field.
292
func (ocuo *ObClusterUpdateOne) SetType(s string) *ObClusterUpdateOne {
293
	ocuo.mutation.SetType(s)
294
	return ocuo
295
}
296

297
// SetRootserviceJSON sets the "rootservice_json" field.
298
func (ocuo *ObClusterUpdateOne) SetRootserviceJSON(s string) *ObClusterUpdateOne {
299
	ocuo.mutation.SetRootserviceJSON(s)
300
	return ocuo
301
}
302

303
// Mutation returns the ObClusterMutation object of the builder.
304
func (ocuo *ObClusterUpdateOne) Mutation() *ObClusterMutation {
305
	return 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.
310
func (ocuo *ObClusterUpdateOne) Select(field string, fields ...string) *ObClusterUpdateOne {
311
	ocuo.fields = append([]string{field}, fields...)
312
	return ocuo
313
}
314

315
// Save executes the query and returns the updated ObCluster entity.
316
func (ocuo *ObClusterUpdateOne) Save(ctx context.Context) (*ObCluster, error) {
317
	var (
318
		err  error
319
		node *ObCluster
320
	)
321
	ocuo.defaults()
322
	if len(ocuo.hooks) == 0 {
323
		if err = ocuo.check(); err != nil {
324
			return nil, err
325
		}
326
		node, err = ocuo.sqlSave(ctx)
327
	} else {
328
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
329
			mutation, ok := m.(*ObClusterMutation)
330
			if !ok {
331
				return nil, fmt.Errorf("unexpected mutation type %T", m)
332
			}
333
			if err = ocuo.check(); err != nil {
334
				return nil, err
335
			}
336
			ocuo.mutation = mutation
337
			node, err = ocuo.sqlSave(ctx)
338
			mutation.done = true
339
			return node, err
340
		})
341
		for i := len(ocuo.hooks) - 1; i >= 0; i-- {
342
			if ocuo.hooks[i] == nil {
343
				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
344
			}
345
			mut = ocuo.hooks[i](mut)
346
		}
347
		if _, err := mut.Mutate(ctx, ocuo.mutation); err != nil {
348
			return nil, err
349
		}
350
	}
351
	return node, err
352
}
353

354
// SaveX is like Save, but panics if an error occurs.
355
func (ocuo *ObClusterUpdateOne) SaveX(ctx context.Context) *ObCluster {
356
	node, err := ocuo.Save(ctx)
357
	if err != nil {
358
		panic(err)
359
	}
360
	return node
361
}
362

363
// Exec executes the query on the entity.
364
func (ocuo *ObClusterUpdateOne) Exec(ctx context.Context) error {
365
	_, err := ocuo.Save(ctx)
366
	return err
367
}
368

369
// ExecX is like Exec, but panics if an error occurs.
370
func (ocuo *ObClusterUpdateOne) ExecX(ctx context.Context) {
371
	if err := ocuo.Exec(ctx); err != nil {
372
		panic(err)
373
	}
374
}
375

376
// defaults sets the default values of the builder before save.
377
func (ocuo *ObClusterUpdateOne) defaults() {
378
	if _, ok := ocuo.mutation.UpdateTime(); !ok {
379
		v := obcluster.UpdateDefaultUpdateTime()
380
		ocuo.mutation.SetUpdateTime(v)
381
	}
382
}
383

384
// check runs all checks and user-defined validators on the builder.
385
func (ocuo *ObClusterUpdateOne) check() error {
386
	if v, ok := ocuo.mutation.ObClusterID(); ok {
387
		if err := obcluster.ObClusterIDValidator(v); err != nil {
388
			return &ValidationError{Name: "ob_cluster_id", err: fmt.Errorf(`ent: validator failed for field "ObCluster.ob_cluster_id": %w`, err)}
389
		}
390
	}
391
	return nil
392
}
393

394
func (ocuo *ObClusterUpdateOne) sqlSave(ctx context.Context) (_node *ObCluster, err error) {
395
	_spec := &sqlgraph.UpdateSpec{
396
		Node: &sqlgraph.NodeSpec{
397
			Table:   obcluster.Table,
398
			Columns: obcluster.Columns,
399
			ID: &sqlgraph.FieldSpec{
400
				Type:   field.TypeInt,
401
				Column: obcluster.FieldID,
402
			},
403
		},
404
	}
405
	id, ok := ocuo.mutation.ID()
406
	if !ok {
407
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ObCluster.id" for update`)}
408
	}
409
	_spec.Node.ID.Value = id
410
	if 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)
413
		for _, f := range fields {
414
			if !obcluster.ValidColumn(f) {
415
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
416
			}
417
			if f != obcluster.FieldID {
418
				_spec.Node.Columns = append(_spec.Node.Columns, f)
419
			}
420
		}
421
	}
422
	if ps := ocuo.mutation.predicates; len(ps) > 0 {
423
		_spec.Predicate = func(selector *sql.Selector) {
424
			for i := range ps {
425
				ps[i](selector)
426
			}
427
		}
428
	}
429
	if value, ok := ocuo.mutation.CreateTime(); ok {
430
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
431
			Type:   field.TypeTime,
432
			Value:  value,
433
			Column: obcluster.FieldCreateTime,
434
		})
435
	}
436
	if value, ok := ocuo.mutation.UpdateTime(); ok {
437
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
438
			Type:   field.TypeTime,
439
			Value:  value,
440
			Column: obcluster.FieldUpdateTime,
441
		})
442
	}
443
	if value, ok := ocuo.mutation.Name(); ok {
444
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
445
			Type:   field.TypeString,
446
			Value:  value,
447
			Column: obcluster.FieldName,
448
		})
449
	}
450
	if value, ok := ocuo.mutation.ObClusterID(); ok {
451
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
452
			Type:   field.TypeInt64,
453
			Value:  value,
454
			Column: obcluster.FieldObClusterID,
455
		})
456
	}
457
	if value, ok := ocuo.mutation.AddedObClusterID(); ok {
458
		_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
459
			Type:   field.TypeInt64,
460
			Value:  value,
461
			Column: obcluster.FieldObClusterID,
462
		})
463
	}
464
	if value, ok := ocuo.mutation.GetType(); ok {
465
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
466
			Type:   field.TypeString,
467
			Value:  value,
468
			Column: obcluster.FieldType,
469
		})
470
	}
471
	if value, ok := ocuo.mutation.RootserviceJSON(); ok {
472
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
473
			Type:   field.TypeString,
474
			Value:  value,
475
			Column: obcluster.FieldRootserviceJSON,
476
		})
477
	}
478
	_node = &ObCluster{config: ocuo.config}
479
	_spec.Assign = _node.assignValues
480
	_spec.ScanValues = _node.scanValues
481
	if err = sqlgraph.UpdateNode(ctx, ocuo.driver, _spec); err != nil {
482
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
483
			err = &NotFoundError{obcluster.Label}
484
		} else if sqlgraph.IsConstraintError(err) {
485
			err = &ConstraintError{err.Error(), err}
486
		}
487
		return nil, err
488
	}
489
	return _node, nil
490
}
491

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.