github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/alert_query.go (about) 1 // Code generated by ent, DO NOT EDIT. 2 3 package ent 4 5 import ( 6 "context" 7 "database/sql/driver" 8 "fmt" 9 "math" 10 11 "entgo.io/ent/dialect/sql" 12 "entgo.io/ent/dialect/sql/sqlgraph" 13 "entgo.io/ent/schema/field" 14 "github.com/crowdsecurity/crowdsec/pkg/database/ent/alert" 15 "github.com/crowdsecurity/crowdsec/pkg/database/ent/decision" 16 "github.com/crowdsecurity/crowdsec/pkg/database/ent/event" 17 "github.com/crowdsecurity/crowdsec/pkg/database/ent/machine" 18 "github.com/crowdsecurity/crowdsec/pkg/database/ent/meta" 19 "github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate" 20 ) 21 22 // AlertQuery is the builder for querying Alert entities. 23 type AlertQuery struct { 24 config 25 ctx *QueryContext 26 order []alert.OrderOption 27 inters []Interceptor 28 predicates []predicate.Alert 29 withOwner *MachineQuery 30 withDecisions *DecisionQuery 31 withEvents *EventQuery 32 withMetas *MetaQuery 33 withFKs bool 34 // intermediate query (i.e. traversal path). 35 sql *sql.Selector 36 path func(context.Context) (*sql.Selector, error) 37 } 38 39 // Where adds a new predicate for the AlertQuery builder. 40 func (aq *AlertQuery) Where(ps ...predicate.Alert) *AlertQuery { 41 aq.predicates = append(aq.predicates, ps...) 42 return aq 43 } 44 45 // Limit the number of records to be returned by this query. 46 func (aq *AlertQuery) Limit(limit int) *AlertQuery { 47 aq.ctx.Limit = &limit 48 return aq 49 } 50 51 // Offset to start from. 52 func (aq *AlertQuery) Offset(offset int) *AlertQuery { 53 aq.ctx.Offset = &offset 54 return aq 55 } 56 57 // Unique configures the query builder to filter duplicate records on query. 58 // By default, unique is set to true, and can be disabled using this method. 59 func (aq *AlertQuery) Unique(unique bool) *AlertQuery { 60 aq.ctx.Unique = &unique 61 return aq 62 } 63 64 // Order specifies how the records should be ordered. 65 func (aq *AlertQuery) Order(o ...alert.OrderOption) *AlertQuery { 66 aq.order = append(aq.order, o...) 67 return aq 68 } 69 70 // QueryOwner chains the current query on the "owner" edge. 71 func (aq *AlertQuery) QueryOwner() *MachineQuery { 72 query := (&MachineClient{config: aq.config}).Query() 73 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 74 if err := aq.prepareQuery(ctx); err != nil { 75 return nil, err 76 } 77 selector := aq.sqlQuery(ctx) 78 if err := selector.Err(); err != nil { 79 return nil, err 80 } 81 step := sqlgraph.NewStep( 82 sqlgraph.From(alert.Table, alert.FieldID, selector), 83 sqlgraph.To(machine.Table, machine.FieldID), 84 sqlgraph.Edge(sqlgraph.M2O, true, alert.OwnerTable, alert.OwnerColumn), 85 ) 86 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 87 return fromU, nil 88 } 89 return query 90 } 91 92 // QueryDecisions chains the current query on the "decisions" edge. 93 func (aq *AlertQuery) QueryDecisions() *DecisionQuery { 94 query := (&DecisionClient{config: aq.config}).Query() 95 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 96 if err := aq.prepareQuery(ctx); err != nil { 97 return nil, err 98 } 99 selector := aq.sqlQuery(ctx) 100 if err := selector.Err(); err != nil { 101 return nil, err 102 } 103 step := sqlgraph.NewStep( 104 sqlgraph.From(alert.Table, alert.FieldID, selector), 105 sqlgraph.To(decision.Table, decision.FieldID), 106 sqlgraph.Edge(sqlgraph.O2M, false, alert.DecisionsTable, alert.DecisionsColumn), 107 ) 108 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 109 return fromU, nil 110 } 111 return query 112 } 113 114 // QueryEvents chains the current query on the "events" edge. 115 func (aq *AlertQuery) QueryEvents() *EventQuery { 116 query := (&EventClient{config: aq.config}).Query() 117 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 118 if err := aq.prepareQuery(ctx); err != nil { 119 return nil, err 120 } 121 selector := aq.sqlQuery(ctx) 122 if err := selector.Err(); err != nil { 123 return nil, err 124 } 125 step := sqlgraph.NewStep( 126 sqlgraph.From(alert.Table, alert.FieldID, selector), 127 sqlgraph.To(event.Table, event.FieldID), 128 sqlgraph.Edge(sqlgraph.O2M, false, alert.EventsTable, alert.EventsColumn), 129 ) 130 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 131 return fromU, nil 132 } 133 return query 134 } 135 136 // QueryMetas chains the current query on the "metas" edge. 137 func (aq *AlertQuery) QueryMetas() *MetaQuery { 138 query := (&MetaClient{config: aq.config}).Query() 139 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 140 if err := aq.prepareQuery(ctx); err != nil { 141 return nil, err 142 } 143 selector := aq.sqlQuery(ctx) 144 if err := selector.Err(); err != nil { 145 return nil, err 146 } 147 step := sqlgraph.NewStep( 148 sqlgraph.From(alert.Table, alert.FieldID, selector), 149 sqlgraph.To(meta.Table, meta.FieldID), 150 sqlgraph.Edge(sqlgraph.O2M, false, alert.MetasTable, alert.MetasColumn), 151 ) 152 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 153 return fromU, nil 154 } 155 return query 156 } 157 158 // First returns the first Alert entity from the query. 159 // Returns a *NotFoundError when no Alert was found. 160 func (aq *AlertQuery) First(ctx context.Context) (*Alert, error) { 161 nodes, err := aq.Limit(1).All(setContextOp(ctx, aq.ctx, "First")) 162 if err != nil { 163 return nil, err 164 } 165 if len(nodes) == 0 { 166 return nil, &NotFoundError{alert.Label} 167 } 168 return nodes[0], nil 169 } 170 171 // FirstX is like First, but panics if an error occurs. 172 func (aq *AlertQuery) FirstX(ctx context.Context) *Alert { 173 node, err := aq.First(ctx) 174 if err != nil && !IsNotFound(err) { 175 panic(err) 176 } 177 return node 178 } 179 180 // FirstID returns the first Alert ID from the query. 181 // Returns a *NotFoundError when no Alert ID was found. 182 func (aq *AlertQuery) FirstID(ctx context.Context) (id int, err error) { 183 var ids []int 184 if ids, err = aq.Limit(1).IDs(setContextOp(ctx, aq.ctx, "FirstID")); err != nil { 185 return 186 } 187 if len(ids) == 0 { 188 err = &NotFoundError{alert.Label} 189 return 190 } 191 return ids[0], nil 192 } 193 194 // FirstIDX is like FirstID, but panics if an error occurs. 195 func (aq *AlertQuery) FirstIDX(ctx context.Context) int { 196 id, err := aq.FirstID(ctx) 197 if err != nil && !IsNotFound(err) { 198 panic(err) 199 } 200 return id 201 } 202 203 // Only returns a single Alert entity found by the query, ensuring it only returns one. 204 // Returns a *NotSingularError when more than one Alert entity is found. 205 // Returns a *NotFoundError when no Alert entities are found. 206 func (aq *AlertQuery) Only(ctx context.Context) (*Alert, error) { 207 nodes, err := aq.Limit(2).All(setContextOp(ctx, aq.ctx, "Only")) 208 if err != nil { 209 return nil, err 210 } 211 switch len(nodes) { 212 case 1: 213 return nodes[0], nil 214 case 0: 215 return nil, &NotFoundError{alert.Label} 216 default: 217 return nil, &NotSingularError{alert.Label} 218 } 219 } 220 221 // OnlyX is like Only, but panics if an error occurs. 222 func (aq *AlertQuery) OnlyX(ctx context.Context) *Alert { 223 node, err := aq.Only(ctx) 224 if err != nil { 225 panic(err) 226 } 227 return node 228 } 229 230 // OnlyID is like Only, but returns the only Alert ID in the query. 231 // Returns a *NotSingularError when more than one Alert ID is found. 232 // Returns a *NotFoundError when no entities are found. 233 func (aq *AlertQuery) OnlyID(ctx context.Context) (id int, err error) { 234 var ids []int 235 if ids, err = aq.Limit(2).IDs(setContextOp(ctx, aq.ctx, "OnlyID")); err != nil { 236 return 237 } 238 switch len(ids) { 239 case 1: 240 id = ids[0] 241 case 0: 242 err = &NotFoundError{alert.Label} 243 default: 244 err = &NotSingularError{alert.Label} 245 } 246 return 247 } 248 249 // OnlyIDX is like OnlyID, but panics if an error occurs. 250 func (aq *AlertQuery) OnlyIDX(ctx context.Context) int { 251 id, err := aq.OnlyID(ctx) 252 if err != nil { 253 panic(err) 254 } 255 return id 256 } 257 258 // All executes the query and returns a list of Alerts. 259 func (aq *AlertQuery) All(ctx context.Context) ([]*Alert, error) { 260 ctx = setContextOp(ctx, aq.ctx, "All") 261 if err := aq.prepareQuery(ctx); err != nil { 262 return nil, err 263 } 264 qr := querierAll[[]*Alert, *AlertQuery]() 265 return withInterceptors[[]*Alert](ctx, aq, qr, aq.inters) 266 } 267 268 // AllX is like All, but panics if an error occurs. 269 func (aq *AlertQuery) AllX(ctx context.Context) []*Alert { 270 nodes, err := aq.All(ctx) 271 if err != nil { 272 panic(err) 273 } 274 return nodes 275 } 276 277 // IDs executes the query and returns a list of Alert IDs. 278 func (aq *AlertQuery) IDs(ctx context.Context) (ids []int, err error) { 279 if aq.ctx.Unique == nil && aq.path != nil { 280 aq.Unique(true) 281 } 282 ctx = setContextOp(ctx, aq.ctx, "IDs") 283 if err = aq.Select(alert.FieldID).Scan(ctx, &ids); err != nil { 284 return nil, err 285 } 286 return ids, nil 287 } 288 289 // IDsX is like IDs, but panics if an error occurs. 290 func (aq *AlertQuery) IDsX(ctx context.Context) []int { 291 ids, err := aq.IDs(ctx) 292 if err != nil { 293 panic(err) 294 } 295 return ids 296 } 297 298 // Count returns the count of the given query. 299 func (aq *AlertQuery) Count(ctx context.Context) (int, error) { 300 ctx = setContextOp(ctx, aq.ctx, "Count") 301 if err := aq.prepareQuery(ctx); err != nil { 302 return 0, err 303 } 304 return withInterceptors[int](ctx, aq, querierCount[*AlertQuery](), aq.inters) 305 } 306 307 // CountX is like Count, but panics if an error occurs. 308 func (aq *AlertQuery) CountX(ctx context.Context) int { 309 count, err := aq.Count(ctx) 310 if err != nil { 311 panic(err) 312 } 313 return count 314 } 315 316 // Exist returns true if the query has elements in the graph. 317 func (aq *AlertQuery) Exist(ctx context.Context) (bool, error) { 318 ctx = setContextOp(ctx, aq.ctx, "Exist") 319 switch _, err := aq.FirstID(ctx); { 320 case IsNotFound(err): 321 return false, nil 322 case err != nil: 323 return false, fmt.Errorf("ent: check existence: %w", err) 324 default: 325 return true, nil 326 } 327 } 328 329 // ExistX is like Exist, but panics if an error occurs. 330 func (aq *AlertQuery) ExistX(ctx context.Context) bool { 331 exist, err := aq.Exist(ctx) 332 if err != nil { 333 panic(err) 334 } 335 return exist 336 } 337 338 // Clone returns a duplicate of the AlertQuery builder, including all associated steps. It can be 339 // used to prepare common query builders and use them differently after the clone is made. 340 func (aq *AlertQuery) Clone() *AlertQuery { 341 if aq == nil { 342 return nil 343 } 344 return &AlertQuery{ 345 config: aq.config, 346 ctx: aq.ctx.Clone(), 347 order: append([]alert.OrderOption{}, aq.order...), 348 inters: append([]Interceptor{}, aq.inters...), 349 predicates: append([]predicate.Alert{}, aq.predicates...), 350 withOwner: aq.withOwner.Clone(), 351 withDecisions: aq.withDecisions.Clone(), 352 withEvents: aq.withEvents.Clone(), 353 withMetas: aq.withMetas.Clone(), 354 // clone intermediate query. 355 sql: aq.sql.Clone(), 356 path: aq.path, 357 } 358 } 359 360 // WithOwner tells the query-builder to eager-load the nodes that are connected to 361 // the "owner" edge. The optional arguments are used to configure the query builder of the edge. 362 func (aq *AlertQuery) WithOwner(opts ...func(*MachineQuery)) *AlertQuery { 363 query := (&MachineClient{config: aq.config}).Query() 364 for _, opt := range opts { 365 opt(query) 366 } 367 aq.withOwner = query 368 return aq 369 } 370 371 // WithDecisions tells the query-builder to eager-load the nodes that are connected to 372 // the "decisions" edge. The optional arguments are used to configure the query builder of the edge. 373 func (aq *AlertQuery) WithDecisions(opts ...func(*DecisionQuery)) *AlertQuery { 374 query := (&DecisionClient{config: aq.config}).Query() 375 for _, opt := range opts { 376 opt(query) 377 } 378 aq.withDecisions = query 379 return aq 380 } 381 382 // WithEvents tells the query-builder to eager-load the nodes that are connected to 383 // the "events" edge. The optional arguments are used to configure the query builder of the edge. 384 func (aq *AlertQuery) WithEvents(opts ...func(*EventQuery)) *AlertQuery { 385 query := (&EventClient{config: aq.config}).Query() 386 for _, opt := range opts { 387 opt(query) 388 } 389 aq.withEvents = query 390 return aq 391 } 392 393 // WithMetas tells the query-builder to eager-load the nodes that are connected to 394 // the "metas" edge. The optional arguments are used to configure the query builder of the edge. 395 func (aq *AlertQuery) WithMetas(opts ...func(*MetaQuery)) *AlertQuery { 396 query := (&MetaClient{config: aq.config}).Query() 397 for _, opt := range opts { 398 opt(query) 399 } 400 aq.withMetas = query 401 return aq 402 } 403 404 // GroupBy is used to group vertices by one or more fields/columns. 405 // It is often used with aggregate functions, like: count, max, mean, min, sum. 406 // 407 // Example: 408 // 409 // var v []struct { 410 // CreatedAt time.Time `json:"created_at,omitempty"` 411 // Count int `json:"count,omitempty"` 412 // } 413 // 414 // client.Alert.Query(). 415 // GroupBy(alert.FieldCreatedAt). 416 // Aggregate(ent.Count()). 417 // Scan(ctx, &v) 418 func (aq *AlertQuery) GroupBy(field string, fields ...string) *AlertGroupBy { 419 aq.ctx.Fields = append([]string{field}, fields...) 420 grbuild := &AlertGroupBy{build: aq} 421 grbuild.flds = &aq.ctx.Fields 422 grbuild.label = alert.Label 423 grbuild.scan = grbuild.Scan 424 return grbuild 425 } 426 427 // Select allows the selection one or more fields/columns for the given query, 428 // instead of selecting all fields in the entity. 429 // 430 // Example: 431 // 432 // var v []struct { 433 // CreatedAt time.Time `json:"created_at,omitempty"` 434 // } 435 // 436 // client.Alert.Query(). 437 // Select(alert.FieldCreatedAt). 438 // Scan(ctx, &v) 439 func (aq *AlertQuery) Select(fields ...string) *AlertSelect { 440 aq.ctx.Fields = append(aq.ctx.Fields, fields...) 441 sbuild := &AlertSelect{AlertQuery: aq} 442 sbuild.label = alert.Label 443 sbuild.flds, sbuild.scan = &aq.ctx.Fields, sbuild.Scan 444 return sbuild 445 } 446 447 // Aggregate returns a AlertSelect configured with the given aggregations. 448 func (aq *AlertQuery) Aggregate(fns ...AggregateFunc) *AlertSelect { 449 return aq.Select().Aggregate(fns...) 450 } 451 452 func (aq *AlertQuery) prepareQuery(ctx context.Context) error { 453 for _, inter := range aq.inters { 454 if inter == nil { 455 return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)") 456 } 457 if trv, ok := inter.(Traverser); ok { 458 if err := trv.Traverse(ctx, aq); err != nil { 459 return err 460 } 461 } 462 } 463 for _, f := range aq.ctx.Fields { 464 if !alert.ValidColumn(f) { 465 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 466 } 467 } 468 if aq.path != nil { 469 prev, err := aq.path(ctx) 470 if err != nil { 471 return err 472 } 473 aq.sql = prev 474 } 475 return nil 476 } 477 478 func (aq *AlertQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Alert, error) { 479 var ( 480 nodes = []*Alert{} 481 withFKs = aq.withFKs 482 _spec = aq.querySpec() 483 loadedTypes = [4]bool{ 484 aq.withOwner != nil, 485 aq.withDecisions != nil, 486 aq.withEvents != nil, 487 aq.withMetas != nil, 488 } 489 ) 490 if aq.withOwner != nil { 491 withFKs = true 492 } 493 if withFKs { 494 _spec.Node.Columns = append(_spec.Node.Columns, alert.ForeignKeys...) 495 } 496 _spec.ScanValues = func(columns []string) ([]any, error) { 497 return (*Alert).scanValues(nil, columns) 498 } 499 _spec.Assign = func(columns []string, values []any) error { 500 node := &Alert{config: aq.config} 501 nodes = append(nodes, node) 502 node.Edges.loadedTypes = loadedTypes 503 return node.assignValues(columns, values) 504 } 505 for i := range hooks { 506 hooks[i](ctx, _spec) 507 } 508 if err := sqlgraph.QueryNodes(ctx, aq.driver, _spec); err != nil { 509 return nil, err 510 } 511 if len(nodes) == 0 { 512 return nodes, nil 513 } 514 if query := aq.withOwner; query != nil { 515 if err := aq.loadOwner(ctx, query, nodes, nil, 516 func(n *Alert, e *Machine) { n.Edges.Owner = e }); err != nil { 517 return nil, err 518 } 519 } 520 if query := aq.withDecisions; query != nil { 521 if err := aq.loadDecisions(ctx, query, nodes, 522 func(n *Alert) { n.Edges.Decisions = []*Decision{} }, 523 func(n *Alert, e *Decision) { n.Edges.Decisions = append(n.Edges.Decisions, e) }); err != nil { 524 return nil, err 525 } 526 } 527 if query := aq.withEvents; query != nil { 528 if err := aq.loadEvents(ctx, query, nodes, 529 func(n *Alert) { n.Edges.Events = []*Event{} }, 530 func(n *Alert, e *Event) { n.Edges.Events = append(n.Edges.Events, e) }); err != nil { 531 return nil, err 532 } 533 } 534 if query := aq.withMetas; query != nil { 535 if err := aq.loadMetas(ctx, query, nodes, 536 func(n *Alert) { n.Edges.Metas = []*Meta{} }, 537 func(n *Alert, e *Meta) { n.Edges.Metas = append(n.Edges.Metas, e) }); err != nil { 538 return nil, err 539 } 540 } 541 return nodes, nil 542 } 543 544 func (aq *AlertQuery) loadOwner(ctx context.Context, query *MachineQuery, nodes []*Alert, init func(*Alert), assign func(*Alert, *Machine)) error { 545 ids := make([]int, 0, len(nodes)) 546 nodeids := make(map[int][]*Alert) 547 for i := range nodes { 548 if nodes[i].machine_alerts == nil { 549 continue 550 } 551 fk := *nodes[i].machine_alerts 552 if _, ok := nodeids[fk]; !ok { 553 ids = append(ids, fk) 554 } 555 nodeids[fk] = append(nodeids[fk], nodes[i]) 556 } 557 if len(ids) == 0 { 558 return nil 559 } 560 query.Where(machine.IDIn(ids...)) 561 neighbors, err := query.All(ctx) 562 if err != nil { 563 return err 564 } 565 for _, n := range neighbors { 566 nodes, ok := nodeids[n.ID] 567 if !ok { 568 return fmt.Errorf(`unexpected foreign-key "machine_alerts" returned %v`, n.ID) 569 } 570 for i := range nodes { 571 assign(nodes[i], n) 572 } 573 } 574 return nil 575 } 576 func (aq *AlertQuery) loadDecisions(ctx context.Context, query *DecisionQuery, nodes []*Alert, init func(*Alert), assign func(*Alert, *Decision)) error { 577 fks := make([]driver.Value, 0, len(nodes)) 578 nodeids := make(map[int]*Alert) 579 for i := range nodes { 580 fks = append(fks, nodes[i].ID) 581 nodeids[nodes[i].ID] = nodes[i] 582 if init != nil { 583 init(nodes[i]) 584 } 585 } 586 if len(query.ctx.Fields) > 0 { 587 query.ctx.AppendFieldOnce(decision.FieldAlertDecisions) 588 } 589 query.Where(predicate.Decision(func(s *sql.Selector) { 590 s.Where(sql.InValues(s.C(alert.DecisionsColumn), fks...)) 591 })) 592 neighbors, err := query.All(ctx) 593 if err != nil { 594 return err 595 } 596 for _, n := range neighbors { 597 fk := n.AlertDecisions 598 node, ok := nodeids[fk] 599 if !ok { 600 return fmt.Errorf(`unexpected referenced foreign-key "alert_decisions" returned %v for node %v`, fk, n.ID) 601 } 602 assign(node, n) 603 } 604 return nil 605 } 606 func (aq *AlertQuery) loadEvents(ctx context.Context, query *EventQuery, nodes []*Alert, init func(*Alert), assign func(*Alert, *Event)) error { 607 fks := make([]driver.Value, 0, len(nodes)) 608 nodeids := make(map[int]*Alert) 609 for i := range nodes { 610 fks = append(fks, nodes[i].ID) 611 nodeids[nodes[i].ID] = nodes[i] 612 if init != nil { 613 init(nodes[i]) 614 } 615 } 616 if len(query.ctx.Fields) > 0 { 617 query.ctx.AppendFieldOnce(event.FieldAlertEvents) 618 } 619 query.Where(predicate.Event(func(s *sql.Selector) { 620 s.Where(sql.InValues(s.C(alert.EventsColumn), fks...)) 621 })) 622 neighbors, err := query.All(ctx) 623 if err != nil { 624 return err 625 } 626 for _, n := range neighbors { 627 fk := n.AlertEvents 628 node, ok := nodeids[fk] 629 if !ok { 630 return fmt.Errorf(`unexpected referenced foreign-key "alert_events" returned %v for node %v`, fk, n.ID) 631 } 632 assign(node, n) 633 } 634 return nil 635 } 636 func (aq *AlertQuery) loadMetas(ctx context.Context, query *MetaQuery, nodes []*Alert, init func(*Alert), assign func(*Alert, *Meta)) error { 637 fks := make([]driver.Value, 0, len(nodes)) 638 nodeids := make(map[int]*Alert) 639 for i := range nodes { 640 fks = append(fks, nodes[i].ID) 641 nodeids[nodes[i].ID] = nodes[i] 642 if init != nil { 643 init(nodes[i]) 644 } 645 } 646 if len(query.ctx.Fields) > 0 { 647 query.ctx.AppendFieldOnce(meta.FieldAlertMetas) 648 } 649 query.Where(predicate.Meta(func(s *sql.Selector) { 650 s.Where(sql.InValues(s.C(alert.MetasColumn), fks...)) 651 })) 652 neighbors, err := query.All(ctx) 653 if err != nil { 654 return err 655 } 656 for _, n := range neighbors { 657 fk := n.AlertMetas 658 node, ok := nodeids[fk] 659 if !ok { 660 return fmt.Errorf(`unexpected referenced foreign-key "alert_metas" returned %v for node %v`, fk, n.ID) 661 } 662 assign(node, n) 663 } 664 return nil 665 } 666 667 func (aq *AlertQuery) sqlCount(ctx context.Context) (int, error) { 668 _spec := aq.querySpec() 669 _spec.Node.Columns = aq.ctx.Fields 670 if len(aq.ctx.Fields) > 0 { 671 _spec.Unique = aq.ctx.Unique != nil && *aq.ctx.Unique 672 } 673 return sqlgraph.CountNodes(ctx, aq.driver, _spec) 674 } 675 676 func (aq *AlertQuery) querySpec() *sqlgraph.QuerySpec { 677 _spec := sqlgraph.NewQuerySpec(alert.Table, alert.Columns, sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt)) 678 _spec.From = aq.sql 679 if unique := aq.ctx.Unique; unique != nil { 680 _spec.Unique = *unique 681 } else if aq.path != nil { 682 _spec.Unique = true 683 } 684 if fields := aq.ctx.Fields; len(fields) > 0 { 685 _spec.Node.Columns = make([]string, 0, len(fields)) 686 _spec.Node.Columns = append(_spec.Node.Columns, alert.FieldID) 687 for i := range fields { 688 if fields[i] != alert.FieldID { 689 _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) 690 } 691 } 692 } 693 if ps := aq.predicates; len(ps) > 0 { 694 _spec.Predicate = func(selector *sql.Selector) { 695 for i := range ps { 696 ps[i](selector) 697 } 698 } 699 } 700 if limit := aq.ctx.Limit; limit != nil { 701 _spec.Limit = *limit 702 } 703 if offset := aq.ctx.Offset; offset != nil { 704 _spec.Offset = *offset 705 } 706 if ps := aq.order; len(ps) > 0 { 707 _spec.Order = func(selector *sql.Selector) { 708 for i := range ps { 709 ps[i](selector) 710 } 711 } 712 } 713 return _spec 714 } 715 716 func (aq *AlertQuery) sqlQuery(ctx context.Context) *sql.Selector { 717 builder := sql.Dialect(aq.driver.Dialect()) 718 t1 := builder.Table(alert.Table) 719 columns := aq.ctx.Fields 720 if len(columns) == 0 { 721 columns = alert.Columns 722 } 723 selector := builder.Select(t1.Columns(columns...)...).From(t1) 724 if aq.sql != nil { 725 selector = aq.sql 726 selector.Select(selector.Columns(columns...)...) 727 } 728 if aq.ctx.Unique != nil && *aq.ctx.Unique { 729 selector.Distinct() 730 } 731 for _, p := range aq.predicates { 732 p(selector) 733 } 734 for _, p := range aq.order { 735 p(selector) 736 } 737 if offset := aq.ctx.Offset; offset != nil { 738 // limit is mandatory for offset clause. We start 739 // with default value, and override it below if needed. 740 selector.Offset(*offset).Limit(math.MaxInt32) 741 } 742 if limit := aq.ctx.Limit; limit != nil { 743 selector.Limit(*limit) 744 } 745 return selector 746 } 747 748 // AlertGroupBy is the group-by builder for Alert entities. 749 type AlertGroupBy struct { 750 selector 751 build *AlertQuery 752 } 753 754 // Aggregate adds the given aggregation functions to the group-by query. 755 func (agb *AlertGroupBy) Aggregate(fns ...AggregateFunc) *AlertGroupBy { 756 agb.fns = append(agb.fns, fns...) 757 return agb 758 } 759 760 // Scan applies the selector query and scans the result into the given value. 761 func (agb *AlertGroupBy) Scan(ctx context.Context, v any) error { 762 ctx = setContextOp(ctx, agb.build.ctx, "GroupBy") 763 if err := agb.build.prepareQuery(ctx); err != nil { 764 return err 765 } 766 return scanWithInterceptors[*AlertQuery, *AlertGroupBy](ctx, agb.build, agb, agb.build.inters, v) 767 } 768 769 func (agb *AlertGroupBy) sqlScan(ctx context.Context, root *AlertQuery, v any) error { 770 selector := root.sqlQuery(ctx).Select() 771 aggregation := make([]string, 0, len(agb.fns)) 772 for _, fn := range agb.fns { 773 aggregation = append(aggregation, fn(selector)) 774 } 775 if len(selector.SelectedColumns()) == 0 { 776 columns := make([]string, 0, len(*agb.flds)+len(agb.fns)) 777 for _, f := range *agb.flds { 778 columns = append(columns, selector.C(f)) 779 } 780 columns = append(columns, aggregation...) 781 selector.Select(columns...) 782 } 783 selector.GroupBy(selector.Columns(*agb.flds...)...) 784 if err := selector.Err(); err != nil { 785 return err 786 } 787 rows := &sql.Rows{} 788 query, args := selector.Query() 789 if err := agb.build.driver.Query(ctx, query, args, rows); err != nil { 790 return err 791 } 792 defer rows.Close() 793 return sql.ScanSlice(rows, v) 794 } 795 796 // AlertSelect is the builder for selecting fields of Alert entities. 797 type AlertSelect struct { 798 *AlertQuery 799 selector 800 } 801 802 // Aggregate adds the given aggregation functions to the selector query. 803 func (as *AlertSelect) Aggregate(fns ...AggregateFunc) *AlertSelect { 804 as.fns = append(as.fns, fns...) 805 return as 806 } 807 808 // Scan applies the selector query and scans the result into the given value. 809 func (as *AlertSelect) Scan(ctx context.Context, v any) error { 810 ctx = setContextOp(ctx, as.ctx, "Select") 811 if err := as.prepareQuery(ctx); err != nil { 812 return err 813 } 814 return scanWithInterceptors[*AlertQuery, *AlertSelect](ctx, as.AlertQuery, as, as.inters, v) 815 } 816 817 func (as *AlertSelect) sqlScan(ctx context.Context, root *AlertQuery, v any) error { 818 selector := root.sqlQuery(ctx) 819 aggregation := make([]string, 0, len(as.fns)) 820 for _, fn := range as.fns { 821 aggregation = append(aggregation, fn(selector)) 822 } 823 switch n := len(*as.selector.flds); { 824 case n == 0 && len(aggregation) > 0: 825 selector.Select(aggregation...) 826 case n != 0 && len(aggregation) > 0: 827 selector.AppendSelect(aggregation...) 828 } 829 rows := &sql.Rows{} 830 query, args := selector.Query() 831 if err := as.driver.Query(ctx, query, args, rows); err != nil { 832 return err 833 } 834 defer rows.Close() 835 return sql.ScanSlice(rows, v) 836 }