oceanbase

Форк
0
/
obcluster_query.go 
921 строка · 24.1 Кб
1
// Code generated by entc, DO NOT EDIT.
2

3
package ent
4

5
import (
6
	"context"
7
	"errors"
8
	"fmt"
9
	"math"
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
// ObClusterQuery is the builder for querying ObCluster entities.
19
type ObClusterQuery struct {
20
	config
21
	limit      *int
22
	offset     *int
23
	unique     *bool
24
	order      []OrderFunc
25
	fields     []string
26
	predicates []predicate.ObCluster
27
	// intermediate query (i.e. traversal path).
28
	sql  *sql.Selector
29
	path func(context.Context) (*sql.Selector, error)
30
}
31

32
// Where adds a new predicate for the ObClusterQuery builder.
33
func (ocq *ObClusterQuery) Where(ps ...predicate.ObCluster) *ObClusterQuery {
34
	ocq.predicates = append(ocq.predicates, ps...)
35
	return ocq
36
}
37

38
// Limit adds a limit step to the query.
39
func (ocq *ObClusterQuery) Limit(limit int) *ObClusterQuery {
40
	ocq.limit = &limit
41
	return ocq
42
}
43

44
// Offset adds an offset step to the query.
45
func (ocq *ObClusterQuery) Offset(offset int) *ObClusterQuery {
46
	ocq.offset = &offset
47
	return ocq
48
}
49

50
// Unique configures the query builder to filter duplicate records on query.
51
// By default, unique is set to true, and can be disabled using this method.
52
func (ocq *ObClusterQuery) Unique(unique bool) *ObClusterQuery {
53
	ocq.unique = &unique
54
	return ocq
55
}
56

57
// Order adds an order step to the query.
58
func (ocq *ObClusterQuery) Order(o ...OrderFunc) *ObClusterQuery {
59
	ocq.order = append(ocq.order, o...)
60
	return ocq
61
}
62

63
// First returns the first ObCluster entity from the query.
64
// Returns a *NotFoundError when no ObCluster was found.
65
func (ocq *ObClusterQuery) First(ctx context.Context) (*ObCluster, error) {
66
	nodes, err := ocq.Limit(1).All(ctx)
67
	if err != nil {
68
		return nil, err
69
	}
70
	if len(nodes) == 0 {
71
		return nil, &NotFoundError{obcluster.Label}
72
	}
73
	return nodes[0], nil
74
}
75

76
// FirstX is like First, but panics if an error occurs.
77
func (ocq *ObClusterQuery) FirstX(ctx context.Context) *ObCluster {
78
	node, err := ocq.First(ctx)
79
	if err != nil && !IsNotFound(err) {
80
		panic(err)
81
	}
82
	return node
83
}
84

85
// FirstID returns the first ObCluster ID from the query.
86
// Returns a *NotFoundError when no ObCluster ID was found.
87
func (ocq *ObClusterQuery) FirstID(ctx context.Context) (id int, err error) {
88
	var ids []int
89
	if ids, err = ocq.Limit(1).IDs(ctx); err != nil {
90
		return
91
	}
92
	if len(ids) == 0 {
93
		err = &NotFoundError{obcluster.Label}
94
		return
95
	}
96
	return ids[0], nil
97
}
98

99
// FirstIDX is like FirstID, but panics if an error occurs.
100
func (ocq *ObClusterQuery) FirstIDX(ctx context.Context) int {
101
	id, err := ocq.FirstID(ctx)
102
	if err != nil && !IsNotFound(err) {
103
		panic(err)
104
	}
105
	return id
106
}
107

108
// Only returns a single ObCluster entity found by the query, ensuring it only returns one.
109
// Returns a *NotSingularError when more than one ObCluster entity is found.
110
// Returns a *NotFoundError when no ObCluster entities are found.
111
func (ocq *ObClusterQuery) Only(ctx context.Context) (*ObCluster, error) {
112
	nodes, err := ocq.Limit(2).All(ctx)
113
	if err != nil {
114
		return nil, err
115
	}
116
	switch len(nodes) {
117
	case 1:
118
		return nodes[0], nil
119
	case 0:
120
		return nil, &NotFoundError{obcluster.Label}
121
	default:
122
		return nil, &NotSingularError{obcluster.Label}
123
	}
124
}
125

126
// OnlyX is like Only, but panics if an error occurs.
127
func (ocq *ObClusterQuery) OnlyX(ctx context.Context) *ObCluster {
128
	node, err := ocq.Only(ctx)
129
	if err != nil {
130
		panic(err)
131
	}
132
	return node
133
}
134

135
// OnlyID is like Only, but returns the only ObCluster ID in the query.
136
// Returns a *NotSingularError when more than one ObCluster ID is found.
137
// Returns a *NotFoundError when no entities are found.
138
func (ocq *ObClusterQuery) OnlyID(ctx context.Context) (id int, err error) {
139
	var ids []int
140
	if ids, err = ocq.Limit(2).IDs(ctx); err != nil {
141
		return
142
	}
143
	switch len(ids) {
144
	case 1:
145
		id = ids[0]
146
	case 0:
147
		err = &NotFoundError{obcluster.Label}
148
	default:
149
		err = &NotSingularError{obcluster.Label}
150
	}
151
	return
152
}
153

154
// OnlyIDX is like OnlyID, but panics if an error occurs.
155
func (ocq *ObClusterQuery) OnlyIDX(ctx context.Context) int {
156
	id, err := ocq.OnlyID(ctx)
157
	if err != nil {
158
		panic(err)
159
	}
160
	return id
161
}
162

163
// All executes the query and returns a list of ObClusters.
164
func (ocq *ObClusterQuery) All(ctx context.Context) ([]*ObCluster, error) {
165
	if err := ocq.prepareQuery(ctx); err != nil {
166
		return nil, err
167
	}
168
	return ocq.sqlAll(ctx)
169
}
170

171
// AllX is like All, but panics if an error occurs.
172
func (ocq *ObClusterQuery) AllX(ctx context.Context) []*ObCluster {
173
	nodes, err := ocq.All(ctx)
174
	if err != nil {
175
		panic(err)
176
	}
177
	return nodes
178
}
179

180
// IDs executes the query and returns a list of ObCluster IDs.
181
func (ocq *ObClusterQuery) IDs(ctx context.Context) ([]int, error) {
182
	var ids []int
183
	if err := ocq.Select(obcluster.FieldID).Scan(ctx, &ids); err != nil {
184
		return nil, err
185
	}
186
	return ids, nil
187
}
188

189
// IDsX is like IDs, but panics if an error occurs.
190
func (ocq *ObClusterQuery) IDsX(ctx context.Context) []int {
191
	ids, err := ocq.IDs(ctx)
192
	if err != nil {
193
		panic(err)
194
	}
195
	return ids
196
}
197

198
// Count returns the count of the given query.
199
func (ocq *ObClusterQuery) Count(ctx context.Context) (int, error) {
200
	if err := ocq.prepareQuery(ctx); err != nil {
201
		return 0, err
202
	}
203
	return ocq.sqlCount(ctx)
204
}
205

206
// CountX is like Count, but panics if an error occurs.
207
func (ocq *ObClusterQuery) CountX(ctx context.Context) int {
208
	count, err := ocq.Count(ctx)
209
	if err != nil {
210
		panic(err)
211
	}
212
	return count
213
}
214

215
// Exist returns true if the query has elements in the graph.
216
func (ocq *ObClusterQuery) Exist(ctx context.Context) (bool, error) {
217
	if err := ocq.prepareQuery(ctx); err != nil {
218
		return false, err
219
	}
220
	return ocq.sqlExist(ctx)
221
}
222

223
// ExistX is like Exist, but panics if an error occurs.
224
func (ocq *ObClusterQuery) ExistX(ctx context.Context) bool {
225
	exist, err := ocq.Exist(ctx)
226
	if err != nil {
227
		panic(err)
228
	}
229
	return exist
230
}
231

232
// Clone returns a duplicate of the ObClusterQuery builder, including all associated steps. It can be
233
// used to prepare common query builders and use them differently after the clone is made.
234
func (ocq *ObClusterQuery) Clone() *ObClusterQuery {
235
	if ocq == nil {
236
		return nil
237
	}
238
	return &ObClusterQuery{
239
		config:     ocq.config,
240
		limit:      ocq.limit,
241
		offset:     ocq.offset,
242
		order:      append([]OrderFunc{}, ocq.order...),
243
		predicates: append([]predicate.ObCluster{}, ocq.predicates...),
244
		// clone intermediate query.
245
		sql:    ocq.sql.Clone(),
246
		path:   ocq.path,
247
		unique: ocq.unique,
248
	}
249
}
250

251
// GroupBy is used to group vertices by one or more fields/columns.
252
// It is often used with aggregate functions, like: count, max, mean, min, sum.
253
//
254
// Example:
255
//
256
//	var v []struct {
257
//		CreateTime time.Time `json:"create_time,omitempty"`
258
//		Count int `json:"count,omitempty"`
259
//	}
260
//
261
//	client.ObCluster.Query().
262
//		GroupBy(obcluster.FieldCreateTime).
263
//		Aggregate(ent.Count()).
264
//		Scan(ctx, &v)
265
//
266
func (ocq *ObClusterQuery) GroupBy(field string, fields ...string) *ObClusterGroupBy {
267
	group := &ObClusterGroupBy{config: ocq.config}
268
	group.fields = append([]string{field}, fields...)
269
	group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
270
		if err := ocq.prepareQuery(ctx); err != nil {
271
			return nil, err
272
		}
273
		return ocq.sqlQuery(ctx), nil
274
	}
275
	return group
276
}
277

