plc.directory mirror
1// Code generated by ent, DO NOT EDIT.
2
3package ent
4
5import (
6 "context"
7 "fmt"
8 "math"
9
10 "entgo.io/ent"
11 "entgo.io/ent/dialect/sql"
12 "entgo.io/ent/dialect/sql/sqlgraph"
13 "entgo.io/ent/schema/field"
14 "tangled.sh/seiso.moe/aletheia.directory/ent/operation"
15 "tangled.sh/seiso.moe/aletheia.directory/ent/predicate"
16)
17
18// OperationQuery is the builder for querying Operation entities.
19type OperationQuery struct {
20 config
21 ctx *QueryContext
22 order []operation.OrderOption
23 inters []Interceptor
24 predicates []predicate.Operation
25 // intermediate query (i.e. traversal path).
26 sql *sql.Selector
27 path func(context.Context) (*sql.Selector, error)
28}
29
30// Where adds a new predicate for the OperationQuery builder.
31func (oq *OperationQuery) Where(ps ...predicate.Operation) *OperationQuery {
32 oq.predicates = append(oq.predicates, ps...)
33 return oq
34}
35
36// Limit the number of records to be returned by this query.
37func (oq *OperationQuery) Limit(limit int) *OperationQuery {
38 oq.ctx.Limit = &limit
39 return oq
40}
41
42// Offset to start from.
43func (oq *OperationQuery) Offset(offset int) *OperationQuery {
44 oq.ctx.Offset = &offset
45 return oq
46}
47
48// Unique configures the query builder to filter duplicate records on query.
49// By default, unique is set to true, and can be disabled using this method.
50func (oq *OperationQuery) Unique(unique bool) *OperationQuery {
51 oq.ctx.Unique = &unique
52 return oq
53}
54
55// Order specifies how the records should be ordered.
56func (oq *OperationQuery) Order(o ...operation.OrderOption) *OperationQuery {
57 oq.order = append(oq.order, o...)
58 return oq
59}
60
61// First returns the first Operation entity from the query.
62// Returns a *NotFoundError when no Operation was found.
63func (oq *OperationQuery) First(ctx context.Context) (*Operation, error) {
64 nodes, err := oq.Limit(1).All(setContextOp(ctx, oq.ctx, ent.OpQueryFirst))
65 if err != nil {
66 return nil, err
67 }
68 if len(nodes) == 0 {
69 return nil, &NotFoundError{operation.Label}
70 }
71 return nodes[0], nil
72}
73
74// FirstX is like First, but panics if an error occurs.
75func (oq *OperationQuery) FirstX(ctx context.Context) *Operation {
76 node, err := oq.First(ctx)
77 if err != nil && !IsNotFound(err) {
78 panic(err)
79 }
80 return node
81}
82
83// FirstID returns the first Operation ID from the query.
84// Returns a *NotFoundError when no Operation ID was found.
85func (oq *OperationQuery) FirstID(ctx context.Context) (id int, err error) {
86 var ids []int
87 if ids, err = oq.Limit(1).IDs(setContextOp(ctx, oq.ctx, ent.OpQueryFirstID)); err != nil {
88 return
89 }
90 if len(ids) == 0 {
91 err = &NotFoundError{operation.Label}
92 return
93 }
94 return ids[0], nil
95}
96
97// FirstIDX is like FirstID, but panics if an error occurs.
98func (oq *OperationQuery) FirstIDX(ctx context.Context) int {
99 id, err := oq.FirstID(ctx)
100 if err != nil && !IsNotFound(err) {
101 panic(err)
102 }
103 return id
104}
105
106// Only returns a single Operation entity found by the query, ensuring it only returns one.
107// Returns a *NotSingularError when more than one Operation entity is found.
108// Returns a *NotFoundError when no Operation entities are found.
109func (oq *OperationQuery) Only(ctx context.Context) (*Operation, error) {
110 nodes, err := oq.Limit(2).All(setContextOp(ctx, oq.ctx, ent.OpQueryOnly))
111 if err != nil {
112 return nil, err
113 }
114 switch len(nodes) {
115 case 1:
116 return nodes[0], nil
117 case 0:
118 return nil, &NotFoundError{operation.Label}
119 default:
120 return nil, &NotSingularError{operation.Label}
121 }
122}
123
124// OnlyX is like Only, but panics if an error occurs.
125func (oq *OperationQuery) OnlyX(ctx context.Context) *Operation {
126 node, err := oq.Only(ctx)
127 if err != nil {
128 panic(err)
129 }
130 return node
131}
132
133// OnlyID is like Only, but returns the only Operation ID in the query.
134// Returns a *NotSingularError when more than one Operation ID is found.
135// Returns a *NotFoundError when no entities are found.
136func (oq *OperationQuery) OnlyID(ctx context.Context) (id int, err error) {
137 var ids []int
138 if ids, err = oq.Limit(2).IDs(setContextOp(ctx, oq.ctx, ent.OpQueryOnlyID)); err != nil {
139 return
140 }
141 switch len(ids) {
142 case 1:
143 id = ids[0]
144 case 0:
145 err = &NotFoundError{operation.Label}
146 default:
147 err = &NotSingularError{operation.Label}
148 }
149 return
150}
151
152// OnlyIDX is like OnlyID, but panics if an error occurs.
153func (oq *OperationQuery) OnlyIDX(ctx context.Context) int {
154 id, err := oq.OnlyID(ctx)
155 if err != nil {
156 panic(err)
157 }
158 return id
159}
160
161// All executes the query and returns a list of Operations.
162func (oq *OperationQuery) All(ctx context.Context) ([]*Operation, error) {
163 ctx = setContextOp(ctx, oq.ctx, ent.OpQueryAll)
164 if err := oq.prepareQuery(ctx); err != nil {
165 return nil, err
166 }
167 qr := querierAll[[]*Operation, *OperationQuery]()
168 return withInterceptors[[]*Operation](ctx, oq, qr, oq.inters)
169}
170
171// AllX is like All, but panics if an error occurs.
172func (oq *OperationQuery) AllX(ctx context.Context) []*Operation {
173 nodes, err := oq.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 Operation IDs.
181func (oq *OperationQuery) IDs(ctx context.Context) (ids []int, err error) {
182 if oq.ctx.Unique == nil && oq.path != nil {
183 oq.Unique(true)
184 }
185 ctx = setContextOp(ctx, oq.ctx, ent.OpQueryIDs)
186 if err = oq.Select(operation.FieldID).Scan(ctx, &ids); err != nil {
187 return nil, err
188 }
189 return ids, nil
190}
191
192// IDsX is like IDs, but panics if an error occurs.
193func (oq *OperationQuery) IDsX(ctx context.Context) []int {
194 ids, err := oq.IDs(ctx)
195 if err != nil {
196 panic(err)
197 }
198 return ids
199}
200
201// Count returns the count of the given query.
202func (oq *OperationQuery) Count(ctx context.Context) (int, error) {
203 ctx = setContextOp(ctx, oq.ctx, ent.OpQueryCount)
204 if err := oq.prepareQuery(ctx); err != nil {
205 return 0, err
206 }
207 return withInterceptors[int](ctx, oq, querierCount[*OperationQuery](), oq.inters)
208}
209
210// CountX is like Count, but panics if an error occurs.
211func (oq *OperationQuery) CountX(ctx context.Context) int {
212 count, err := oq.Count(ctx)
213 if err != nil {
214 panic(err)
215 }
216 return count
217}
218
219// Exist returns true if the query has elements in the graph.
220func (oq *OperationQuery) Exist(ctx context.Context) (bool, error) {
221 ctx = setContextOp(ctx, oq.ctx, ent.OpQueryExist)
222 switch _, err := oq.FirstID(ctx); {
223 case IsNotFound(err):
224 return false, nil
225 case err != nil:
226 return false, fmt.Errorf("ent: check existence: %w", err)
227 default:
228 return true, nil
229 }
230}
231
232// ExistX is like Exist, but panics if an error occurs.
233func (oq *OperationQuery) ExistX(ctx context.Context) bool {
234 exist, err := oq.Exist(ctx)
235 if err != nil {
236 panic(err)
237 }
238 return exist
239}
240
241// Clone returns a duplicate of the OperationQuery builder, including all associated steps. It can be
242// used to prepare common query builders and use them differently after the clone is made.
243func (oq *OperationQuery) Clone() *OperationQuery {
244 if oq == nil {
245 return nil
246 }
247 return &OperationQuery{
248 config: oq.config,
249 ctx: oq.ctx.Clone(),
250 order: append([]operation.OrderOption{}, oq.order...),
251 inters: append([]Interceptor{}, oq.inters...),
252 predicates: append([]predicate.Operation{}, oq.predicates...),
253 // clone intermediate query.
254 sql: oq.sql.Clone(),
255 path: oq.path,
256 }
257}
258
259// GroupBy is used to group vertices by one or more fields/columns.
260// It is often used with aggregate functions, like: count, max, mean, min, sum.
261//
262// Example:
263//
264// var v []struct {
265// Did string `json:"did,omitempty"`
266// Count int `json:"count,omitempty"`
267// }
268//
269// client.Operation.Query().
270// GroupBy(operation.FieldDid).
271// Aggregate(ent.Count()).
272// Scan(ctx, &v)
273func (oq *OperationQuery) GroupBy(field string, fields ...string) *OperationGroupBy {
274 oq.ctx.Fields = append([]string{field}, fields...)
275 grbuild := &OperationGroupBy{build: oq}
276 grbuild.flds = &oq.ctx.Fields
277 grbuild.label = operation.Label
278 grbuild.scan = grbuild.Scan
279 return grbuild
280}
281
282// Select allows the selection one or more fields/columns for the given query,
283// instead of selecting all fields in the entity.
284//
285// Example:
286//
287// var v []struct {
288// Did string `json:"did,omitempty"`
289// }
290//
291// client.Operation.Query().
292// Select(operation.FieldDid).
293// Scan(ctx, &v)
294func (oq *OperationQuery) Select(fields ...string) *OperationSelect {
295 oq.ctx.Fields = append(oq.ctx.Fields, fields...)
296 sbuild := &OperationSelect{OperationQuery: oq}
297 sbuild.label = operation.Label
298 sbuild.flds, sbuild.scan = &oq.ctx.Fields, sbuild.Scan
299 return sbuild
300}
301
302// Aggregate returns a OperationSelect configured with the given aggregations.
303func (oq *OperationQuery) Aggregate(fns ...AggregateFunc) *OperationSelect {
304 return oq.Select().Aggregate(fns...)
305}
306
307func (oq *OperationQuery) prepareQuery(ctx context.Context) error {
308 for _, inter := range oq.inters {
309 if inter == nil {
310 return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
311 }
312 if trv, ok := inter.(Traverser); ok {
313 if err := trv.Traverse(ctx, oq); err != nil {
314 return err
315 }
316 }
317 }
318 for _, f := range oq.ctx.Fields {
319 if !operation.ValidColumn(f) {
320 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
321 }
322 }
323 if oq.path != nil {
324 prev, err := oq.path(ctx)
325 if err != nil {
326 return err
327 }
328 oq.sql = prev
329 }
330 return nil
331}
332
333func (oq *OperationQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Operation, error) {
334 var (
335 nodes = []*Operation{}
336 _spec = oq.querySpec()
337 )
338 _spec.ScanValues = func(columns []string) ([]any, error) {
339 return (*Operation).scanValues(nil, columns)
340 }
341 _spec.Assign = func(columns []string, values []any) error {
342 node := &Operation{config: oq.config}
343 nodes = append(nodes, node)
344 return node.assignValues(columns, values)
345 }
346 for i := range hooks {
347 hooks[i](ctx, _spec)
348 }
349 if err := sqlgraph.QueryNodes(ctx, oq.driver, _spec); err != nil {
350 return nil, err
351 }
352 if len(nodes) == 0 {
353 return nodes, nil
354 }
355 return nodes, nil
356}
357
358func (oq *OperationQuery) sqlCount(ctx context.Context) (int, error) {
359 _spec := oq.querySpec()
360 _spec.Node.Columns = oq.ctx.Fields
361 if len(oq.ctx.Fields) > 0 {
362 _spec.Unique = oq.ctx.Unique != nil && *oq.ctx.Unique
363 }
364 return sqlgraph.CountNodes(ctx, oq.driver, _spec)
365}
366
367func (oq *OperationQuery) querySpec() *sqlgraph.QuerySpec {
368 _spec := sqlgraph.NewQuerySpec(operation.Table, operation.Columns, sqlgraph.NewFieldSpec(operation.FieldID, field.TypeInt))
369 _spec.From = oq.sql
370 if unique := oq.ctx.Unique; unique != nil {
371 _spec.Unique = *unique
372 } else if oq.path != nil {
373 _spec.Unique = true
374 }
375 if fields := oq.ctx.Fields; len(fields) > 0 {
376 _spec.Node.Columns = make([]string, 0, len(fields))
377 _spec.Node.Columns = append(_spec.Node.Columns, operation.FieldID)
378 for i := range fields {
379 if fields[i] != operation.FieldID {
380 _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
381 }
382 }
383 }
384 if ps := oq.predicates; len(ps) > 0 {
385 _spec.Predicate = func(selector *sql.Selector) {
386 for i := range ps {
387 ps[i](selector)
388 }
389 }
390 }
391 if limit := oq.ctx.Limit; limit != nil {
392 _spec.Limit = *limit
393 }
394 if offset := oq.ctx.Offset; offset != nil {
395 _spec.Offset = *offset
396 }
397 if ps := oq.order; len(ps) > 0 {
398 _spec.Order = func(selector *sql.Selector) {
399 for i := range ps {
400 ps[i](selector)
401 }
402 }
403 }
404 return _spec
405}
406
407func (oq *OperationQuery) sqlQuery(ctx context.Context) *sql.Selector {
408 builder := sql.Dialect(oq.driver.Dialect())
409 t1 := builder.Table(operation.Table)
410 columns := oq.ctx.Fields
411 if len(columns) == 0 {
412 columns = operation.Columns
413 }
414 selector := builder.Select(t1.Columns(columns...)...).From(t1)
415 if oq.sql != nil {
416 selector = oq.sql
417 selector.Select(selector.Columns(columns...)...)
418 }
419 if oq.ctx.Unique != nil && *oq.ctx.Unique {
420 selector.Distinct()
421 }
422 for _, p := range oq.predicates {
423 p(selector)
424 }
425 for _, p := range oq.order {
426 p(selector)
427 }
428 if offset := oq.ctx.Offset; offset != nil {
429 // limit is mandatory for offset clause. We start
430 // with default value, and override it below if needed.
431 selector.Offset(*offset).Limit(math.MaxInt32)
432 }
433 if limit := oq.ctx.Limit; limit != nil {
434 selector.Limit(*limit)
435 }
436 return selector
437}
438
439// OperationGroupBy is the group-by builder for Operation entities.
440type OperationGroupBy struct {
441 selector
442 build *OperationQuery
443}
444
445// Aggregate adds the given aggregation functions to the group-by query.
446func (ogb *OperationGroupBy) Aggregate(fns ...AggregateFunc) *OperationGroupBy {
447 ogb.fns = append(ogb.fns, fns...)
448 return ogb
449}
450
451// Scan applies the selector query and scans the result into the given value.
452func (ogb *OperationGroupBy) Scan(ctx context.Context, v any) error {
453 ctx = setContextOp(ctx, ogb.build.ctx, ent.OpQueryGroupBy)
454 if err := ogb.build.prepareQuery(ctx); err != nil {
455 return err
456 }
457 return scanWithInterceptors[*OperationQuery, *OperationGroupBy](ctx, ogb.build, ogb, ogb.build.inters, v)
458}
459
460func (ogb *OperationGroupBy) sqlScan(ctx context.Context, root *OperationQuery, v any) error {
461 selector := root.sqlQuery(ctx).Select()
462 aggregation := make([]string, 0, len(ogb.fns))
463 for _, fn := range ogb.fns {
464 aggregation = append(aggregation, fn(selector))
465 }
466 if len(selector.SelectedColumns()) == 0 {
467 columns := make([]string, 0, len(*ogb.flds)+len(ogb.fns))
468 for _, f := range *ogb.flds {
469 columns = append(columns, selector.C(f))
470 }
471 columns = append(columns, aggregation...)
472 selector.Select(columns...)
473 }
474 selector.GroupBy(selector.Columns(*ogb.flds...)...)
475 if err := selector.Err(); err != nil {
476 return err
477 }
478 rows := &sql.Rows{}
479 query, args := selector.Query()
480 if err := ogb.build.driver.Query(ctx, query, args, rows); err != nil {
481 return err
482 }
483 defer rows.Close()
484 return sql.ScanSlice(rows, v)
485}
486
487// OperationSelect is the builder for selecting fields of Operation entities.
488type OperationSelect struct {
489 *OperationQuery
490 selector
491}
492
493// Aggregate adds the given aggregation functions to the selector query.
494func (os *OperationSelect) Aggregate(fns ...AggregateFunc) *OperationSelect {
495 os.fns = append(os.fns, fns...)
496 return os
497}
498
499// Scan applies the selector query and scans the result into the given value.
500func (os *OperationSelect) Scan(ctx context.Context, v any) error {
501 ctx = setContextOp(ctx, os.ctx, ent.OpQuerySelect)
502 if err := os.prepareQuery(ctx); err != nil {
503 return err
504 }
505 return scanWithInterceptors[*OperationQuery, *OperationSelect](ctx, os.OperationQuery, os, os.inters, v)
506}
507
508func (os *OperationSelect) sqlScan(ctx context.Context, root *OperationQuery, v any) error {
509 selector := root.sqlQuery(ctx)
510 aggregation := make([]string, 0, len(os.fns))
511 for _, fn := range os.fns {
512 aggregation = append(aggregation, fn(selector))
513 }
514 switch n := len(*os.selector.flds); {
515 case n == 0 && len(aggregation) > 0:
516 selector.Select(aggregation...)
517 case n != 0 && len(aggregation) > 0:
518 selector.AppendSelect(aggregation...)
519 }
520 rows := &sql.Rows{}
521 query, args := selector.Query()
522 if err := os.driver.Query(ctx, query, args, rows); err != nil {
523 return err
524 }
525 defer rows.Close()
526 return sql.ScanSlice(rows, v)
527}