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