278
// Select allows the selection one or more fields/columns for the given query,
279
// instead of selecting all fields in the entity.
280
//
281
// Example:
282
//
283
//	var v []struct {
284
//		CreateTime time.Time `json:"create_time,omitempty"`
285
//	}
286
//
287
//	client.ObCluster.Query().
288
//		Select(obcluster.FieldCreateTime).
289
//		Scan(ctx, &v)
290
//
291
func (ocq *ObClusterQuery) Select(fields ...string) *ObClusterSelect {
292
	ocq.fields = append(ocq.fields, fields...)
293
	return &ObClusterSelect{ObClusterQuery: ocq}
294
}
295

296
func (ocq *ObClusterQuery) prepareQuery(ctx context.Context) error {
297
	for _, f := range ocq.fields {
298
		if !obcluster.ValidColumn(f) {
299
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
300
		}
301
	}
302
	if ocq.path != nil {
303
		prev, err := ocq.path(ctx)
304
		if err != nil {
305
			return err
306
		}
307
		ocq.sql = prev
308
	}
309
	return nil
310
}
311

312
func (ocq *ObClusterQuery) sqlAll(ctx context.Context) ([]*ObCluster, error) {
313
	var (
314
		nodes = []*ObCluster{}
315
		_spec = ocq.querySpec()
316
	)
317
	_spec.ScanValues = func(columns []string) ([]interface{}, error) {
318
		node := &ObCluster{config: ocq.config}
319
		nodes = append(nodes, node)
320
		return node.scanValues(columns)
321
	}
322
	_spec.Assign = func(columns []string, values []interface{}) error {
323
		if len(nodes) == 0 {
324
			return fmt.Errorf("ent: Assign called without calling ScanValues")
325
		}
326
		node := nodes[len(nodes)-1]
327
		return node.assignValues(columns, values)
328
	}
329
	if err := sqlgraph.QueryNodes(ctx, ocq.driver, _spec); err != nil {
330
		return nil, err
331
	}
332
	if len(nodes) == 0 {
333
		return nodes, nil
334
	}
335
	return nodes, nil
336
}
337

