github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/bouncer_query.go (about) 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/dialect/sql" 11 "entgo.io/ent/dialect/sql/sqlgraph" 12 "entgo.io/ent/schema/field" 13 "github.com/crowdsecurity/crowdsec/pkg/database/ent/bouncer" 14 "github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate" 15 ) 16 17 // BouncerQuery is the builder for querying Bouncer entities. 18 type BouncerQuery struct { 19 config 20 ctx *QueryContext 21 order []bouncer.OrderOption 22 inters []Interceptor 23 predicates []predicate.Bouncer 24 // intermediate query (i.e. traversal path). 25 sql *sql.Selector 26 path func(context.Context) (*sql.Selector, error) 27 } 28 29 // Where adds a new predicate for the BouncerQuery builder. 30 func (bq *BouncerQuery) Where(ps ...predicate.Bouncer) *BouncerQuery { 31 bq.predicates = append(bq.predicates, ps...) 32 return bq 33 } 34 35 // Limit the number of records to be returned by this query. 36 func (bq *BouncerQuery) Limit(limit int) *BouncerQuery { 37 bq.ctx.Limit = &limit 38 return bq 39 } 40 41 // Offset to start from. 42 func (bq *BouncerQuery) Offset(offset int) *BouncerQuery { 43 bq.ctx.Offset = &offset 44 return bq 45 } 46 47 // Unique configures the query builder to filter duplicate records on query. 48 // By default, unique is set to true, and can be disabled using this method. 49 func (bq *BouncerQuery) Unique(unique bool) *BouncerQuery { 50 bq.ctx.Unique = &unique 51 return bq 52 } 53 54 // Order specifies how the records should be ordered. 55 func (bq *BouncerQuery) Order(o ...bouncer.OrderOption) *BouncerQuery { 56 bq.order = append(bq.order, o...) 57 return bq 58 } 59 60 // First returns the first Bouncer entity from the query. 61 // Returns a *NotFoundError when no Bouncer was found. 62 func (bq *BouncerQuery) First(ctx context.Context) (*Bouncer, error) { 63 nodes, err := bq.Limit(1).All(setContextOp(ctx, bq.ctx, "First")) 64 if err != nil { 65 return nil, err 66 } 67 if len(nodes) == 0 { 68 return nil, &NotFoundError{bouncer.Label} 69 } 70 return nodes[0], nil 71 } 72 73 // FirstX is like First, but panics if an error occurs. 74 func (bq *BouncerQuery) FirstX(ctx context.Context) *Bouncer { 75 node, err := bq.First(ctx) 76 if err != nil && !IsNotFound(err) { 77 panic(err) 78 } 79 return node 80 } 81 82 // FirstID returns the first Bouncer ID from the query. 83 // Returns a *NotFoundError when no Bouncer ID was found. 84 func (bq *BouncerQuery) FirstID(ctx context.Context) (id int, err error) { 85 var ids []int 86 if ids, err = bq.Limit(1).IDs(setContextOp(ctx, bq.ctx, "FirstID")); err != nil { 87 return 88 } 89 if len(ids) == 0 { 90 err = &NotFoundError{bouncer.Label} 91 return 92 } 93 return ids[0], nil 94 } 95 96 // FirstIDX is like FirstID, but panics if an error occurs. 97 func (bq *BouncerQuery) FirstIDX(ctx context.Context) int { 98 id, err := bq.FirstID(ctx) 99 if err != nil && !IsNotFound(err) { 100 panic(err) 101 } 102 return id 103 } 104 105 // Only returns a single Bouncer entity found by the query, ensuring it only returns one. 106 // Returns a *NotSingularError when more than one Bouncer entity is found. 107 // Returns a *NotFoundError when no Bouncer entities are found. 108 func (bq *BouncerQuery) Only(ctx context.Context) (*Bouncer, error) { 109 nodes, err := bq.Limit(2).All(setContextOp(ctx, bq.ctx, "Only")) 110 if err != nil { 111 return nil, err 112 } 113 switch len(nodes) { 114 case 1: 115 return nodes[0], nil 116 case 0: 117 return nil, &NotFoundError{bouncer.Label} 118 default: 119 return nil, &NotSingularError{bouncer.Label} 120 } 121 } 122 123 // OnlyX is like Only, but panics if an error occurs. 124 func (bq *BouncerQuery) OnlyX(ctx context.Context) *Bouncer { 125 node, err := bq.Only(ctx) 126 if err != nil { 127 panic(err) 128 } 129 return node 130 } 131 132 // OnlyID is like Only, but returns the only Bouncer ID in the query. 133 // Returns a *NotSingularError when more than one Bouncer ID is found. 134 // Returns a *NotFoundError when no entities are found. 135 func (bq *BouncerQuery) OnlyID(ctx context.Context) (id int, err error) { 136 var ids []int 137 if ids, err = bq.Limit(2).IDs(setContextOp(ctx, bq.ctx, "OnlyID")); err != nil { 138 return 139 } 140 switch len(ids) { 141 case 1: 142 id = ids[0] 143 case 0: 144 err = &NotFoundError{bouncer.Label} 145 default: 146 err = &NotSingularError{bouncer.Label} 147 } 148 return 149 } 150 151 // OnlyIDX is like OnlyID, but panics if an error occurs. 152 func (bq *BouncerQuery) OnlyIDX(ctx context.Context) int { 153 id, err := bq.OnlyID(ctx) 154 if err != nil { 155 panic(err) 156 } 157 return id 158 } 159 160 // All executes the query and returns a list of Bouncers. 161 func (bq *BouncerQuery) All(ctx context.Context) ([]*Bouncer, error) { 162 ctx = setContextOp(ctx, bq.ctx, "All") 163 if err := bq.prepareQuery(ctx); err != nil { 164 return nil, err 165 } 166 qr := querierAll[[]*Bouncer, *BouncerQuery]() 167 return withInterceptors[[]*Bouncer](ctx, bq, qr, bq.inters) 168 } 169 170 // AllX is like All, but panics if an error occurs. 171 func (bq *BouncerQuery) AllX(ctx context.Context) []*Bouncer { 172 nodes, err := bq.All(ctx) 173 if err != nil { 174 panic(err) 175 } 176 return nodes 177 } 178 179 // IDs executes the query and returns a list of Bouncer IDs. 180 func (bq *BouncerQuery) IDs(ctx context.Context) (ids []int, err error) { 181 if bq.ctx.Unique == nil && bq.path != nil { 182 bq.Unique(true) 183 } 184 ctx = setContextOp(ctx, bq.ctx, "IDs") 185 if err = bq.Select(bouncer.FieldID).Scan(ctx, &ids); err != nil { 186 return nil, err 187 } 188 return ids, nil 189 } 190 191 // IDsX is like IDs, but panics if an error occurs. 192 func (bq *BouncerQuery) IDsX(ctx context.Context) []int { 193 ids, err := bq.IDs(ctx) 194 if err != nil { 195 panic(err) 196 } 197 return ids 198 } 199 200 // Count returns the count of the given query. 201 func (bq *BouncerQuery) Count(ctx context.Context) (int, error) { 202 ctx = setContextOp(ctx, bq.ctx, "Count") 203 if err := bq.prepareQuery(ctx); err != nil { 204 return 0, err 205 } 206 return withInterceptors[int](ctx, bq, querierCount[*BouncerQuery](), bq.inters) 207 } 208 209 // CountX is like Count, but panics if an error occurs. 210 func (bq *BouncerQuery) CountX(ctx context.Context) int { 211 count, err := bq.Count(ctx) 212 if err != nil { 213 panic(err) 214 } 215 return count 216 } 217 218 // Exist returns true if the query has elements in the graph. 219 func (bq *BouncerQuery) Exist(ctx context.Context) (bool, error) { 220 ctx = setContextOp(ctx, bq.ctx, "Exist") 221 switch _, err := bq.FirstID(ctx); { 222 case IsNotFound(err): 223 return false, nil 224 case err != nil: 225 return false, fmt.Errorf("ent: check existence: %w", err) 226 default: 227 return true, nil 228 } 229 } 230 231 // ExistX is like Exist, but panics if an error occurs. 232 func (bq *BouncerQuery) ExistX(ctx context.Context) bool { 233 exist, err := bq.Exist(ctx) 234 if err != nil { 235 panic(err) 236 } 237 return exist 238 } 239 240 // Clone returns a duplicate of the BouncerQuery builder, including all associated steps. It can be 241 // used to prepare common query builders and use them differently after the clone is made. 242 func (bq *BouncerQuery) Clone() *BouncerQuery { 243 if bq == nil { 244 return nil 245 } 246 return &BouncerQuery{ 247 config: bq.config, 248 ctx: bq.ctx.Clone(), 249 order: append([]bouncer.OrderOption{}, bq.order...), 250 inters: append([]Interceptor{}, bq.inters...), 251 predicates: append([]predicate.Bouncer{}, bq.predicates...), 252 // clone intermediate query. 253 sql: bq.sql.Clone(), 254 path: bq.path, 255 } 256 } 257 258 // GroupBy is used to group vertices by one or more fields/columns. 259 // It is often used with aggregate functions, like: count, max, mean, min, sum. 260 // 261 // Example: 262 // 263 // var v []struct { 264 // CreatedAt time.Time `json:"created_at"` 265 // Count int `json:"count,omitempty"` 266 // } 267 // 268 // client.Bouncer.Query(). 269 // GroupBy(bouncer.FieldCreatedAt). 270 // Aggregate(ent.Count()). 271 // Scan(ctx, &v) 272 func (bq *BouncerQuery) GroupBy(field string, fields ...string) *BouncerGroupBy { 273 bq.ctx.Fields = append([]string{field}, fields...) 274 grbuild := &BouncerGroupBy{build: bq} 275 grbuild.flds = &bq.ctx.Fields 276 grbuild.label = bouncer.Label 277 grbuild.scan = grbuild.Scan 278 return grbuild 279 } 280 281 // Select allows the selection one or more fields/columns for the given query, 282 // instead of selecting all fields in the entity. 283 // 284 // Example: 285 // 286 // var v []struct { 287 // CreatedAt time.Time `json:"created_at"` 288 // } 289 // 290 // client.Bouncer.Query(). 291 // Select(bouncer.FieldCreatedAt). 292 // Scan(ctx, &v) 293 func (bq *BouncerQuery) Select(fields ...string) *BouncerSelect { 294 bq.ctx.Fields = append(bq.ctx.Fields, fields...) 295 sbuild := &BouncerSelect{BouncerQuery: bq} 296 sbuild.label = bouncer.Label 297 sbuild.flds, sbuild.scan = &bq.ctx.Fields, sbuild.Scan 298 return sbuild 299 } 300 301 // Aggregate returns a BouncerSelect configured with the given aggregations. 302 func (bq *BouncerQuery) Aggregate(fns ...AggregateFunc) *BouncerSelect { 303 return bq.Select().Aggregate(fns...) 304 } 305 306 func (bq *BouncerQuery) prepareQuery(ctx context.Context) error { 307 for _, inter := range bq.inters { 308 if inter == nil { 309 return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)") 310 } 311 if trv, ok := inter.(Traverser); ok { 312 if err := trv.Traverse(ctx, bq); err != nil { 313 return err 314 } 315 } 316 } 317 for _, f := range bq.ctx.Fields { 318 if !bouncer.ValidColumn(f) { 319 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 320 } 321 } 322 if bq.path != nil { 323 prev, err := bq.path(ctx) 324 if err != nil { 325 return err 326 } 327 bq.sql = prev 328 } 329 return nil 330 } 331 332 func (bq *BouncerQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Bouncer, error) { 333 var ( 334 nodes = []*Bouncer{} 335 _spec = bq.querySpec() 336 ) 337 _spec.ScanValues = func(columns []string) ([]any, error) { 338 return (*Bouncer).scanValues(nil, columns) 339 } 340 _spec.Assign = func(columns []string, values []any) error { 341 node := &Bouncer{config: bq.config} 342 nodes = append(nodes, node) 343 return node.assignValues(columns, values) 344 } 345 for i := range hooks { 346 hooks[i](ctx, _spec) 347 } 348 if err := sqlgraph.QueryNodes(ctx, bq.driver, _spec); err != nil { 349 return nil, err 350 } 351 if len(nodes) == 0 { 352 return nodes, nil 353 } 354 return nodes, nil 355 } 356 357 func (bq *BouncerQuery) sqlCount(ctx context.Context) (int, error) { 358 _spec := bq.querySpec() 359 _spec.Node.Columns = bq.ctx.Fields 360 if len(bq.ctx.Fields) > 0 { 361 _spec.Unique = bq.ctx.Unique != nil && *bq.ctx.Unique 362 } 363 return sqlgraph.CountNodes(ctx, bq.driver, _spec) 364 } 365 366 func (bq *BouncerQuery) querySpec() *sqlgraph.QuerySpec { 367 _spec := sqlgraph.NewQuerySpec(bouncer.Table, bouncer.Columns, sqlgraph.NewFieldSpec(bouncer.FieldID, field.TypeInt)) 368 _spec.From = bq.sql 369 if unique := bq.ctx.Unique; unique != nil { 370 _spec.Unique = *unique 371 } else if bq.path != nil { 372 _spec.Unique = true 373 } 374 if fields := bq.ctx.Fields; len(fields) > 0 { 375 _spec.Node.Columns = make([]string, 0, len(fields)) 376 _spec.Node.Columns = append(_spec.Node.Columns, bouncer.FieldID) 377 for i := range fields { 378 if fields[i] != bouncer.FieldID { 379 _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) 380 } 381 } 382 } 383 if ps := bq.predicates; len(ps) > 0 { 384 _spec.Predicate = func(selector *sql.Selector) { 385 for i := range ps { 386 ps[i](selector) 387 } 388 } 389 } 390 if limit := bq.ctx.Limit; limit != nil { 391 _spec.Limit = *limit 392 } 393 if offset := bq.ctx.Offset; offset != nil { 394 _spec.Offset = *offset 395 } 396 if ps := bq.order; len(ps) > 0 { 397 _spec.Order = func(selector *sql.Selector) { 398 for i := range ps { 399 ps[i](selector) 400 } 401 } 402 } 403 return _spec 404 } 405 406 func (bq *BouncerQuery) sqlQuery(ctx context.Context) *sql.Selector { 407 builder := sql.Dialect(bq.driver.Dialect()) 408 t1 := builder.Table(bouncer.Table) 409 columns := bq.ctx.Fields 410 if len(columns) == 0 { 411 columns = bouncer.Columns 412 } 413 selector := builder.Select(t1.Columns(columns...)...).From(t1) 414 if bq.sql != nil { 415 selector = bq.sql 416 selector.Select(selector.Columns(columns...)...) 417 } 418 if bq.ctx.Unique != nil && *bq.ctx.Unique { 419 selector.Distinct() 420 } 421 for _, p := range bq.predicates { 422 p(selector) 423 } 424 for _, p := range bq.order { 425 p(selector) 426 } 427 if offset := bq.ctx.Offset; offset != nil { 428 // limit is mandatory for offset clause. We start 429 // with default value, and override it below if needed. 430 selector.Offset(*offset).Limit(math.MaxInt32) 431 } 432 if limit := bq.ctx.Limit; limit != nil { 433 selector.Limit(*limit) 434 } 435 return selector 436 } 437 438 // BouncerGroupBy is the group-by builder for Bouncer entities. 439 type BouncerGroupBy struct { 440 selector 441 build *BouncerQuery 442 } 443 444 // Aggregate adds the given aggregation functions to the group-by query. 445 func (bgb *BouncerGroupBy) Aggregate(fns ...AggregateFunc) *BouncerGroupBy { 446 bgb.fns = append(bgb.fns, fns...) 447 return bgb 448 } 449 450 // Scan applies the selector query and scans the result into the given value. 451 func (bgb *BouncerGroupBy) Scan(ctx context.Context, v any) error { 452 ctx = setContextOp(ctx, bgb.build.ctx, "GroupBy") 453 if err := bgb.build.prepareQuery(ctx); err != nil { 454 return err 455 } 456 return scanWithInterceptors[*BouncerQuery, *BouncerGroupBy](ctx, bgb.build, bgb, bgb.build.inters, v) 457 } 458 459 func (bgb *BouncerGroupBy) sqlScan(ctx context.Context, root *BouncerQuery, v any) error { 460 selector := root.sqlQuery(ctx).Select() 461 aggregation := make([]string, 0, len(bgb.fns)) 462 for _, fn := range bgb.fns { 463 aggregation = append(aggregation, fn(selector)) 464 } 465 if len(selector.SelectedColumns()) == 0 { 466 columns := make([]string, 0, len(*bgb.flds)+len(bgb.fns)) 467 for _, f := range *bgb.flds { 468 columns = append(columns, selector.C(f)) 469 } 470 columns = append(columns, aggregation...) 471 selector.Select(columns...) 472 } 473 selector.GroupBy(selector.Columns(*bgb.flds...)...) 474 if err := selector.Err(); err != nil { 475 return err 476 } 477 rows := &sql.Rows{} 478 query, args := selector.Query() 479 if err := bgb.build.driver.Query(ctx, query, args, rows); err != nil { 480 return err 481 } 482 defer rows.Close() 483 return sql.ScanSlice(rows, v) 484 } 485 486 // BouncerSelect is the builder for selecting fields of Bouncer entities. 487 type BouncerSelect struct { 488 *BouncerQuery 489 selector 490 } 491 492 // Aggregate adds the given aggregation functions to the selector query. 493 func (bs *BouncerSelect) Aggregate(fns ...AggregateFunc) *BouncerSelect { 494 bs.fns = append(bs.fns, fns...) 495 return bs 496 } 497 498 // Scan applies the selector query and scans the result into the given value. 499 func (bs *BouncerSelect) Scan(ctx context.Context, v any) error { 500 ctx = setContextOp(ctx, bs.ctx, "Select") 501 if err := bs.prepareQuery(ctx); err != nil { 502 return err 503 } 504 return scanWithInterceptors[*BouncerQuery, *BouncerSelect](ctx, bs.BouncerQuery, bs, bs.inters, v) 505 } 506 507 func (bs *BouncerSelect) sqlScan(ctx context.Context, root *BouncerQuery, v any) error { 508 selector := root.sqlQuery(ctx) 509 aggregation := make([]string, 0, len(bs.fns)) 510 for _, fn := range bs.fns { 511 aggregation = append(aggregation, fn(selector)) 512 } 513 switch n := len(*bs.selector.flds); { 514 case n == 0 && len(aggregation) > 0: 515 selector.Select(aggregation...) 516 case n != 0 && len(aggregation) > 0: 517 selector.AppendSelect(aggregation...) 518 } 519 rows := &sql.Rows{} 520 query, args := selector.Query() 521 if err := bs.driver.Query(ctx, query, args, rows); err != nil { 522 return err 523 } 524 defer rows.Close() 525 return sql.ScanSlice(rows, v) 526 }