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