338
func (ocq *ObClusterQuery) sqlCount(ctx context.Context) (int, error) {
339
	_spec := ocq.querySpec()
340
	_spec.Node.Columns = ocq.fields
341
	if len(ocq.fields) > 0 {
342
		_spec.Unique = ocq.unique != nil && *ocq.unique
343
	}
344
	return sqlgraph.CountNodes(ctx, ocq.driver, _spec)
345
}
346

347
func (ocq *ObClusterQuery) sqlExist(ctx context.Context) (bool, error) {
348
	n, err := ocq.sqlCount(ctx)
349
	if err != nil {
350
		return false, fmt.Errorf("ent: check existence: %w", err)
351
	}
352
	return n > 0, nil
353
}
354

355
func (ocq *ObClusterQuery) querySpec() *sqlgraph.QuerySpec {
356
	_spec := &sqlgraph.QuerySpec{
357
		Node: &sqlgraph.NodeSpec{
358
			Table:   obcluster.Table,
359
			Columns: obcluster.Columns,
360
			ID: &sqlgraph.FieldSpec{
361
				Type:   field.TypeInt,
362
				Column: obcluster.FieldID,
363
			},
364
		},
365
		From:   ocq.sql,
366
		Unique: true,
367
	}
368
	if unique := ocq.unique; unique != nil {
369
		_spec.Unique = *unique
370
	}
371
	if fields := ocq.fields; len(fields) > 0 {
372
		_spec.Node.Columns = make([]string, 0, len(fields))
373
		_spec.Node.Columns = append(_spec.Node.Columns, obcluster.FieldID)
374
		for i := range fields {
375
			if fields[i] != obcluster.FieldID {
376
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
377
			}
378
		}
379
	}
380
	if ps := ocq.predicates; len(ps) > 0 {
381
		_spec.Predicate = func(selector *sql.Selector) {
382
			for i := range ps {
383
				ps[i](selector)
384
			}
385
		}
386
	}
387
	if limit := ocq.limit; limit != nil {
388
		_spec.Limit = *limit
389
	}
390
	if offset := ocq.offset; offset != nil {
391
		_spec.Offset = *offset
392
	}
393
	if ps := ocq.order; len(ps) > 0 {
394
		_spec.Order = func(selector *sql.Selector) {
395
			for i := range ps {
396
				ps[i](selector)
397
			}
398
		}
399
	}
400
	return _spec
401
}
402

