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