| 1 |
// Code generated by ent, DO NOT EDIT. |
| 2 |
|
| 3 |
package ent |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
"sync" |
| 11 |
|
| 12 |
"entgo.io/ent" |
| 13 |
"entgo.io/ent/dialect/sql" |
| 14 |
"entgo.io/ent/dialect/sql/sqlgraph" |
| 15 |
"github.com/in-toto/archivista/ent/attestation" |
| 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/payloaddigest" |
| 20 |
"github.com/in-toto/archivista/ent/signature" |
| 21 |
"github.com/in-toto/archivista/ent/statement" |
| 22 |
"github.com/in-toto/archivista/ent/subject" |
| 23 |
"github.com/in-toto/archivista/ent/subjectdigest" |
| 24 |
"github.com/in-toto/archivista/ent/timestamp" |
| 25 |
) |
| 26 |
|
| 27 |
// ent aliases to avoid import conflicts in user's code. |
| 28 |
type ( |
| 29 |
Op = ent.Op |
| 30 |
Hook = ent.Hook |
| 31 |
Value = ent.Value |
| 32 |
Query = ent.Query |
| 33 |
QueryContext = ent.QueryContext |
| 34 |
Querier = ent.Querier |
| 35 |
QuerierFunc = ent.QuerierFunc |
| 36 |
Interceptor = ent.Interceptor |
| 37 |
InterceptFunc = ent.InterceptFunc |
| 38 |
Traverser = ent.Traverser |
| 39 |
TraverseFunc = ent.TraverseFunc |
| 40 |
Policy = ent.Policy |
| 41 |
Mutator = ent.Mutator |
| 42 |
Mutation = ent.Mutation |
| 43 |
MutateFunc = ent.MutateFunc |
| 44 |
) |
| 45 |
|
| 46 |
type clientCtxKey struct{} |
| 47 |
|
| 48 |
// FromContext returns a Client stored inside a context, or nil if there isn't one. |
| 49 |
func FromContext(ctx context.Context) *Client { |
| 50 |
c, _ := ctx.Value(clientCtxKey{}).(*Client) |
| 51 |
return c |
| 52 |
} |
| 53 |
|
| 54 |
// NewContext returns a new context with the given Client attached. |
| 55 |
func NewContext(parent context.Context, c *Client) context.Context { |
| 56 |
return context.WithValue(parent, clientCtxKey{}, c) |
| 57 |
} |
| 58 |
|
| 59 |
type txCtxKey struct{} |
| 60 |
|
| 61 |
// TxFromContext returns a Tx stored inside a context, or nil if there isn't one. |
| 62 |
func TxFromContext(ctx context.Context) *Tx { |
| 63 |
tx, _ := ctx.Value(txCtxKey{}).(*Tx) |
| 64 |
return tx |
| 65 |
} |
| 66 |
|
| 67 |
// NewTxContext returns a new context with the given Tx attached. |
| 68 |
func NewTxContext(parent context.Context, tx *Tx) context.Context { |
| 69 |
return context.WithValue(parent, txCtxKey{}, tx) |
| 70 |
} |
| 71 |
|
| 72 |
// OrderFunc applies an ordering on the sql selector. |
| 73 |
// Deprecated: Use Asc/Desc functions or the package builders instead. |
| 74 |
type OrderFunc func(*sql.Selector) |
| 75 |
|
| 76 |
var ( |
| 77 |
initCheck sync.Once |
| 78 |
columnCheck sql.ColumnCheck |
| 79 |
) |
| 80 |
|
| 81 |
// checkColumn checks if the column exists in the given table. |
| 82 |
func checkColumn(t, c string) error { |
| 83 |
initCheck.Do(func() { |
| 84 |
columnCheck = sql.NewColumnCheck(map[string]func(string) bool{ |
| 85 |
attestation.Table: attestation.ValidColumn, |
| 86 |
attestationcollection.Table: attestationcollection.ValidColumn, |
| 87 |
attestationpolicy.Table: attestationpolicy.ValidColumn, |
| 88 |
dsse.Table: dsse.ValidColumn, |
| 89 |
payloaddigest.Table: payloaddigest.ValidColumn, |
| 90 |
signature.Table: signature.ValidColumn, |
| 91 |
statement.Table: statement.ValidColumn, |
| 92 |
subject.Table: subject.ValidColumn, |
| 93 |
subjectdigest.Table: subjectdigest.ValidColumn, |
| 94 |
timestamp.Table: timestamp.ValidColumn, |
| 95 |
}) |
| 96 |
}) |
| 97 |
return columnCheck(t, c) |
| 98 |
} |
| 99 |
|
| 100 |
// Asc applies the given fields in ASC order. |
| 101 |
func Asc(fields ...string) func(*sql.Selector) { |
| 102 |
return func(s *sql.Selector) { |
| 103 |
for _, f := range fields { |
| 104 |
if err := checkColumn(s.TableName(), f); err != nil { |
| 105 |
s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)}) |
| 106 |
} |
| 107 |
s.OrderBy(sql.Asc(s.C(f))) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// Desc applies the given fields in DESC order. |
| 113 |
func Desc(fields ...string) func(*sql.Selector) { |
| 114 |
return func(s *sql.Selector) { |
| 115 |
for _, f := range fields { |
| 116 |
if err := checkColumn(s.TableName(), f); err != nil { |
| 117 |
s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)}) |
| 118 |
} |
| 119 |
s.OrderBy(sql.Desc(s.C(f))) |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// AggregateFunc applies an aggregation step on the group-by traversal/selector. |
| 125 |
type AggregateFunc func(*sql.Selector) string |
| 126 |
|
| 127 |
// As is a pseudo aggregation function for renaming another other functions with custom names. For example: |
| 128 |
// |
| 129 |
// GroupBy(field1, field2). |
| 130 |
// Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")). |
| 131 |
// Scan(ctx, &v) |
| 132 |
func As(fn AggregateFunc, end string) AggregateFunc { |
| 133 |
return func(s *sql.Selector) string { |
| 134 |
return sql.As(fn(s), end) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Count applies the "count" aggregation function on each group. |
| 139 |
func Count() AggregateFunc { |
| 140 |
return func(s *sql.Selector) string { |
| 141 |
return sql.Count("*") |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Max applies the "max" aggregation function on the given field of each group. |
| 146 |
func Max(field string) AggregateFunc { |
| 147 |
return func(s *sql.Selector) string { |
| 148 |
if err := checkColumn(s.TableName(), field); err != nil { |
| 149 |
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) |
| 150 |
return "" |
| 151 |
} |
| 152 |
return sql.Max(s.C(field)) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// Mean applies the "mean" aggregation function on the given field of each group. |
| 157 |
func Mean(field string) AggregateFunc { |
| 158 |
return func(s *sql.Selector) string { |
| 159 |
if err := checkColumn(s.TableName(), field); err != nil { |
| 160 |
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) |
| 161 |
return "" |
| 162 |
} |
| 163 |
return sql.Avg(s.C(field)) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// Min applies the "min" aggregation function on the given field of each group. |
| 168 |
func Min(field string) AggregateFunc { |
| 169 |
return func(s *sql.Selector) string { |
| 170 |
if err := checkColumn(s.TableName(), field); err != nil { |
| 171 |
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) |
| 172 |
return "" |
| 173 |
} |
| 174 |
return sql.Min(s.C(field)) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// Sum applies the "sum" aggregation function on the given field of each group. |
| 179 |
func Sum(field string) AggregateFunc { |
| 180 |
return func(s *sql.Selector) string { |
| 181 |
if err := checkColumn(s.TableName(), field); err != nil { |
| 182 |
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)}) |
| 183 |
return "" |
| 184 |
} |
| 185 |
return sql.Sum(s.C(field)) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// ValidationError returns when validating a field or edge fails. |
| 190 |
type ValidationError struct { |
| 191 |
Name string // Field or edge name. |
| 192 |
err error |
| 193 |
} |
| 194 |
|
| 195 |
// Error implements the error interface. |
| 196 |
func (e *ValidationError) Error() string { |
| 197 |
return e.err.Error() |
| 198 |
} |
| 199 |
|
| 200 |
// Unwrap implements the errors.Wrapper interface. |
| 201 |
func (e *ValidationError) Unwrap() error { |
| 202 |
return e.err |
| 203 |
} |
| 204 |
|
| 205 |
// IsValidationError returns a boolean indicating whether the error is a validation error. |
| 206 |
func IsValidationError(err error) bool { |
| 207 |
if err == nil { |
| 208 |
return false |
| 209 |
} |
| 210 |
var e *ValidationError |
| 211 |
return errors.As(err, &e) |
| 212 |
} |
| 213 |
|
| 214 |
// NotFoundError returns when trying to fetch a specific entity and it was not found in the database. |
| 215 |
type NotFoundError struct { |
| 216 |
label string |
| 217 |
} |
| 218 |
|
| 219 |
// Error implements the error interface. |
| 220 |
func (e *NotFoundError) Error() string { |
| 221 |
return "ent: " + e.label + " not found" |
| 222 |
} |
| 223 |
|
| 224 |
// IsNotFound returns a boolean indicating whether the error is a not found error. |
| 225 |
func IsNotFound(err error) bool { |
| 226 |
if err == nil { |
| 227 |
return false |
| 228 |
} |
| 229 |
var e *NotFoundError |
| 230 |
return errors.As(err, &e) |
| 231 |
} |
| 232 |
|
| 233 |
// MaskNotFound masks not found error. |
| 234 |
func MaskNotFound(err error) error { |
| 235 |
if IsNotFound(err) { |
| 236 |
return nil |
| 237 |
} |
| 238 |
return err |
| 239 |
} |
| 240 |
|
| 241 |
// NotSingularError returns when trying to fetch a singular entity and more then one was found in the database. |
| 242 |
type NotSingularError struct { |
| 243 |
label string |
| 244 |
} |
| 245 |
|
| 246 |
// Error implements the error interface. |
| 247 |
func (e *NotSingularError) Error() string { |
| 248 |
return "ent: " + e.label + " not singular" |
| 249 |
} |
| 250 |
|
| 251 |
// IsNotSingular returns a boolean indicating whether the error is a not singular error. |
| 252 |
func IsNotSingular(err error) bool { |
| 253 |
if err == nil { |
| 254 |
return false |
| 255 |
} |
| 256 |
var e *NotSingularError |
| 257 |
return errors.As(err, &e) |
| 258 |
} |
| 259 |
|
| 260 |
// NotLoadedError returns when trying to get a node that was not loaded by the query. |
| 261 |
type NotLoadedError struct { |
| 262 |
edge string |
| 263 |
} |
| 264 |
|
| 265 |
// Error implements the error interface. |
| 266 |
func (e *NotLoadedError) Error() string { |
| 267 |
return "ent: " + e.edge + " edge was not loaded" |
| 268 |
} |
| 269 |
|
| 270 |
// IsNotLoaded returns a boolean indicating whether the error is a not loaded error. |
| 271 |
func IsNotLoaded(err error) bool { |
| 272 |
if err == nil { |
| 273 |
return false |
| 274 |
} |
| 275 |
var e *NotLoadedError |
| 276 |
return errors.As(err, &e) |
| 277 |
} |
| 278 |
|
| 279 |
// ConstraintError returns when trying to create/update one or more entities and |
| 280 |
// one or more of their constraints failed. For example, violation of edge or |
| 281 |
// field uniqueness. |
| 282 |
type ConstraintError struct { |
| 283 |
msg string |
| 284 |
wrap error |
| 285 |
} |
| 286 |
|
| 287 |
// Error implements the error interface. |
| 288 |
func (e ConstraintError) Error() string { |
| 289 |
return "ent: constraint failed: " + e.msg |
| 290 |
} |
| 291 |
|
| 292 |
// Unwrap implements the errors.Wrapper interface. |
| 293 |
func (e *ConstraintError) Unwrap() error { |
| 294 |
return e.wrap |
| 295 |
} |
| 296 |
|
| 297 |
// IsConstraintError returns a boolean indicating whether the error is a constraint failure. |
| 298 |
func IsConstraintError(err error) bool { |
| 299 |
if err == nil { |
| 300 |
return false |
| 301 |
} |
| 302 |
var e *ConstraintError |
| 303 |
return errors.As(err, &e) |
| 304 |
} |
| 305 |
|
| 306 |
// selector embedded by the different Select/GroupBy builders. |
| 307 |
type selector struct { |
| 308 |
label string |
| 309 |
flds *[]string |
| 310 |
fns []AggregateFunc |
| 311 |
scan func(context.Context, any) error |
| 312 |
} |
| 313 |
|
| 314 |
// ScanX is like Scan, but panics if an error occurs. |
| 315 |
func (s *selector) ScanX(ctx context.Context, v any) { |
| 316 |
if err := s.scan(ctx, v); err != nil { |
| 317 |
panic(err) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// Strings returns list of strings from a selector. It is only allowed when selecting one field. |
| 322 |
func (s *selector) Strings(ctx context.Context) ([]string, error) { |
| 323 |
if len(*s.flds) > 1 { |
| 324 |
return nil, errors.New("ent: Strings is not achievable when selecting more than 1 field") |
| 325 |
} |
| 326 |
var v []string |
| 327 |
if err := s.scan(ctx, &v); err != nil { |
| 328 |
return nil, err |
| 329 |
} |
| 330 |
return v, nil |
| 331 |
} |
| 332 |
|
| 333 |
// StringsX is like Strings, but panics if an error occurs. |
| 334 |
func (s *selector) StringsX(ctx context.Context) []string { |
| 335 |
v, err := s.Strings(ctx) |
| 336 |
if err != nil { |
| 337 |
panic(err) |
| 338 |
} |
| 339 |
return v |
| 340 |
} |
| 341 |
|
| 342 |
// String returns a single string from a selector. It is only allowed when selecting one field. |
| 343 |
func (s *selector) String(ctx context.Context) (_ string, err error) { |
| 344 |
var v []string |
| 345 |
if v, err = s.Strings(ctx); err != nil { |
| 346 |
return |
| 347 |
} |
| 348 |
switch len(v) { |
| 349 |
case 1: |
| 350 |
return v[0], nil |
| 351 |
case 0: |
| 352 |
err = &NotFoundError{s.label} |
| 353 |
default: |
| 354 |
err = fmt.Errorf("ent: Strings returned %d results when one was expected", len(v)) |
| 355 |
} |
| 356 |
return |
| 357 |
} |
| 358 |
|
| 359 |
// StringX is like String, but panics if an error occurs. |
| 360 |
func (s *selector) StringX(ctx context.Context) string { |
| 361 |
v, err := s.String(ctx) |
| 362 |
if err != nil { |
| 363 |
panic(err) |
| 364 |
} |
| 365 |
return v |
| 366 |
} |
| 367 |
|
| 368 |
// Ints returns list of ints from a selector. It is only allowed when selecting one field. |
| 369 |
func (s *selector) Ints(ctx context.Context) ([]int, error) { |
| 370 |
if len(*s.flds) > 1 { |
| 371 |
return nil, errors.New("ent: Ints is not achievable when selecting more than 1 field") |
| 372 |
} |
| 373 |
var v []int |
| 374 |
if err := s.scan(ctx, &v); err != nil { |
| 375 |
return nil, err |
| 376 |
} |
| 377 |
return v, nil |
| 378 |
} |
| 379 |
|
| 380 |
// IntsX is like Ints, but panics if an error occurs. |
| 381 |
func (s *selector) IntsX(ctx context.Context) []int { |
| 382 |
v, err := s.Ints(ctx) |
| 383 |
if err != nil { |
| 384 |
panic(err) |
| 385 |
} |
| 386 |
return v |
| 387 |
} |
| 388 |
|
| 389 |
// Int returns a single int from a selector. It is only allowed when selecting one field. |
| 390 |
func (s *selector) Int(ctx context.Context) (_ int, err error) { |
| 391 |
var v []int |
| 392 |
if v, err = s.Ints(ctx); err != nil { |
| 393 |
return |
| 394 |
} |
| 395 |
switch len(v) { |
| 396 |
case 1: |
| 397 |
return v[0], nil |
| 398 |
case 0: |
| 399 |
err = &NotFoundError{s.label} |
| 400 |
default: |
| 401 |
err = fmt.Errorf("ent: Ints returned %d results when one was expected", len(v)) |
| 402 |
} |
| 403 |
return |
| 404 |
} |
| 405 |
|
| 406 |
// IntX is like Int, but panics if an error occurs. |
| 407 |
func (s *selector) IntX(ctx context.Context) int { |
| 408 |
v, err := s.Int(ctx) |
| 409 |
if err != nil { |
| 410 |
panic(err) |
| 411 |
} |
| 412 |
return v |
| 413 |
} |
| 414 |
|
| 415 |
// Float64s returns list of float64s from a selector. It is only allowed when selecting one field. |
| 416 |
func (s *selector) Float64s(ctx context.Context) ([]float64, error) { |
| 417 |
if len(*s.flds) > 1 { |
| 418 |
return nil, errors.New("ent: Float64s is not achievable when selecting more than 1 field") |
| 419 |
} |
| 420 |
var v []float64 |
| 421 |
if err := s.scan(ctx, &v); err != nil { |
| 422 |
return nil, err |
| 423 |
} |
| 424 |
return v, nil |
| 425 |
} |
| 426 |
|
| 427 |
// Float64sX is like Float64s, but panics if an error occurs. |
| 428 |
func (s *selector) Float64sX(ctx context.Context) []float64 { |
| 429 |
v, err := s.Float64s(ctx) |
| 430 |
if err != nil { |
| 431 |
panic(err) |
| 432 |
} |
| 433 |
return v |
| 434 |
} |
| 435 |
|
| 436 |
// Float64 returns a single float64 from a selector. It is only allowed when selecting one field. |
| 437 |
func (s *selector) Float64(ctx context.Context) (_ float64, err error) { |
| 438 |
var v []float64 |
| 439 |
if v, err = s.Float64s(ctx); err != nil { |
| 440 |
return |
| 441 |
} |
| 442 |
switch len(v) { |
| 443 |
case 1: |
| 444 |
return v[0], nil |
| 445 |
case 0: |
| 446 |
err = &NotFoundError{s.label} |
| 447 |
default: |
| 448 |
err = fmt.Errorf("ent: Float64s returned %d results when one was expected", len(v)) |
| 449 |
} |
| 450 |
return |
| 451 |
} |
| 452 |
|
| 453 |
// Float64X is like Float64, but panics if an error occurs. |
| 454 |
func (s *selector) Float64X(ctx context.Context) float64 { |
| 455 |
v, err := s.Float64(ctx) |
| 456 |
if err != nil { |
| 457 |
panic(err) |
| 458 |
} |
| 459 |
return v |
| 460 |
} |
| 461 |
|
| 462 |
// Bools returns list of bools from a selector. It is only allowed when selecting one field. |
| 463 |
func (s *selector) Bools(ctx context.Context) ([]bool, error) { |
| 464 |
if len(*s.flds) > 1 { |
| 465 |
return nil, errors.New("ent: Bools is not achievable when selecting more than 1 field") |
| 466 |
} |
| 467 |
var v []bool |
| 468 |
if err := s.scan(ctx, &v); err != nil { |
| 469 |
return nil, err |
| 470 |
} |
| 471 |
return v, nil |
| 472 |
} |
| 473 |
|
| 474 |
// BoolsX is like Bools, but panics if an error occurs. |
| 475 |
func (s *selector) BoolsX(ctx context.Context) []bool { |
| 476 |
v, err := s.Bools(ctx) |
| 477 |
if err != nil { |
| 478 |
panic(err) |
| 479 |
} |
| 480 |
return v |
| 481 |
} |
| 482 |
|
| 483 |
// Bool returns a single bool from a selector. It is only allowed when selecting one field. |
| 484 |
func (s *selector) Bool(ctx context.Context) (_ bool, err error) { |
| 485 |
var v []bool |
| 486 |
if v, err = s.Bools(ctx); err != nil { |
| 487 |
return |
| 488 |
} |
| 489 |
switch len(v) { |
| 490 |
case 1: |
| 491 |
return v[0], nil |
| 492 |
case 0: |
| 493 |
err = &NotFoundError{s.label} |
| 494 |
default: |
| 495 |
err = fmt.Errorf("ent: Bools returned %d results when one was expected", len(v)) |
| 496 |
} |
| 497 |
return |
| 498 |
} |
| 499 |
|
| 500 |
// BoolX is like Bool, but panics if an error occurs. |
| 501 |
func (s *selector) BoolX(ctx context.Context) bool { |
| 502 |
v, err := s.Bool(ctx) |
| 503 |
if err != nil { |
| 504 |
panic(err) |
| 505 |
} |
| 506 |
return v |
| 507 |
} |
| 508 |
|
| 509 |
// withHooks invokes the builder operation with the given hooks, if any. |
| 510 |
func withHooks[V Value, M any, PM interface { |
| 511 |
*M |
| 512 |
Mutation |
| 513 |
}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { |
| 514 |
if len(hooks) == 0 { |
| 515 |
return exec(ctx) |
| 516 |
} |
| 517 |
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 518 |
mutationT, ok := any(m).(PM) |
| 519 |
if !ok { |
| 520 |
return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 521 |
} |
| 522 |
// Set the mutation to the builder. |
| 523 |
*mutation = *mutationT |
| 524 |
return exec(ctx) |
| 525 |
}) |
| 526 |
for i := len(hooks) - 1; i >= 0; i-- { |
| 527 |
if hooks[i] == nil { |
| 528 |
return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") |
| 529 |
} |
| 530 |
mut = hooks[i](mut) |
| 531 |
} |
| 532 |
v, err := mut.Mutate(ctx, mutation) |
| 533 |
if err != nil { |
| 534 |
return value, err |
| 535 |
} |
| 536 |
nv, ok := v.(V) |
| 537 |
if !ok { |
| 538 |
return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) |
| 539 |
} |
| 540 |
return nv, nil |
| 541 |
} |
| 542 |
|
| 543 |
// setContextOp returns a new context with the given QueryContext attached (including its op) in case it does not exist. |
| 544 |
func setContextOp(ctx context.Context, qc *QueryContext, op string) context.Context { |
| 545 |
if ent.QueryFromContext(ctx) == nil { |
| 546 |
qc.Op = op |
| 547 |
ctx = ent.NewQueryContext(ctx, qc) |
| 548 |
} |
| 549 |
return ctx |
| 550 |
} |
| 551 |
|
| 552 |
func querierAll[V Value, Q interface { |
| 553 |
sqlAll(context.Context, ...queryHook) (V, error) |
| 554 |
}]() Querier { |
| 555 |
return QuerierFunc(func(ctx context.Context, q Query) (Value, error) { |
| 556 |
query, ok := q.(Q) |
| 557 |
if !ok { |
| 558 |
return nil, fmt.Errorf("unexpected query type %T", q) |
| 559 |
} |
| 560 |
return query.sqlAll(ctx) |
| 561 |
}) |
| 562 |
} |
| 563 |
|
| 564 |
func querierCount[Q interface { |
| 565 |
sqlCount(context.Context) (int, error) |
| 566 |
}]() Querier { |
| 567 |
return QuerierFunc(func(ctx context.Context, q Query) (Value, error) { |
| 568 |
query, ok := q.(Q) |
| 569 |
if !ok { |
| 570 |
return nil, fmt.Errorf("unexpected query type %T", q) |
| 571 |
} |
| 572 |
return query.sqlCount(ctx) |
| 573 |
}) |
| 574 |
} |
| 575 |
|
| 576 |
func withInterceptors[V Value](ctx context.Context, q Query, qr Querier, inters []Interceptor) (v V, err error) { |
| 577 |
for i := len(inters) - 1; i >= 0; i-- { |
| 578 |
qr = inters[i].Intercept(qr) |
| 579 |
} |
| 580 |
rv, err := qr.Query(ctx, q) |
| 581 |
if err != nil { |
| 582 |
return v, err |
| 583 |
} |
| 584 |
vt, ok := rv.(V) |
| 585 |
if !ok { |
| 586 |
return v, fmt.Errorf("unexpected type %T returned from %T. expected type: %T", vt, q, v) |
| 587 |
} |
| 588 |
return vt, nil |
| 589 |
} |
| 590 |
|
| 591 |
func scanWithInterceptors[Q1 ent.Query, Q2 interface { |
| 592 |
sqlScan(context.Context, Q1, any) error |
| 593 |
}](ctx context.Context, rootQuery Q1, selectOrGroup Q2, inters []Interceptor, v any) error { |
| 594 |
rv := reflect.ValueOf(v) |
| 595 |
var qr Querier = QuerierFunc(func(ctx context.Context, q Query) (Value, error) { |
| 596 |
query, ok := q.(Q1) |
| 597 |
if !ok { |
| 598 |
return nil, fmt.Errorf("unexpected query type %T", q) |
| 599 |
} |
| 600 |
if err := selectOrGroup.sqlScan(ctx, query, v); err != nil { |
| 601 |
return nil, err |
| 602 |
} |
| 603 |
if k := rv.Kind(); k == reflect.Pointer && rv.Elem().CanInterface() { |
| 604 |
return rv.Elem().Interface(), nil |
| 605 |
} |
| 606 |
return v, nil |
| 607 |
}) |
| 608 |
for i := len(inters) - 1; i >= 0; i-- { |
| 609 |
qr = inters[i].Intercept(qr) |
| 610 |
} |
| 611 |
vv, err := qr.Query(ctx, rootQuery) |
| 612 |
if err != nil { |
| 613 |
return err |
| 614 |
} |
| 615 |
switch rv2 := reflect.ValueOf(vv); { |
| 616 |
case rv.IsNil(), rv2.IsNil(), rv.Kind() != reflect.Pointer: |
| 617 |
case rv.Type() == rv2.Type(): |
| 618 |
rv.Elem().Set(rv2.Elem()) |
| 619 |
case rv.Elem().Type() == rv2.Type(): |
| 620 |
rv.Elem().Set(rv2) |
| 621 |
} |
| 622 |
return nil |
| 623 |
} |
| 624 |
|
| 625 |
// queryHook describes an internal hook for the different sqlAll methods. |
| 626 |
type queryHook func(context.Context, *sqlgraph.QuerySpec) |