403
func (ocq *ObClusterQuery) sqlQuery(ctx context.Context) *sql.Selector {
404
	builder := sql.Dialect(ocq.driver.Dialect())
405
	t1 := builder.Table(obcluster.Table)
406
	columns := ocq.fields
407
	if len(columns) == 0 {
408
		columns = obcluster.Columns
409
	}
410
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
411
	if ocq.sql != nil {
412
		selector = ocq.sql
413
		selector.Select(selector.Columns(columns...)...)
414
	}
415
	if ocq.unique != nil && *ocq.unique {
416
		selector.Distinct()
417
	}
418
	for _, p := range ocq.predicates {
419
		p(selector)
420
	}
421
	for _, p := range ocq.order {
422
		p(selector)
423
	}
424
	if offset := ocq.offset; offset != nil {
425
		// limit is mandatory for offset clause. We start
426
		// with default value, and override it below if needed.
427
		selector.Offset(*offset).Limit(math.MaxInt32)
428
	}
429
	if limit := ocq.limit; limit != nil {
430
		selector.Limit(*limit)
431
	}
432
	return selector
433
}
434

435
// ObClusterGroupBy is the group-by builder for ObCluster entities.
436
type ObClusterGroupBy struct {
437
	config
438
	fields []string
439
	fns    []AggregateFunc
440
	// intermediate query (i.e. traversal path).
441
	sql  *sql.Selector
442
	path func(context.Context) (*sql.Selector, error)
443
}
444

445
// Aggregate adds the given aggregation functions to the group-by query.
446
func (ocgb *ObClusterGroupBy) Aggregate(fns ...AggregateFunc) *ObClusterGroupBy {
447
	ocgb.fns = append(ocgb.fns, fns...)
448
	return ocgb
449
}
450

451
// Scan applies the group-by query and scans the result into the given value.
452
func (ocgb *ObClusterGroupBy) Scan(ctx context.Context, v interface{}) error {
453
	query, err := ocgb.path(ctx)
454
	if err != nil {
455
		return err
456
	}
457
	ocgb.sql = query
458
	return ocgb.sqlScan(ctx, v)
459
}
460

461
// ScanX is like Scan, but panics if an error occurs.
462
func (ocgb *ObClusterGroupBy) ScanX(ctx context.Context, v interface{}) {
463
	if err := ocgb.Scan(ctx, v); err != nil {
464
		panic(err)
465
	}
466
}
467

