Debian Codegraph @

ent/attestation_query.go

raw
1 // Code generated by ent, DO NOT EDIT.
2
3 package ent
4
5 import (
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 "github.com/google/uuid"
15 "github.com/in-toto/archivista/ent/attestation"
16 "github.com/in-toto/archivista/ent/attestationcollection"
17 "github.com/in-toto/archivista/ent/predicate"
18 )
19
20 // AttestationQuery is the builder for querying Attestation entities.
21 type AttestationQuery struct {
22 config
23 ctx *QueryContext
24 order []attestation.OrderOption
25 inters []Interceptor
26 predicates []predicate.Attestation
27 withAttestationCollection *AttestationCollectionQuery
28 withFKs bool
29 modifiers []func(*sql.Selector)
30 loadTotal []func(context.Context, []*Attestation) error
31 // intermediate query (i.e. traversal path).
32 sql *sql.Selector
33 path func(context.Context) (*sql.Selector, error)
34 }
35
36 // Where adds a new predicate for the AttestationQuery builder.
37 func (_q *AttestationQuery) Where(ps ...predicate.Attestation) *AttestationQuery {
38 _q.predicates = append(_q.predicates, ps...)
39 return _q
40 }
41
42 // Limit the number of records to be returned by this query.
43 func (_q *AttestationQuery) Limit(limit int) *AttestationQuery {
44 _q.ctx.Limit = &limit
45 return _q
46 }
47
48 // Offset to start from.
49 func (_q *AttestationQuery) Offset(offset int) *AttestationQuery {
50 _q.ctx.Offset = &offset
51 return _q
52 }
53
54 // Unique configures the query builder to filter duplicate records on query.
55 // By default, unique is set to true, and can be disabled using this method.
56 func (_q *AttestationQuery) Unique(unique bool) *AttestationQuery {
57 _q.ctx.Unique = &unique
58 return _q
59 }
60
61 // Order specifies how the records should be ordered.
62 func (_q *AttestationQuery) Order(o ...attestation.OrderOption) *AttestationQuery {
63 _q.order = append(_q.order, o...)
64 return _q
65 }
66
67 // QueryAttestationCollection chains the current query on the "attestation_collection" edge.
68 func (_q *AttestationQuery) QueryAttestationCollection() *AttestationCollectionQuery {
69 query := (&AttestationCollectionClient{config: _q.config}).Query()
70 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
71 if err := _q.prepareQuery(ctx); err != nil {
72 return nil, err
73 }
74 selector := _q.sqlQuery(ctx)
75 if err := selector.Err(); err != nil {
76 return nil, err
77 }
78 step := sqlgraph.NewStep(
79 sqlgraph.From(attestation.Table, attestation.FieldID, selector),
80 sqlgraph.To(attestationcollection.Table, attestationcollection.FieldID),
81 sqlgraph.Edge(sqlgraph.M2O, true, attestation.AttestationCollectionTable, attestation.AttestationCollectionColumn),
82 )
83 fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
84 return fromU, nil
85 }
86 return query
87 }
88
89 // First returns the first Attestation entity from the query.
90 // Returns a *NotFoundError when no Attestation was found.
91 func (_q *AttestationQuery) First(ctx context.Context) (*Attestation, error) {
92 nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
93 if err != nil {
94 return nil, err
95 }
96 if len(nodes) == 0 {
97 return nil, &NotFoundError{attestation.Label}
98 }
99 return nodes[0], nil
100 }
101
102 // FirstX is like First, but panics if an error occurs.
103 func (_q *AttestationQuery) FirstX(ctx context.Context) *Attestation {
104 node, err := _q.First(ctx)
105 if err != nil && !IsNotFound(err) {
106 panic(err)
107 }
108 return node
109 }
110
111 // FirstID returns the first Attestation ID from the query.
112 // Returns a *NotFoundError when no Attestation ID was found.
113 func (_q *AttestationQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
114 var ids []uuid.UUID
115 if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
116 return
117 }
118 if len(ids) == 0 {
119 err = &NotFoundError{attestation.Label}
120 return
121 }
122 return ids[0], nil
123 }
124
125 // FirstIDX is like FirstID, but panics if an error occurs.
126 func (_q *AttestationQuery) FirstIDX(ctx context.Context) uuid.UUID {
127 id, err := _q.FirstID(ctx)
128 if err != nil && !IsNotFound(err) {
129 panic(err)
130 }
131 return id
132 }
133
134 // Only returns a single Attestation entity found by the query, ensuring it only returns one.
135 // Returns a *NotSingularError when more than one Attestation entity is found.
136 // Returns a *NotFoundError when no Attestation entities are found.
137 func (_q *AttestationQuery) Only(ctx context.Context) (*Attestation, error) {
138 nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
139 if err != nil {
140 return nil, err
141 }
142 switch len(nodes) {
143 case 1:
144 return nodes[0], nil
145 case 0:
146 return nil, &NotFoundError{attestation.Label}
147 default:
148 return nil, &NotSingularError{attestation.Label}
149 }
150 }
151
152 // OnlyX is like Only, but panics if an error occurs.
153 func (_q *AttestationQuery) OnlyX(ctx context.Context) *Attestation {
154 node, err := _q.Only(ctx)
155 if err != nil {
156 panic(err)
157 }
158 return node
159 }
160
161 // OnlyID is like Only, but returns the only Attestation ID in the query.
162 // Returns a *NotSingularError when more than one Attestation ID is found.
163 // Returns a *NotFoundError when no entities are found.
164 func (_q *AttestationQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
165 var ids []uuid.UUID
166 if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
167 return
168 }
169 switch len(ids) {
170 case 1:
171 id = ids[0]
172 case 0:
173 err = &NotFoundError{attestation.Label}
174 default:
175 err = &NotSingularError{attestation.Label}
176 }
177 return
178 }
179
180 // OnlyIDX is like OnlyID, but panics if an error occurs.
181 func (_q *AttestationQuery) OnlyIDX(ctx context.Context) uuid.UUID {
182 id, err := _q.OnlyID(ctx)
183 if err != nil {
184 panic(err)
185 }
186 return id
187 }
188
189 // All executes the query and returns a list of Attestations.
190 func (_q *AttestationQuery) All(ctx context.Context) ([]*Attestation, error) {
191 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
192 if err := _q.prepareQuery(ctx); err != nil {
193 return nil, err
194 }
195 qr := querierAll[[]*Attestation, *AttestationQuery]()
196 return withInterceptors[[]*Attestation](ctx, _q, qr, _q.inters)
197 }
198
199 // AllX is like All, but panics if an error occurs.
200 func (_q *AttestationQuery) AllX(ctx context.Context) []*Attestation {
201 nodes, err := _q.All(ctx)
202 if err != nil {
203 panic(err)
204 }
205 return nodes
206 }
207
208 // IDs executes the query and returns a list of Attestation IDs.
209 func (_q *AttestationQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) {
210 if _q.ctx.Unique == nil && _q.path != nil {
211 _q.Unique(true)
212 }
213 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
214 if err = _q.Select(attestation.FieldID).Scan(ctx, &ids); err != nil {
215 return nil, err
216 }
217 return ids, nil
218 }
219
220 // IDsX is like IDs, but panics if an error occurs.
221 func (_q *AttestationQuery) IDsX(ctx context.Context) []uuid.UUID {
222 ids, err := _q.IDs(ctx)
223 if err != nil {
224 panic(err)
225 }
226 return ids
227 }
228
229 // Count returns the count of the given query.
230 func (_q *AttestationQuery) Count(ctx context.Context) (int, error) {
231 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
232 if err := _q.prepareQuery(ctx); err != nil {
233 return 0, err
234 }
235 return withInterceptors[int](ctx, _q, querierCount[*AttestationQuery](), _q.inters)
236 }
237
238 // CountX is like Count, but panics if an error occurs.
239 func (_q *AttestationQuery) CountX(ctx context.Context) int {
240 count, err := _q.Count(ctx)
241 if err != nil {
242 panic(err)
243 }
244 return count
245 }
246
247 // Exist returns true if the query has elements in the graph.
248 func (_q *AttestationQuery) Exist(ctx context.Context) (bool, error) {
249 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
250 switch _, err := _q.FirstID(ctx); {
251 case IsNotFound(err):
252 return false, nil
253 case err != nil:
254 return false, fmt.Errorf("ent: check existence: %w", err)
255 default:
256 return true, nil
257 }
258 }
259
260 // ExistX is like Exist, but panics if an error occurs.
261 func (_q *AttestationQuery) ExistX(ctx context.Context) bool {
262 exist, err := _q.Exist(ctx)
263 if err != nil {
264 panic(err)
265 }
266 return exist
267 }
268
269 // Clone returns a duplicate of the AttestationQuery builder, including all associated steps. It can be
270 // used to prepare common query builders and use them differently after the clone is made.
271 func (_q *AttestationQuery) Clone() *AttestationQuery {
272 if _q == nil {
273 return nil
274 }
275 return &AttestationQuery{
276 config: _q.config,
277 ctx: _q.ctx.Clone(),
278 order: append([]attestation.OrderOption{}, _q.order...),
279 inters: append([]Interceptor{}, _q.inters...),
280 predicates: append([]predicate.Attestation{}, _q.predicates...),
281 withAttestationCollection: _q.withAttestationCollection.Clone(),
282 // clone intermediate query.
283 sql: _q.sql.Clone(),
284 path: _q.path,
285 }
286 }
287
288 // WithAttestationCollection tells the query-builder to eager-load the nodes that are connected to
289 // the "attestation_collection" edge. The optional arguments are used to configure the query builder of the edge.
290 func (_q *AttestationQuery) WithAttestationCollection(opts ...func(*AttestationCollectionQuery)) *AttestationQuery {
291 query := (&AttestationCollectionClient{config: _q.config}).Query()
292 for _, opt := range opts {
293 opt(query)
294 }
295 _q.withAttestationCollection = query
296 return _q
297 }
298
299 // GroupBy is used to group vertices by one or more fields/columns.
300 // It is often used with aggregate functions, like: count, max, mean, min, sum.
301 //
302 // Example:
303 //
304 // var v []struct {
305 // Type string `json:"type,omitempty"`
306 // Count int `json:"count,omitempty"`
307 // }
308 //
309 // client.Attestation.Query().
310 // GroupBy(attestation.FieldType).
311 // Aggregate(ent.Count()).
312 // Scan(ctx, &v)
313 func (_q *AttestationQuery) GroupBy(field string, fields ...string) *AttestationGroupBy {
314 _q.ctx.Fields = append([]string{field}, fields...)
315 grbuild := &AttestationGroupBy{build: _q}
316 grbuild.flds = &_q.ctx.Fields
317 grbuild.label = attestation.Label
318 grbuild.scan = grbuild.Scan
319 return grbuild
320 }
321
322 // Select allows the selection one or more fields/columns for the given query,
323 // instead of selecting all fields in the entity.
324 //
325 // Example:
326 //
327 // var v []struct {
328 // Type string `json:"type,omitempty"`
329 // }
330 //
331 // client.Attestation.Query().
332 // Select(attestation.FieldType).
333 // Scan(ctx, &v)
334 func (_q *AttestationQuery) Select(fields ...string) *AttestationSelect {
335 _q.ctx.Fields = append(_q.ctx.Fields, fields...)
336 sbuild := &AttestationSelect{AttestationQuery: _q}
337 sbuild.label = attestation.Label
338 sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
339 return sbuild
340 }
341
342 // Aggregate returns a AttestationSelect configured with the given aggregations.
343 func (_q *AttestationQuery) Aggregate(fns ...AggregateFunc) *AttestationSelect {
344 return _q.Select().Aggregate(fns...)
345 }
346
347 func (_q *AttestationQuery) prepareQuery(ctx context.Context) error {
348 for _, inter := range _q.inters {
349 if inter == nil {
350 return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
351 }
352 if trv, ok := inter.(Traverser); ok {
353 if err := trv.Traverse(ctx, _q); err != nil {
354 return err
355 }
356 }
357 }
358 for _, f := range _q.ctx.Fields {
359 if !attestation.ValidColumn(f) {
360 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
361 }
362 }
363 if _q.path != nil {
364 prev, err := _q.path(ctx)
365 if err != nil {
366 return err
367 }
368 _q.sql = prev
369 }
370 return nil
371 }
372
373 func (_q *AttestationQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Attestation, error) {
374 var (
375 nodes = []*Attestation{}
376 withFKs = _q.withFKs
377 _spec = _q.querySpec()
378 loadedTypes = [1]bool{
379 _q.withAttestationCollection != nil,
380 }
381 )
382 if _q.withAttestationCollection != nil {
383 withFKs = true
384 }
385 if withFKs {
386 _spec.Node.Columns = append(_spec.Node.Columns, attestation.ForeignKeys...)
387 }
388 _spec.ScanValues = func(columns []string) ([]any, error) {
389 return (*Attestation).scanValues(nil, columns)
390 }
391 _spec.Assign = func(columns []string, values []any) error {
392 node := &Attestation{config: _q.config}
393 nodes = append(nodes, node)
394 node.Edges.loadedTypes = loadedTypes
395 return node.assignValues(columns, values)
396 }
397 if len(_q.modifiers) > 0 {
398 _spec.Modifiers = _q.modifiers
399 }
400 for i := range hooks {
401 hooks[i](ctx, _spec)
402 }
403 if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
404 return nil, err
405 }
406 if len(nodes) == 0 {
407 return nodes, nil
408 }
409 if query := _q.withAttestationCollection; query != nil {
410 if err := _q.loadAttestationCollection(ctx, query, nodes, nil,
411 func(n *Attestation, e *AttestationCollection) { n.Edges.AttestationCollection = e }); err != nil {
412 return nil, err
413 }
414 }
415 for i := range _q.loadTotal {
416 if err := _q.loadTotal[i](ctx, nodes); err != nil {
417 return nil, err
418 }
419 }
420 return nodes, nil
421 }
422
423 func (_q *AttestationQuery) loadAttestationCollection(ctx context.Context, query *AttestationCollectionQuery, nodes []*Attestation, init func(*Attestation), assign func(*Attestation, *AttestationCollection)) error {
424 ids := make([]uuid.UUID, 0, len(nodes))
425 nodeids := make(map[uuid.UUID][]*Attestation)
426 for i := range nodes {
427 if nodes[i].attestation_collection_attestations == nil {
428 continue
429 }
430 fk := *nodes[i].attestation_collection_attestations
431 if _, ok := nodeids[fk]; !ok {
432 ids = append(ids, fk)
433 }
434 nodeids[fk] = append(nodeids[fk], nodes[i])
435 }
436 if len(ids) == 0 {
437 return nil
438 }
439 query.Where(attestationcollection.IDIn(ids...))
440 neighbors, err := query.All(ctx)
441 if err != nil {
442 return err
443 }
444 for _, n := range neighbors {
445 nodes, ok := nodeids[n.ID]
446 if !ok {
447 return fmt.Errorf(`unexpected foreign-key "attestation_collection_attestations" returned %v`, n.ID)
448 }
449 for i := range nodes {
450 assign(nodes[i], n)
451 }
452 }
453 return nil
454 }
455
456 func (_q *AttestationQuery) sqlCount(ctx context.Context) (int, error) {
457 _spec := _q.querySpec()
458 if len(_q.modifiers) > 0 {
459 _spec.Modifiers = _q.modifiers
460 }
461 _spec.Node.Columns = _q.ctx.Fields
462 if len(_q.ctx.Fields) > 0 {
463 _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
464 }
465 return sqlgraph.CountNodes(ctx, _q.driver, _spec)
466 }
467
468 func (_q *AttestationQuery) querySpec() *sqlgraph.QuerySpec {
469 _spec := sqlgraph.NewQuerySpec(attestation.Table, attestation.Columns, sqlgraph.NewFieldSpec(attestation.FieldID, field.TypeUUID))
470 _spec.From = _q.sql
471 if unique := _q.ctx.Unique; unique != nil {
472 _spec.Unique = *unique
473 } else if _q.path != nil {
474 _spec.Unique = true
475 }
476 if fields := _q.ctx.Fields; len(fields) > 0 {
477 _spec.Node.Columns = make([]string, 0, len(fields))
478 _spec.Node.Columns = append(_spec.Node.Columns, attestation.FieldID)
479 for i := range fields {
480 if fields[i] != attestation.FieldID {
481 _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
482 }
483 }
484 }
485 if ps := _q.predicates; len(ps) > 0 {
486 _spec.Predicate = func(selector *sql.Selector) {
487 for i := range ps {
488 ps[i](selector)
489 }
490 }
491 }
492 if limit := _q.ctx.Limit; limit != nil {
493 _spec.Limit = *limit
494 }
495 if offset := _q.ctx.Offset; offset != nil {
496 _spec.Offset = *offset
497 }
498 if ps := _q.order; len(ps) > 0 {
499 _spec.Order = func(selector *sql.Selector) {
500 for i := range ps {
501 ps[i](selector)
502 }
503 }
504 }
505 return _spec
506 }
507
508 func (_q *AttestationQuery) sqlQuery(ctx context.Context) *sql.Selector {
509 builder := sql.Dialect(_q.driver.Dialect())
510 t1 := builder.Table(attestation.Table)
511 columns := _q.ctx.Fields
512 if len(columns) == 0 {
513 columns = attestation.Columns
514 }
515 selector := builder.Select(t1.Columns(columns...)...).From(t1)
516 if _q.sql != nil {
517 selector = _q.sql
518 selector.Select(selector.Columns(columns...)...)
519 }
520 if _q.ctx.Unique != nil && *_q.ctx.Unique {
521 selector.Distinct()
522 }
523 for _, p := range _q.predicates {
524 p(selector)
525 }
526 for _, p := range _q.order {
527 p(selector)
528 }
529 if offset := _q.ctx.Offset; offset != nil {
530 // limit is mandatory for offset clause. We start
531 // with default value, and override it below if needed.
532 selector.Offset(*offset).Limit(math.MaxInt32)
533 }
534 if limit := _q.ctx.Limit; limit != nil {
535 selector.Limit(*limit)
536 }
537 return selector
538 }
539
540 // AttestationGroupBy is the group-by builder for Attestation entities.
541 type AttestationGroupBy struct {
542 selector
543 build *AttestationQuery
544 }
545
546 // Aggregate adds the given aggregation functions to the group-by query.
547 func (_g *AttestationGroupBy) Aggregate(fns ...AggregateFunc) *AttestationGroupBy {
548 _g.fns = append(_g.fns, fns...)
549 return _g
550 }
551
552 // Scan applies the selector query and scans the result into the given value.
553 func (_g *AttestationGroupBy) Scan(ctx context.Context, v any) error {
554 ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
555 if err := _g.build.prepareQuery(ctx); err != nil {
556 return err
557 }
558 return scanWithInterceptors[*AttestationQuery, *AttestationGroupBy](ctx, _g.build, _g, _g.build.inters, v)
559 }
560
561 func (_g *AttestationGroupBy) sqlScan(ctx context.Context, root *AttestationQuery, v any) error {
562 selector := root.sqlQuery(ctx).Select()
563 aggregation := make([]string, 0, len(_g.fns))
564 for _, fn := range _g.fns {
565 aggregation = append(aggregation, fn(selector))
566 }
567 if len(selector.SelectedColumns()) == 0 {
568 columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
569 for _, f := range *_g.flds {
570 columns = append(columns, selector.C(f))
571 }
572 columns = append(columns, aggregation...)
573 selector.Select(columns...)
574 }
575 selector.GroupBy(selector.Columns(*_g.flds...)...)
576 if err := selector.Err(); err != nil {
577 return err
578 }
579 rows := &sql.Rows{}
580 query, args := selector.Query()
581 if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
582 return err
583 }
584 defer rows.Close()
585 return sql.ScanSlice(rows, v)
586 }
587
588 // AttestationSelect is the builder for selecting fields of Attestation entities.
589 type AttestationSelect struct {
590 *AttestationQuery
591 selector
592 }
593
594 // Aggregate adds the given aggregation functions to the selector query.
595 func (_s *AttestationSelect) Aggregate(fns ...AggregateFunc) *AttestationSelect {
596 _s.fns = append(_s.fns, fns...)
597 return _s
598 }
599
600 // Scan applies the selector query and scans the result into the given value.
601 func (_s *AttestationSelect) Scan(ctx context.Context, v any) error {
602 ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
603 if err := _s.prepareQuery(ctx); err != nil {
604 return err
605 }
606 return scanWithInterceptors[*AttestationQuery, *AttestationSelect](ctx, _s.AttestationQuery, _s, _s.inters, v)
607 }
608
609 func (_s *AttestationSelect) sqlScan(ctx context.Context, root *AttestationQuery, v any) error {
610 selector := root.sqlQuery(ctx)
611 aggregation := make([]string, 0, len(_s.fns))
612 for _, fn := range _s.fns {
613 aggregation = append(aggregation, fn(selector))
614 }
615 switch n := len(*_s.selector.flds); {
616 case n == 0 && len(aggregation) > 0:
617 selector.Select(aggregation...)
618 case n != 0 && len(aggregation) > 0:
619 selector.AppendSelect(aggregation...)
620 }
621 rows := &sql.Rows{}
622 query, args := selector.Query()
623 if err := _s.driver.Query(ctx, query, args, rows); err != nil {
624 return err
625 }
626 defer rows.Close()
627 return sql.ScanSlice(rows, v)
628 }