Debian Codegraph @

ent/statement_query.go

raw
1 // Code generated by ent, DO NOT EDIT.
2
3 package ent
4
5 import (
6 "context"
7 "database/sql/driver"
8 "fmt"
9 "math"
10
11 "entgo.io/ent"
12 "entgo.io/ent/dialect/sql"
13 "entgo.io/ent/dialect/sql/sqlgraph"
14 "entgo.io/ent/schema/field"
15 "github.com/google/uuid"
16 "github.com/in-toto/archivista/ent/attestationcollection"
17 "github.com/in-toto/archivista/ent/attestationpolicy"
18 "github.com/in-toto/archivista/ent/dsse"
19 "github.com/in-toto/archivista/ent/predicate"
20 "github.com/in-toto/archivista/ent/statement"
21 "github.com/in-toto/archivista/ent/subject"
22 )
23
24 // StatementQuery is the builder for querying Statement entities.
25 type StatementQuery struct {
26 config
27 ctx *QueryContext
28 order []statement.OrderOption
29 inters []Interceptor
30 predicates []predicate.Statement
31 withSubjects *SubjectQuery
32 withPolicy *AttestationPolicyQuery
33 withAttestationCollections *AttestationCollectionQuery
34 withDsse *DsseQuery
35 modifiers []func(*sql.Selector)
36 loadTotal []func(context.Context, []*Statement) error
37 withNamedSubjects map[string]*SubjectQuery
38 withNamedDsse map[string]*DsseQuery
39 // intermediate query (i.e. traversal path).
40 sql *sql.Selector
41 path func(context.Context) (*sql.Selector, error)
42 }
43
44 // Where adds a new predicate for the StatementQuery builder.
45 func (_q *StatementQuery) Where(ps ...predicate.Statement) *StatementQuery {
46 _q.predicates = append(_q.predicates, ps...)
47 return _q
48 }
49
50 // Limit the number of records to be returned by this query.
51 func (_q *StatementQuery) Limit(limit int) *StatementQuery {
52 _q.ctx.Limit = &limit
53 return _q
54 }
55
56 // Offset to start from.
57 func (_q *StatementQuery) Offset(offset int) *StatementQuery {
58 _q.ctx.Offset = &offset
59 return _q
60 }
61
62 // Unique configures the query builder to filter duplicate records on query.
63 // By default, unique is set to true, and can be disabled using this method.
64 func (_q *StatementQuery) Unique(unique bool) *StatementQuery {
65 _q.ctx.Unique = &unique
66 return _q
67 }
68
69 // Order specifies how the records should be ordered.
70 func (_q *StatementQuery) Order(o ...statement.OrderOption) *StatementQuery {
71 _q.order = append(_q.order, o...)
72 return _q
73 }
74
75 // QuerySubjects chains the current query on the "subjects" edge.
76 func (_q *StatementQuery) QuerySubjects() *SubjectQuery {
77 query := (&SubjectClient{config: _q.config}).Query()
78 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
79 if err := _q.prepareQuery(ctx); err != nil {
80 return nil, err
81 }
82 selector := _q.sqlQuery(ctx)
83 if err := selector.Err(); err != nil {
84 return nil, err
85 }
86 step := sqlgraph.NewStep(
87 sqlgraph.From(statement.Table, statement.FieldID, selector),
88 sqlgraph.To(subject.Table, subject.FieldID),
89 sqlgraph.Edge(sqlgraph.O2M, false, statement.SubjectsTable, statement.SubjectsColumn),
90 )
91 fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
92 return fromU, nil
93 }
94 return query
95 }
96
97 // QueryPolicy chains the current query on the "policy" edge.
98 func (_q *StatementQuery) QueryPolicy() *AttestationPolicyQuery {
99 query := (&AttestationPolicyClient{config: _q.config}).Query()
100 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
101 if err := _q.prepareQuery(ctx); err != nil {
102 return nil, err
103 }
104 selector := _q.sqlQuery(ctx)
105 if err := selector.Err(); err != nil {
106 return nil, err
107 }
108 step := sqlgraph.NewStep(
109 sqlgraph.From(statement.Table, statement.FieldID, selector),
110 sqlgraph.To(attestationpolicy.Table, attestationpolicy.FieldID),
111 sqlgraph.Edge(sqlgraph.O2O, false, statement.PolicyTable, statement.PolicyColumn),
112 )
113 fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
114 return fromU, nil
115 }
116 return query
117 }
118
119 // QueryAttestationCollections chains the current query on the "attestation_collections" edge.
120 func (_q *StatementQuery) QueryAttestationCollections() *AttestationCollectionQuery {
121 query := (&AttestationCollectionClient{config: _q.config}).Query()
122 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
123 if err := _q.prepareQuery(ctx); err != nil {
124 return nil, err
125 }
126 selector := _q.sqlQuery(ctx)
127 if err := selector.Err(); err != nil {
128 return nil, err
129 }
130 step := sqlgraph.NewStep(
131 sqlgraph.From(statement.Table, statement.FieldID, selector),
132 sqlgraph.To(attestationcollection.Table, attestationcollection.FieldID),
133 sqlgraph.Edge(sqlgraph.O2O, false, statement.AttestationCollectionsTable, statement.AttestationCollectionsColumn),
134 )
135 fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
136 return fromU, nil
137 }
138 return query
139 }
140
141 // QueryDsse chains the current query on the "dsse" edge.
142 func (_q *StatementQuery) QueryDsse() *DsseQuery {
143 query := (&DsseClient{config: _q.config}).Query()
144 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
145 if err := _q.prepareQuery(ctx); err != nil {
146 return nil, err
147 }
148 selector := _q.sqlQuery(ctx)
149 if err := selector.Err(); err != nil {
150 return nil, err
151 }
152 step := sqlgraph.NewStep(
153 sqlgraph.From(statement.Table, statement.FieldID, selector),
154 sqlgraph.To(dsse.Table, dsse.FieldID),
155 sqlgraph.Edge(sqlgraph.O2M, true, statement.DsseTable, statement.DsseColumn),
156 )
157 fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
158 return fromU, nil
159 }
160 return query
161 }
162
163 // First returns the first Statement entity from the query.
164 // Returns a *NotFoundError when no Statement was found.
165 func (_q *StatementQuery) First(ctx context.Context) (*Statement, error) {
166 nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
167 if err != nil {
168 return nil, err
169 }
170 if len(nodes) == 0 {
171 return nil, &NotFoundError{statement.Label}
172 }
173 return nodes[0], nil
174 }
175
176 // FirstX is like First, but panics if an error occurs.
177 func (_q *StatementQuery) FirstX(ctx context.Context) *Statement {
178 node, err := _q.First(ctx)
179 if err != nil && !IsNotFound(err) {
180 panic(err)
181 }
182 return node
183 }
184
185 // FirstID returns the first Statement ID from the query.
186 // Returns a *NotFoundError when no Statement ID was found.
187 func (_q *StatementQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
188 var ids []uuid.UUID
189 if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
190 return
191 }
192 if len(ids) == 0 {
193 err = &NotFoundError{statement.Label}
194 return
195 }
196 return ids[0], nil
197 }
198
199 // FirstIDX is like FirstID, but panics if an error occurs.
200 func (_q *StatementQuery) FirstIDX(ctx context.Context) uuid.UUID {
201 id, err := _q.FirstID(ctx)
202 if err != nil && !IsNotFound(err) {
203 panic(err)
204 }
205 return id
206 }
207
208 // Only returns a single Statement entity found by the query, ensuring it only returns one.
209 // Returns a *NotSingularError when more than one Statement entity is found.
210 // Returns a *NotFoundError when no Statement entities are found.
211 func (_q *StatementQuery) Only(ctx context.Context) (*Statement, error) {
212 nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
213 if err != nil {
214 return nil, err
215 }
216 switch len(nodes) {
217 case 1:
218 return nodes[0], nil
219 case 0:
220 return nil, &NotFoundError{statement.Label}
221 default:
222 return nil, &NotSingularError{statement.Label}
223 }
224 }
225
226 // OnlyX is like Only, but panics if an error occurs.
227 func (_q *StatementQuery) OnlyX(ctx context.Context) *Statement {
228 node, err := _q.Only(ctx)
229 if err != nil {
230 panic(err)
231 }
232 return node
233 }
234
235 // OnlyID is like Only, but returns the only Statement ID in the query.
236 // Returns a *NotSingularError when more than one Statement ID is found.
237 // Returns a *NotFoundError when no entities are found.
238 func (_q *StatementQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
239 var ids []uuid.UUID
240 if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
241 return
242 }
243 switch len(ids) {
244 case 1:
245 id = ids[0]
246 case 0:
247 err = &NotFoundError{statement.Label}
248 default:
249 err = &NotSingularError{statement.Label}
250 }
251 return
252 }
253
254 // OnlyIDX is like OnlyID, but panics if an error occurs.
255 func (_q *StatementQuery) OnlyIDX(ctx context.Context) uuid.UUID {
256 id, err := _q.OnlyID(ctx)
257 if err != nil {
258 panic(err)
259 }
260 return id
261 }
262
263 // All executes the query and returns a list of Statements.
264 func (_q *StatementQuery) All(ctx context.Context) ([]*Statement, error) {
265 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
266 if err := _q.prepareQuery(ctx); err != nil {
267 return nil, err
268 }
269 qr := querierAll[[]*Statement, *StatementQuery]()
270 return withInterceptors[[]*Statement](ctx, _q, qr, _q.inters)
271 }
272
273 // AllX is like All, but panics if an error occurs.
274 func (_q *StatementQuery) AllX(ctx context.Context) []*Statement {
275 nodes, err := _q.All(ctx)
276 if err != nil {
277 panic(err)
278 }
279 return nodes
280 }
281
282 // IDs executes the query and returns a list of Statement IDs.
283 func (_q *StatementQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) {
284 if _q.ctx.Unique == nil && _q.path != nil {
285 _q.Unique(true)
286 }
287 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
288 if err = _q.Select(statement.FieldID).Scan(ctx, &ids); err != nil {
289 return nil, err
290 }
291 return ids, nil
292 }
293
294 // IDsX is like IDs, but panics if an error occurs.
295 func (_q *StatementQuery) IDsX(ctx context.Context) []uuid.UUID {
296 ids, err := _q.IDs(ctx)
297 if err != nil {
298 panic(err)
299 }
300 return ids
301 }
302
303 // Count returns the count of the given query.
304 func (_q *StatementQuery) Count(ctx context.Context) (int, error) {
305 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
306 if err := _q.prepareQuery(ctx); err != nil {
307 return 0, err
308 }
309 return withInterceptors[int](ctx, _q, querierCount[*StatementQuery](), _q.inters)
310 }
311
312 // CountX is like Count, but panics if an error occurs.
313 func (_q *StatementQuery) CountX(ctx context.Context) int {
314 count, err := _q.Count(ctx)
315 if err != nil {
316 panic(err)
317 }
318 return count
319 }
320
321 // Exist returns true if the query has elements in the graph.
322 func (_q *StatementQuery) Exist(ctx context.Context) (bool, error) {
323 ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
324 switch _, err := _q.FirstID(ctx); {
325 case IsNotFound(err):
326 return false, nil
327 case err != nil:
328 return false, fmt.Errorf("ent: check existence: %w", err)
329 default:
330 return true, nil
331 }
332 }
333
334 // ExistX is like Exist, but panics if an error occurs.
335 func (_q *StatementQuery) ExistX(ctx context.Context) bool {
336 exist, err := _q.Exist(ctx)
337 if err != nil {
338 panic(err)
339 }
340 return exist
341 }
342
343 // Clone returns a duplicate of the StatementQuery builder, including all associated steps. It can be
344 // used to prepare common query builders and use them differently after the clone is made.
345 func (_q *StatementQuery) Clone() *StatementQuery {
346 if _q == nil {
347 return nil
348 }
349 return &StatementQuery{
350 config: _q.config,
351 ctx: _q.ctx.Clone(),
352 order: append([]statement.OrderOption{}, _q.order...),
353 inters: append([]Interceptor{}, _q.inters...),
354 predicates: append([]predicate.Statement{}, _q.predicates...),
355 withSubjects: _q.withSubjects.Clone(),
356 withPolicy: _q.withPolicy.Clone(),
357 withAttestationCollections: _q.withAttestationCollections.Clone(),
358 withDsse: _q.withDsse.Clone(),
359 // clone intermediate query.
360 sql: _q.sql.Clone(),
361 path: _q.path,
362 }
363 }
364
365 // WithSubjects tells the query-builder to eager-load the nodes that are connected to
366 // the "subjects" edge. The optional arguments are used to configure the query builder of the edge.
367 func (_q *StatementQuery) WithSubjects(opts ...func(*SubjectQuery)) *StatementQuery {
368 query := (&SubjectClient{config: _q.config}).Query()
369 for _, opt := range opts {
370 opt(query)
371 }
372 _q.withSubjects = query
373 return _q
374 }
375
376 // WithPolicy tells the query-builder to eager-load the nodes that are connected to
377 // the "policy" edge. The optional arguments are used to configure the query builder of the edge.
378 func (_q *StatementQuery) WithPolicy(opts ...func(*AttestationPolicyQuery)) *StatementQuery {
379 query := (&AttestationPolicyClient{config: _q.config}).Query()
380 for _, opt := range opts {
381 opt(query)
382 }
383 _q.withPolicy = query
384 return _q
385 }
386
387 // WithAttestationCollections tells the query-builder to eager-load the nodes that are connected to
388 // the "attestation_collections" edge. The optional arguments are used to configure the query builder of the edge.
389 func (_q *StatementQuery) WithAttestationCollections(opts ...func(*AttestationCollectionQuery)) *StatementQuery {
390 query := (&AttestationCollectionClient{config: _q.config}).Query()
391 for _, opt := range opts {
392 opt(query)
393 }
394 _q.withAttestationCollections = query
395 return _q
396 }
397
398 // WithDsse tells the query-builder to eager-load the nodes that are connected to
399 // the "dsse" edge. The optional arguments are used to configure the query builder of the edge.
400 func (_q *StatementQuery) WithDsse(opts ...func(*DsseQuery)) *StatementQuery {
401 query := (&DsseClient{config: _q.config}).Query()
402 for _, opt := range opts {
403 opt(query)
404 }
405 _q.withDsse = query
406 return _q
407 }
408
409 // GroupBy is used to group vertices by one or more fields/columns.
410 // It is often used with aggregate functions, like: count, max, mean, min, sum.
411 //
412 // Example:
413 //
414 // var v []struct {
415 // Predicate string `json:"predicate,omitempty"`
416 // Count int `json:"count,omitempty"`
417 // }
418 //
419 // client.Statement.Query().
420 // GroupBy(statement.FieldPredicate).
421 // Aggregate(ent.Count()).
422 // Scan(ctx, &v)
423 func (_q *StatementQuery) GroupBy(field string, fields ...string) *StatementGroupBy {
424 _q.ctx.Fields = append([]string{field}, fields...)
425 grbuild := &StatementGroupBy{build: _q}
426 grbuild.flds = &_q.ctx.Fields
427 grbuild.label = statement.Label
428 grbuild.scan = grbuild.Scan
429 return grbuild
430 }
431
432 // Select allows the selection one or more fields/columns for the given query,
433 // instead of selecting all fields in the entity.
434 //
435 // Example:
436 //
437 // var v []struct {
438 // Predicate string `json:"predicate,omitempty"`
439 // }
440 //
441 // client.Statement.Query().
442 // Select(statement.FieldPredicate).
443 // Scan(ctx, &v)
444 func (_q *StatementQuery) Select(fields ...string) *StatementSelect {
445 _q.ctx.Fields = append(_q.ctx.Fields, fields...)
446 sbuild := &StatementSelect{StatementQuery: _q}
447 sbuild.label = statement.Label
448 sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
449 return sbuild
450 }
451
452 // Aggregate returns a StatementSelect configured with the given aggregations.
453 func (_q *StatementQuery) Aggregate(fns ...AggregateFunc) *StatementSelect {
454 return _q.Select().Aggregate(fns...)
455 }
456
457 func (_q *StatementQuery) prepareQuery(ctx context.Context) error {
458 for _, inter := range _q.inters {
459 if inter == nil {
460 return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
461 }
462 if trv, ok := inter.(Traverser); ok {
463 if err := trv.Traverse(ctx, _q); err != nil {
464 return err
465 }
466 }
467 }
468 for _, f := range _q.ctx.Fields {
469 if !statement.ValidColumn(f) {
470 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
471 }
472 }
473 if _q.path != nil {
474 prev, err := _q.path(ctx)
475 if err != nil {
476 return err
477 }
478 _q.sql = prev
479 }
480 return nil
481 }
482
483 func (_q *StatementQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Statement, error) {
484 var (
485 nodes = []*Statement{}
486 _spec = _q.querySpec()
487 loadedTypes = [4]bool{
488 _q.withSubjects != nil,
489 _q.withPolicy != nil,
490 _q.withAttestationCollections != nil,
491 _q.withDsse != nil,
492 }
493 )
494 _spec.ScanValues = func(columns []string) ([]any, error) {
495 return (*Statement).scanValues(nil, columns)
496 }
497 _spec.Assign = func(columns []string, values []any) error {
498 node := &Statement{config: _q.config}
499 nodes = append(nodes, node)
500 node.Edges.loadedTypes = loadedTypes
501 return node.assignValues(columns, values)
502 }
503 if len(_q.modifiers) > 0 {
504 _spec.Modifiers = _q.modifiers
505 }
506 for i := range hooks {
507 hooks[i](ctx, _spec)
508 }
509 if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
510 return nil, err
511 }
512 if len(nodes) == 0 {
513 return nodes, nil
514 }
515 if query := _q.withSubjects; query != nil {
516 if err := _q.loadSubjects(ctx, query, nodes,
517 func(n *Statement) { n.Edges.Subjects = []*Subject{} },
518 func(n *Statement, e *Subject) { n.Edges.Subjects = append(n.Edges.Subjects, e) }); err != nil {
519 return nil, err
520 }
521 }
522 if query := _q.withPolicy; query != nil {
523 if err := _q.loadPolicy(ctx, query, nodes, nil,
524 func(n *Statement, e *AttestationPolicy) { n.Edges.Policy = e }); err != nil {
525 return nil, err
526 }
527 }
528 if query := _q.withAttestationCollections; query != nil {
529 if err := _q.loadAttestationCollections(ctx, query, nodes, nil,
530 func(n *Statement, e *AttestationCollection) { n.Edges.AttestationCollections = e }); err != nil {
531 return nil, err
532 }
533 }
534 if query := _q.withDsse; query != nil {
535 if err := _q.loadDsse(ctx, query, nodes,
536 func(n *Statement) { n.Edges.Dsse = []*Dsse{} },
537 func(n *Statement, e *Dsse) { n.Edges.Dsse = append(n.Edges.Dsse, e) }); err != nil {
538 return nil, err
539 }
540 }
541 for name, query := range _q.withNamedSubjects {
542 if err := _q.loadSubjects(ctx, query, nodes,
543 func(n *Statement) { n.appendNamedSubjects(name) },
544 func(n *Statement, e *Subject) { n.appendNamedSubjects(name, e) }); err != nil {
545 return nil, err
546 }
547 }
548 for name, query := range _q.withNamedDsse {
549 if err := _q.loadDsse(ctx, query, nodes,
550 func(n *Statement) { n.appendNamedDsse(name) },
551 func(n *Statement, e *Dsse) { n.appendNamedDsse(name, e) }); err != nil {
552 return nil, err
553 }
554 }
555 for i := range _q.loadTotal {
556 if err := _q.loadTotal[i](ctx, nodes); err != nil {
557 return nil, err
558 }
559 }
560 return nodes, nil
561 }
562
563 func (_q *StatementQuery) loadSubjects(ctx context.Context, query *SubjectQuery, nodes []*Statement, init func(*Statement), assign func(*Statement, *Subject)) error {
564 fks := make([]driver.Value, 0, len(nodes))
565 nodeids := make(map[uuid.UUID]*Statement)
566 for i := range nodes {
567 fks = append(fks, nodes[i].ID)
568 nodeids[nodes[i].ID] = nodes[i]
569 if init != nil {
570 init(nodes[i])
571 }
572 }
573 query.withFKs = true
574 query.Where(predicate.Subject(func(s *sql.Selector) {
575 s.Where(sql.InValues(s.C(statement.SubjectsColumn), fks...))
576 }))
577 neighbors, err := query.All(ctx)
578 if err != nil {
579 return err
580 }
581 for _, n := range neighbors {
582 fk := n.statement_subjects
583 if fk == nil {
584 return fmt.Errorf(`foreign-key "statement_subjects" is nil for node %v`, n.ID)
585 }
586 node, ok := nodeids[*fk]
587 if !ok {
588 return fmt.Errorf(`unexpected referenced foreign-key "statement_subjects" returned %v for node %v`, *fk, n.ID)
589 }
590 assign(node, n)
591 }
592 return nil
593 }
594 func (_q *StatementQuery) loadPolicy(ctx context.Context, query *AttestationPolicyQuery, nodes []*Statement, init func(*Statement), assign func(*Statement, *AttestationPolicy)) error {
595 fks := make([]driver.Value, 0, len(nodes))
596 nodeids := make(map[uuid.UUID]*Statement)
597 for i := range nodes {
598 fks = append(fks, nodes[i].ID)
599 nodeids[nodes[i].ID] = nodes[i]
600 }
601 query.withFKs = true
602 query.Where(predicate.AttestationPolicy(func(s *sql.Selector) {
603 s.Where(sql.InValues(s.C(statement.PolicyColumn), fks...))
604 }))
605 neighbors, err := query.All(ctx)
606 if err != nil {
607 return err
608 }
609 for _, n := range neighbors {
610 fk := n.statement_policy
611 if fk == nil {
612 return fmt.Errorf(`foreign-key "statement_policy" is nil for node %v`, n.ID)
613 }
614 node, ok := nodeids[*fk]
615 if !ok {
616 return fmt.Errorf(`unexpected referenced foreign-key "statement_policy" returned %v for node %v`, *fk, n.ID)
617 }
618 assign(node, n)
619 }
620 return nil
621 }
622 func (_q *StatementQuery) loadAttestationCollections(ctx context.Context, query *AttestationCollectionQuery, nodes []*Statement, init func(*Statement), assign func(*Statement, *AttestationCollection)) error {
623 fks := make([]driver.Value, 0, len(nodes))
624 nodeids := make(map[uuid.UUID]*Statement)
625 for i := range nodes {
626 fks = append(fks, nodes[i].ID)
627 nodeids[nodes[i].ID] = nodes[i]
628 }
629 query.withFKs = true
630 query.Where(predicate.AttestationCollection(func(s *sql.Selector) {
631 s.Where(sql.InValues(s.C(statement.AttestationCollectionsColumn), fks...))
632 }))
633 neighbors, err := query.All(ctx)
634 if err != nil {
635 return err
636 }
637 for _, n := range neighbors {
638 fk := n.statement_attestation_collections
639 if fk == nil {
640 return fmt.Errorf(`foreign-key "statement_attestation_collections" is nil for node %v`, n.ID)
641 }
642 node, ok := nodeids[*fk]
643 if !ok {
644 return fmt.Errorf(`unexpected referenced foreign-key "statement_attestation_collections" returned %v for node %v`, *fk, n.ID)
645 }
646 assign(node, n)
647 }
648 return nil
649 }
650 func (_q *StatementQuery) loadDsse(ctx context.Context, query *DsseQuery, nodes []*Statement, init func(*Statement), assign func(*Statement, *Dsse)) error {
651 fks := make([]driver.Value, 0, len(nodes))
652 nodeids := make(map[uuid.UUID]*Statement)
653 for i := range nodes {
654 fks = append(fks, nodes[i].ID)
655 nodeids[nodes[i].ID] = nodes[i]
656 if init != nil {
657 init(nodes[i])
658 }
659 }
660 query.withFKs = true
661 query.Where(predicate.Dsse(func(s *sql.Selector) {
662 s.Where(sql.InValues(s.C(statement.DsseColumn), fks...))
663 }))
664 neighbors, err := query.All(ctx)
665 if err != nil {
666 return err
667 }
668 for _, n := range neighbors {
669 fk := n.dsse_statement
670 if fk == nil {
671 return fmt.Errorf(`foreign-key "dsse_statement" is nil for node %v`, n.ID)
672 }
673 node, ok := nodeids[*fk]
674 if !ok {
675 return fmt.Errorf(`unexpected referenced foreign-key "dsse_statement" returned %v for node %v`, *fk, n.ID)
676 }
677 assign(node, n)
678 }
679 return nil
680 }
681
682 func (_q *StatementQuery) sqlCount(ctx context.Context) (int, error) {
683 _spec := _q.querySpec()
684 if len(_q.modifiers) > 0 {
685 _spec.Modifiers = _q.modifiers
686 }
687 _spec.Node.Columns = _q.ctx.Fields
688 if len(_q.ctx.Fields) > 0 {
689 _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
690 }
691 return sqlgraph.CountNodes(ctx, _q.driver, _spec)
692 }
693
694 func (_q *StatementQuery) querySpec() *sqlgraph.QuerySpec {
695 _spec := sqlgraph.NewQuerySpec(statement.Table, statement.Columns, sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID))
696 _spec.From = _q.sql
697 if unique := _q.ctx.Unique; unique != nil {
698 _spec.Unique = *unique
699 } else if _q.path != nil {
700 _spec.Unique = true
701 }
702 if fields := _q.ctx.Fields; len(fields) > 0 {
703 _spec.Node.Columns = make([]string, 0, len(fields))
704 _spec.Node.Columns = append(_spec.Node.Columns, statement.FieldID)
705 for i := range fields {
706 if fields[i] != statement.FieldID {
707 _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
708 }
709 }
710 }
711 if ps := _q.predicates; len(ps) > 0 {
712 _spec.Predicate = func(selector *sql.Selector) {
713 for i := range ps {
714 ps[i](selector)
715 }
716 }
717 }
718 if limit := _q.ctx.Limit; limit != nil {
719 _spec.Limit = *limit
720 }
721 if offset := _q.ctx.Offset; offset != nil {
722 _spec.Offset = *offset
723 }
724 if ps := _q.order; len(ps) > 0 {
725 _spec.Order = func(selector *sql.Selector) {
726 for i := range ps {
727 ps[i](selector)
728 }
729 }
730 }
731 return _spec
732 }
733
734 func (_q *StatementQuery) sqlQuery(ctx context.Context) *sql.Selector {
735 builder := sql.Dialect(_q.driver.Dialect())
736 t1 := builder.Table(statement.Table)
737 columns := _q.ctx.Fields
738 if len(columns) == 0 {
739 columns = statement.Columns
740 }
741 selector := builder.Select(t1.Columns(columns...)...).From(t1)
742 if _q.sql != nil {
743 selector = _q.sql
744 selector.Select(selector.Columns(columns...)...)
745 }
746 if _q.ctx.Unique != nil && *_q.ctx.Unique {
747 selector.Distinct()
748 }
749 for _, p := range _q.predicates {
750 p(selector)
751 }
752 for _, p := range _q.order {
753 p(selector)
754 }
755 if offset := _q.ctx.Offset; offset != nil {
756 // limit is mandatory for offset clause. We start
757 // with default value, and override it below if needed.
758 selector.Offset(*offset).Limit(math.MaxInt32)
759 }
760 if limit := _q.ctx.Limit; limit != nil {
761 selector.Limit(*limit)
762 }
763 return selector
764 }
765
766 // WithNamedSubjects tells the query-builder to eager-load the nodes that are connected to the "subjects"
767 // edge with the given name. The optional arguments are used to configure the query builder of the edge.
768 func (_q *StatementQuery) WithNamedSubjects(name string, opts ...func(*SubjectQuery)) *StatementQuery {
769 query := (&SubjectClient{config: _q.config}).Query()
770 for _, opt := range opts {
771 opt(query)
772 }
773 if _q.withNamedSubjects == nil {
774 _q.withNamedSubjects = make(map[string]*SubjectQuery)
775 }
776 _q.withNamedSubjects[name] = query
777 return _q
778 }
779
780 // WithNamedDsse tells the query-builder to eager-load the nodes that are connected to the "dsse"
781 // edge with the given name. The optional arguments are used to configure the query builder of the edge.
782 func (_q *StatementQuery) WithNamedDsse(name string, opts ...func(*DsseQuery)) *StatementQuery {
783 query := (&DsseClient{config: _q.config}).Query()
784 for _, opt := range opts {
785 opt(query)
786 }
787 if _q.withNamedDsse == nil {
788 _q.withNamedDsse = make(map[string]*DsseQuery)
789 }
790 _q.withNamedDsse[name] = query
791 return _q
792 }
793
794 // StatementGroupBy is the group-by builder for Statement entities.
795 type StatementGroupBy struct {
796 selector
797 build *StatementQuery
798 }
799
800 // Aggregate adds the given aggregation functions to the group-by query.
801 func (_g *StatementGroupBy) Aggregate(fns ...AggregateFunc) *StatementGroupBy {
802 _g.fns = append(_g.fns, fns...)
803 return _g
804 }
805
806 // Scan applies the selector query and scans the result into the given value.
807 func (_g *StatementGroupBy) Scan(ctx context.Context, v any) error {
808 ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
809 if err := _g.build.prepareQuery(ctx); err != nil {
810 return err
811 }
812 return scanWithInterceptors[*StatementQuery, *StatementGroupBy](ctx, _g.build, _g, _g.build.inters, v)
813 }
814
815 func (_g *StatementGroupBy) sqlScan(ctx context.Context, root *StatementQuery, v any) error {
816 selector := root.sqlQuery(ctx).Select()
817 aggregation := make([]string, 0, len(_g.fns))
818 for _, fn := range _g.fns {
819 aggregation = append(aggregation, fn(selector))
820 }
821 if len(selector.SelectedColumns()) == 0 {
822 columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
823 for _, f := range *_g.flds {
824 columns = append(columns, selector.C(f))
825 }
826 columns = append(columns, aggregation...)
827 selector.Select(columns...)
828 }
829 selector.GroupBy(selector.Columns(*_g.flds...)...)
830 if err := selector.Err(); err != nil {
831 return err
832 }
833 rows := &sql.Rows{}
834 query, args := selector.Query()
835 if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
836 return err
837 }
838 defer rows.Close()
839 return sql.ScanSlice(rows, v)
840 }
841
842 // StatementSelect is the builder for selecting fields of Statement entities.
843 type StatementSelect struct {
844 *StatementQuery
845 selector
846 }
847
848 // Aggregate adds the given aggregation functions to the selector query.
849 func (_s *StatementSelect) Aggregate(fns ...AggregateFunc) *StatementSelect {
850 _s.fns = append(_s.fns, fns...)
851 return _s
852 }
853
854 // Scan applies the selector query and scans the result into the given value.
855 func (_s *StatementSelect) Scan(ctx context.Context, v any) error {
856 ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
857 if err := _s.prepareQuery(ctx); err != nil {
858 return err
859 }
860 return scanWithInterceptors[*StatementQuery, *StatementSelect](ctx, _s.StatementQuery, _s, _s.inters, v)
861 }
862
863 func (_s *StatementSelect) sqlScan(ctx context.Context, root *StatementQuery, v any) error {
864 selector := root.sqlQuery(ctx)
865 aggregation := make([]string, 0, len(_s.fns))
866 for _, fn := range _s.fns {
867 aggregation = append(aggregation, fn(selector))
868 }
869 switch n := len(*_s.selector.flds); {
870 case n == 0 && len(aggregation) > 0:
871 selector.Select(aggregation...)
872 case n != 0 && len(aggregation) > 0:
873 selector.AppendSelect(aggregation...)
874 }
875 rows := &sql.Rows{}
876 query, args := selector.Query()
877 if err := _s.driver.Query(ctx, query, args, rows); err != nil {
878 return err
879 }
880 defer rows.Close()
881 return sql.ScanSlice(rows, v)
882 }