468
// Strings returns list of strings from group-by.
469
// It is only allowed when executing a group-by query with one field.
470
func (ocgb *ObClusterGroupBy) Strings(ctx context.Context) ([]string, error) {
471
	if len(ocgb.fields) > 1 {
472
		return nil, errors.New("ent: ObClusterGroupBy.Strings is not achievable when grouping more than 1 field")
473
	}
474
	var v []string
475
	if err := ocgb.Scan(ctx, &v); err != nil {
476
		return nil, err
477
	}
478
	return v, nil
479
}
480

481
// StringsX is like Strings, but panics if an error occurs.
482
func (ocgb *ObClusterGroupBy) StringsX(ctx context.Context) []string {
483
	v, err := ocgb.Strings(ctx)
484
	if err != nil {
485
		panic(err)
486
	}
487
	return v
488
}
489

490
// String returns a single string from a group-by query.
491
// It is only allowed when executing a group-by query with one field.
492
func (ocgb *ObClusterGroupBy) String(ctx context.Context) (_ string, err error) {
493
	var v []string
494
	if v, err = ocgb.Strings(ctx); err != nil {
495
		return
496
	}
497
	switch len(v) {
498
	case 1:
499
		return v[0], nil
500
	case 0:
501
		err = &NotFoundError{obcluster.Label}
502
	default:
503
		err = fmt.Errorf("ent: ObClusterGroupBy.Strings returned %d results when one was expected", len(v))
504
	}
505
	return
506
}
507

508
// StringX is like String, but panics if an error occurs.
509
func (ocgb *ObClusterGroupBy) StringX(ctx context.Context) string {
510
	v, err := ocgb.String(ctx)
511
	if err != nil {
512
		panic(err)
513
	}
514
	return v
515
}
516

517
// Ints returns list of ints from group-by.
518
// It is only allowed when executing a group-by query with one field.
519
func (ocgb *ObClusterGroupBy) Ints(ctx context.Context) ([]int, error) {
520
	if len(ocgb.fields) > 1 {
521
		return nil, errors.New("ent: ObClusterGroupBy.Ints is not achievable when grouping more than 1 field")
522
	}
523
	var v []int
524
	if err := ocgb.Scan(ctx, &v); err != nil {
525
		return nil, err
526
	}
527
	return v, nil
528
}
529

530
// IntsX is like Ints, but panics if an error occurs.
531
func (ocgb *ObClusterGroupBy) IntsX(ctx context.Context) []int {
532
	v, err := ocgb.Ints(ctx)
533
	if err != nil {
534
		panic(err)
535
	}
536
	return v
537
}
538

539
// Int returns a single int from a group-by query.
540
// It is only allowed when executing a group-by query with one field.
541
func (ocgb *ObClusterGroupBy) Int(ctx context.Context) (_ int, err error) {
542
	var v []int
543
	if v, err = ocgb.Ints(ctx); err != nil {
544
		return
545
	}
546
	switch len(v) {
547
	case 1:
548
		return v[0], nil
549
	case 0:
550
		err = &NotFoundError{obcluster.Label}
551
	default:
552
		err = fmt.Errorf("ent: ObClusterGroupBy.Ints returned %d results when one was expected", len(v))
553
	}
554
	return
555
}
556

557
// IntX is like Int, but panics if an error occurs.
558
func (ocgb *ObClusterGroupBy) IntX(ctx context.Context) int {
559
	v, err := ocgb.Int(ctx)
560
	if err != nil {
561
		panic(err)
562
	}
563
	return v
564
}
565

566
// Float64s returns list of float64s from group-by.
567
// It is only allowed when executing a group-by query with one field.
568
func (ocgb *ObClusterGroupBy) Float64s(ctx context.Context) ([]float64, error) {
569
	if len(ocgb.fields) > 1 {
570
		return nil, errors.New("ent: ObClusterGroupBy.Float64s is not achievable when grouping more than 1 field")
571
	}
572
	var v []float64
573
	if err := ocgb.Scan(ctx, &v); err != nil {
574
		return nil, err
575
	}
576
	return v, nil
577
}
578

579
// Float64sX is like Float64s, but panics if an error occurs.
580
func (ocgb *ObClusterGroupBy) Float64sX(ctx context.Context) []float64 {
581
	v, err := ocgb.Float64s(ctx)
582
	if err != nil {
583
		panic(err)
584
	}
585
	return v
586
}
587

588
// Float64 returns a single float64 from a group-by query.
589
// It is only allowed when executing a group-by query with one field.
590
func (ocgb *ObClusterGroupBy) Float64(ctx context.Context) (_ float64, err error) {
591
	var v []float64
592
	if v, err = ocgb.Float64s(ctx); err != nil {
593
		return
594
	}
595
	switch len(v) {
596
	case 1:
597
		return v[0], nil
598
	case 0:
599
		err = &NotFoundError{obcluster.Label}
600
	default:
601
		err = fmt.Errorf("ent: ObClusterGroupBy.Float64s returned %d results when one was expected", len(v))
602
	}
603
	return
604
}
605

606
// Float64X is like Float64, but panics if an error occurs.
607
func (ocgb *ObClusterGroupBy) Float64X(ctx context.Context) float64 {
608
	v, err := ocgb.Float64(ctx)
609
	if err != nil {
610
		panic(err)
611
	}
612
	return v
613
}
614

615
// Bools returns list of bools from group-by.
616
// It is only allowed when executing a group-by query with one field.
617
func (ocgb *ObClusterGroupBy) Bools(ctx context.Context) ([]bool, error) {
618
	if len(ocgb.fields) > 1 {
619
		return nil, errors.New("ent: ObClusterGroupBy.Bools is not achievable when grouping more than 1 field")
620
	}
621
	var v []bool
622
	if err := ocgb.Scan(ctx, &v); err != nil {
623
		return nil, err
624
	}
625
	return v, nil
626
}
627

628
// BoolsX is like Bools, but panics if an error occurs.
629
func (ocgb *ObClusterGroupBy) BoolsX(ctx context.Context) []bool {
630
	v, err := ocgb.Bools(ctx)
631
	if err != nil {
632
		panic(err)
633
	}
634
	return v
635
}
636

637
// Bool returns a single bool from a group-by query.
638
// It is only allowed when executing a group-by query with one field.
639
func (ocgb *ObClusterGroupBy) Bool(ctx context.Context) (_ bool, err error) {
640
	var v []bool
641
	if v, err = ocgb.Bools(ctx); err != nil {
642
		return
643
	}
644
	switch len(v) {
645
	case 1:
646
		return v[0], nil
647
	case 0:
648
		err = &NotFoundError{obcluster.Label}
649
	default:
650
		err = fmt.Errorf("ent: ObClusterGroupBy.Bools returned %d results when one was expected", len(v))
651
	}
652
	return
653
}
654

655
// BoolX is like Bool, but panics if an error occurs.
656
func (ocgb *ObClusterGroupBy) BoolX(ctx context.Context) bool {
657
	v, err := ocgb.Bool(ctx)
658
	if err != nil {
659
		panic(err)
660
	}
661
	return v
662
}
663

664
func (ocgb *ObClusterGroupBy) sqlScan(ctx context.Context, v interface{}) error {
665
	for _, f := range ocgb.fields {
666
		if !obcluster.ValidColumn(f) {
667
			return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
668
		}
669
	}
670
	selector := ocgb.sqlQuery()
671
	if err := selector.Err(); err != nil {
672
		return err
673
	}
674
	rows := &sql.Rows{}
675
	query, args := selector.Query()
676
	if err := ocgb.driver.Query(ctx, query, args, rows); err != nil {
677
		return err
678
	}
679
	defer rows.Close()
680
	return sql.ScanSlice(rows, v)
681
}
682

683
func (ocgb *ObClusterGroupBy) sqlQuery() *sql.Selector {
684
	selector := ocgb.sql.Select()
685
	aggregation := make([]string, 0, len(ocgb.fns))
686
	for _, fn := range ocgb.fns {
687
		aggregation = append(aggregation, fn(selector))
688
	}
689
	// If no columns were selected in a custom aggregation function, the default
690
	// selection is the fields used for "group-by", and the aggregation functions.
691
	if len(selector.SelectedColumns()) == 0 {
692
		columns := make([]string, 0, len(ocgb.fields)+len(ocgb.fns))
693
		for _, f := range ocgb.fields {
694
			columns = append(columns, selector.C(f))
695
		}
696
		columns = append(columns, aggregation...)
697
		selector.Select(columns...)
698
	}
699
	return selector.GroupBy(selector.Columns(ocgb.fields...)...)
700
}
701

702
// ObClusterSelect is the builder for selecting fields of ObCluster entities.
703
type ObClusterSelect struct {
704
	*ObClusterQuery
705
	// intermediate query (i.e. traversal path).
706
	sql *sql.Selector
707
}
708

709
// Scan applies the selector query and scans the result into the given value.
710
func (ocs *ObClusterSelect) Scan(ctx context.Context, v interface{}) error {
711
	if err := ocs.prepareQuery(ctx); err != nil {
712
		return err
713
	}
714
	ocs.sql = ocs.ObClusterQuery.sqlQuery(ctx)
715
	return ocs.sqlScan(ctx, v)
716
}
717

718
// ScanX is like Scan, but panics if an error occurs.
719
func (ocs *ObClusterSelect) ScanX(ctx context.Context, v interface{}) {
720
	if err := ocs.Scan(ctx, v); err != nil {
721
		panic(err)
722
	}
723
}
724

725
// Strings returns list of strings from a selector. It is only allowed when selecting one field.
726
func (ocs *ObClusterSelect) Strings(ctx context.Context) ([]string, error) {
727
	if len(ocs.fields) > 1 {
728
		return nil, errors.New("ent: ObClusterSelect.Strings is not achievable when selecting more than 1 field")
729
	}
730
	var v []string
731
	if err := ocs.Scan(ctx, &v); err != nil {
732
		return nil, err
733
	}
734
	return v, nil
735
}
736

737
// StringsX is like Strings, but panics if an error occurs.
738
func (ocs *ObClusterSelect) StringsX(ctx context.Context) []string {
739
	v, err := ocs.Strings(ctx)
740
	if err != nil {
741
		panic(err)
742
	}
743
	return v
744
}
745

746
// String returns a single string from a selector. It is only allowed when selecting one field.
747
func (ocs *ObClusterSelect) String(ctx context.Context) (_ string, err error) {
748
	var v []string
749
	if v, err = ocs.Strings(ctx); err != nil {
750
		return
751
	}
752
	switch len(v) {
753
	case 1:
754
		return v[0], nil
755
	case 0:
756
		err = &NotFoundError{obcluster.Label}
757
	default:
758
		err = fmt.Errorf("ent: ObClusterSelect.Strings returned %d results when one was expected", len(v))
759
	}
760
	return
761
}
762

763
// StringX is like String, but panics if an error occurs.
764
func (ocs *ObClusterSelect) StringX(ctx context.Context) string {
765
	v, err := ocs.String(ctx)
766
	if err != nil {
767
		panic(err)
768
	}
769
	return v
770
}
771

772
// Ints returns list of ints from a selector. It is only allowed when selecting one field.
773
func (ocs *ObClusterSelect) Ints(ctx context.Context) ([]int, error) {
774
	if len(ocs.fields) > 1 {
775
		return nil, errors.New("ent: ObClusterSelect.Ints is not achievable when selecting more than 1 field")
776
	}
777
	var v []int
778
	if err := ocs.Scan(ctx, &v); err != nil {
779
		return nil, err
780
	}
781
	return v, nil
782
}
783

784
// IntsX is like Ints, but panics if an error occurs.
785
func (ocs *ObClusterSelect) IntsX(ctx context.Context) []int {
786
	v, err := ocs.Ints(ctx)
787
	if err != nil {
788
		panic(err)
789
	}
790
	return v
791
}
792

793
// Int returns a single int from a selector. It is only allowed when selecting one field.
794
func (ocs *ObClusterSelect) Int(ctx context.Context) (_ int, err error) {
795
	var v []int
796
	if v, err = ocs.Ints(ctx); err != nil {
797
		return
798
	}
799
	switch len(v) {
800
	case 1:
801
		return v[0], nil
802
	case 0:
803
		err = &NotFoundError{obcluster.Label}
804
	default:
805
		err = fmt.Errorf("ent: ObClusterSelect.Ints returned %d results when one was expected", len(v))
806
	}
807
	return
808
}
809

810
// IntX is like Int, but panics if an error occurs.
811
func (ocs *ObClusterSelect) IntX(ctx context.Context) int {
812
	v, err := ocs.Int(ctx)
813
	if err != nil {
814
		panic(err)
815
	}
816
	return v
817
}
818

819
// Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
820
func (ocs *ObClusterSelect) Float64s(ctx context.Context) ([]float64, error) {
821
	if len(ocs.fields) > 1 {
822
		return nil, errors.New("ent: ObClusterSelect.Float64s is not achievable when selecting more than 1 field")
823
	}
824
	var v []float64
825
	if err := ocs.Scan(ctx, &v); err != nil {
826
		return nil, err
827
	}
828
	return v, nil
829
}
830

831
// Float64sX is like Float64s, but panics if an error occurs.
832
func (ocs *ObClusterSelect) Float64sX(ctx context.Context) []float64 {
833
	v, err := ocs.Float64s(ctx)
834
	if err != nil {
835
		panic(err)
836
	}
837
	return v
838
}
839

840
// Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
841
func (ocs *ObClusterSelect) Float64(ctx context.Context) (_ float64, err error) {
842
	var v []float64
843
	if v, err = ocs.Float64s(ctx); err != nil {
844
		return
845
	}
846
	switch len(v) {
847
	case 1:
848
		return v[0], nil
849
	case 0:
850
		err = &NotFoundError{obcluster.Label}
851
	default:
852
		err = fmt.Errorf("ent: ObClusterSelect.Float64s returned %d results when one was expected", len(v))
853
	}
854
	return
855
}
856

857
// Float64X is like Float64, but panics if an error occurs.
858
func (ocs *ObClusterSelect) Float64X(ctx context.Context) float64 {
859
	v, err := ocs.Float64(ctx)
860
	if err != nil {
861
		panic(err)
862
	}
863
	return v
864
}
865

866
// Bools returns list of bools from a selector. It is only allowed when selecting one field.
867
func (ocs *ObClusterSelect) Bools(ctx context.Context) ([]bool, error) {
868
	if len(ocs.fields) > 1 {
869
		return nil, errors.New("ent: ObClusterSelect.Bools is not achievable when selecting more than 1 field")
870
	}
871
	var v []bool
872
	if err := ocs.Scan(ctx, &v); err != nil {
873
		return nil, err
874
	}
875
	return v, nil
876
}
877

878
// BoolsX is like Bools, but panics if an error occurs.
879
func (ocs *ObClusterSelect) BoolsX(ctx context.Context) []bool {
880
	v, err := ocs.Bools(ctx)
881
	if err != nil {
882
		panic(err)
883
	}
884
	return v
885
}
886

887
// Bool returns a single bool from a selector. It is only allowed when selecting one field.
888
func (ocs *ObClusterSelect) Bool(ctx context.Context) (_ bool, err error) {
889
	var v []bool
890
	if v, err = ocs.Bools(ctx); err != nil {
891
		return
892
	}
893
	switch len(v) {
894
	case 1:
895
		return v[0], nil
896
	case 0:
897
		err = &NotFoundError{obcluster.Label}
898
	default:
899
		err = fmt.Errorf("ent: ObClusterSelect.Bools returned %d results when one was expected", len(v))
900
	}
901
	return
902
}
903

904
// BoolX is like Bool, but panics if an error occurs.
905
func (ocs *ObClusterSelect) BoolX(ctx context.Context) bool {
906
	v, err := ocs.Bool(ctx)
907
	if err != nil {
908
		panic(err)
909
	}
910
	return v
911
}
912

913
func (ocs *ObClusterSelect) sqlScan(ctx context.Context, v interface{}) error {
914
	rows := &sql.Rows{}
915
	query, args := ocs.sql.Query()
916
	if err := ocs.driver.Query(ctx, query, args, rows); err != nil {
917
		return err
918
	}
919
	defer rows.Close()
920
	return sql.ScanSlice(rows, v)
921
}
922

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

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

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

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