bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/database/ent/alert_query.go (about) 1 // Code generated by entc, DO NOT EDIT. 2 3 package ent 4 5 import ( 6 "context" 7 "database/sql/driver" 8 "errors" 9 "fmt" 10 "math" 11 12 "entgo.io/ent/dialect/sql" 13 "entgo.io/ent/dialect/sql/sqlgraph" 14 "entgo.io/ent/schema/field" 15 "bitbucket.org/Aishee/synsec/pkg/database/ent/alert" 16 "bitbucket.org/Aishee/synsec/pkg/database/ent/decision" 17 "bitbucket.org/Aishee/synsec/pkg/database/ent/event" 18 "bitbucket.org/Aishee/synsec/pkg/database/ent/machine" 19 "bitbucket.org/Aishee/synsec/pkg/database/ent/meta" 20 "bitbucket.org/Aishee/synsec/pkg/database/ent/predicate" 21 ) 22 23 // AlertQuery is the builder for querying Alert entities. 24 type AlertQuery struct { 25 config 26 limit *int 27 offset *int 28 order []OrderFunc 29 fields []string 30 predicates []predicate.Alert 31 // eager-loading edges. 32 withOwner *MachineQuery 33 withDecisions *DecisionQuery 34 withEvents *EventQuery 35 withMetas *MetaQuery 36 withFKs bool 37 // intermediate query (i.e. traversal path). 38 sql *sql.Selector 39 path func(context.Context) (*sql.Selector, error) 40 } 41 42 // Where adds a new predicate for the AlertQuery builder. 43 func (aq *AlertQuery) Where(ps ...predicate.Alert) *AlertQuery { 44 aq.predicates = append(aq.predicates, ps...) 45 return aq 46 } 47 48 // Limit adds a limit step to the query. 49 func (aq *AlertQuery) Limit(limit int) *AlertQuery { 50 aq.limit = &limit 51 return aq 52 } 53 54 // Offset adds an offset step to the query. 55 func (aq *AlertQuery) Offset(offset int) *AlertQuery { 56 aq.offset = &offset 57 return aq 58 } 59 60 // Order adds an order step to the query. 61 func (aq *AlertQuery) Order(o ...OrderFunc) *AlertQuery { 62 aq.order = append(aq.order, o...) 63 return aq 64 } 65 66 // QueryOwner chains the current query on the "owner" edge. 67 func (aq *AlertQuery) QueryOwner() *MachineQuery { 68 query := &MachineQuery{config: aq.config} 69 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 70 if err := aq.prepareQuery(ctx); err != nil { 71 return nil, err 72 } 73 selector := aq.sqlQuery(ctx) 74 if err := selector.Err(); err != nil { 75 return nil, err 76 } 77 step := sqlgraph.NewStep( 78 sqlgraph.From(alert.Table, alert.FieldID, selector), 79 sqlgraph.To(machine.Table, machine.FieldID), 80 sqlgraph.Edge(sqlgraph.M2O, true, alert.OwnerTable, alert.OwnerColumn), 81 ) 82 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 83 return fromU, nil 84 } 85 return query 86 } 87 88 // QueryDecisions chains the current query on the "decisions" edge. 89 func (aq *AlertQuery) QueryDecisions() *DecisionQuery { 90 query := &DecisionQuery{config: aq.config} 91 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 92 if err := aq.prepareQuery(ctx); err != nil { 93 return nil, err 94 } 95 selector := aq.sqlQuery(ctx) 96 if err := selector.Err(); err != nil { 97 return nil, err 98 } 99 step := sqlgraph.NewStep( 100 sqlgraph.From(alert.Table, alert.FieldID, selector), 101 sqlgraph.To(decision.Table, decision.FieldID), 102 sqlgraph.Edge(sqlgraph.O2M, false, alert.DecisionsTable, alert.DecisionsColumn), 103 ) 104 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 105 return fromU, nil 106 } 107 return query 108 } 109 110 // QueryEvents chains the current query on the "events" edge. 111 func (aq *AlertQuery) QueryEvents() *EventQuery { 112 query := &EventQuery{config: aq.config} 113 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 114 if err := aq.prepareQuery(ctx); err != nil { 115 return nil, err 116 } 117 selector := aq.sqlQuery(ctx) 118 if err := selector.Err(); err != nil { 119 return nil, err 120 } 121 step := sqlgraph.NewStep( 122 sqlgraph.From(alert.Table, alert.FieldID, selector), 123 sqlgraph.To(event.Table, event.FieldID), 124 sqlgraph.Edge(sqlgraph.O2M, false, alert.EventsTable, alert.EventsColumn), 125 ) 126 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 127 return fromU, nil 128 } 129 return query 130 } 131 132 // QueryMetas chains the current query on the "metas" edge. 133 func (aq *AlertQuery) QueryMetas() *MetaQuery { 134 query := &MetaQuery{config: aq.config} 135 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 136 if err := aq.prepareQuery(ctx); err != nil { 137 return nil, err 138 } 139 selector := aq.sqlQuery(ctx) 140 if err := selector.Err(); err != nil { 141 return nil, err 142 } 143 step := sqlgraph.NewStep( 144 sqlgraph.From(alert.Table, alert.FieldID, selector), 145 sqlgraph.To(meta.Table, meta.FieldID), 146 sqlgraph.Edge(sqlgraph.O2M, false, alert.MetasTable, alert.MetasColumn), 147 ) 148 fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step) 149 return fromU, nil 150 } 151 return query 152 } 153 154 // First returns the first Alert entity from the query. 155 // Returns a *NotFoundError when no Alert was found. 156 func (aq *AlertQuery) First(ctx context.Context) (*Alert, error) { 157 nodes, err := aq.Limit(1).All(ctx) 158 if err != nil { 159 return nil, err 160 } 161 if len(nodes) == 0 { 162 return nil, &NotFoundError{alert.Label} 163 } 164 return nodes[0], nil 165 } 166 167 // FirstX is like First, but panics if an error occurs. 168 func (aq *AlertQuery) FirstX(ctx context.Context) *Alert { 169 node, err := aq.First(ctx) 170 if err != nil && !IsNotFound(err) { 171 panic(err) 172 } 173 return node 174 } 175 176 // FirstID returns the first Alert ID from the query. 177 // Returns a *NotFoundError when no Alert ID was found. 178 func (aq *AlertQuery) FirstID(ctx context.Context) (id int, err error) { 179 var ids []int 180 if ids, err = aq.Limit(1).IDs(ctx); err != nil { 181 return 182 } 183 if len(ids) == 0 { 184 err = &NotFoundError{alert.Label} 185 return 186 } 187 return ids[0], nil 188 } 189 190 // FirstIDX is like FirstID, but panics if an error occurs. 191 func (aq *AlertQuery) FirstIDX(ctx context.Context) int { 192 id, err := aq.FirstID(ctx) 193 if err != nil && !IsNotFound(err) { 194 panic(err) 195 } 196 return id 197 } 198 199 // Only returns a single Alert entity found by the query, ensuring it only returns one. 200 // Returns a *NotSingularError when exactly one Alert entity is not found. 201 // Returns a *NotFoundError when no Alert entities are found. 202 func (aq *AlertQuery) Only(ctx context.Context) (*Alert, error) { 203 nodes, err := aq.Limit(2).All(ctx) 204 if err != nil { 205 return nil, err 206 } 207 switch len(nodes) { 208 case 1: 209 return nodes[0], nil 210 case 0: 211 return nil, &NotFoundError{alert.Label} 212 default: 213 return nil, &NotSingularError{alert.Label} 214 } 215 } 216 217 // OnlyX is like Only, but panics if an error occurs. 218 func (aq *AlertQuery) OnlyX(ctx context.Context) *Alert { 219 node, err := aq.Only(ctx) 220 if err != nil { 221 panic(err) 222 } 223 return node 224 } 225 226 // OnlyID is like Only, but returns the only Alert ID in the query. 227 // Returns a *NotSingularError when exactly one Alert ID is not found. 228 // Returns a *NotFoundError when no entities are found. 229 func (aq *AlertQuery) OnlyID(ctx context.Context) (id int, err error) { 230 var ids []int 231 if ids, err = aq.Limit(2).IDs(ctx); err != nil { 232 return 233 } 234 switch len(ids) { 235 case 1: 236 id = ids[0] 237 case 0: 238 err = &NotFoundError{alert.Label} 239 default: 240 err = &NotSingularError{alert.Label} 241 } 242 return 243 } 244 245 // OnlyIDX is like OnlyID, but panics if an error occurs. 246 func (aq *AlertQuery) OnlyIDX(ctx context.Context) int { 247 id, err := aq.OnlyID(ctx) 248 if err != nil { 249 panic(err) 250 } 251 return id 252 } 253 254 // All executes the query and returns a list of Alerts. 255 func (aq *AlertQuery) All(ctx context.Context) ([]*Alert, error) { 256 if err := aq.prepareQuery(ctx); err != nil { 257 return nil, err 258 } 259 return aq.sqlAll(ctx) 260 } 261 262 // AllX is like All, but panics if an error occurs. 263 func (aq *AlertQuery) AllX(ctx context.Context) []*Alert { 264 nodes, err := aq.All(ctx) 265 if err != nil { 266 panic(err) 267 } 268 return nodes 269 } 270 271 // IDs executes the query and returns a list of Alert IDs. 272 func (aq *AlertQuery) IDs(ctx context.Context) ([]int, error) { 273 var ids []int 274 if err := aq.Select(alert.FieldID).Scan(ctx, &ids); err != nil { 275 return nil, err 276 } 277 return ids, nil 278 } 279 280 // IDsX is like IDs, but panics if an error occurs. 281 func (aq *AlertQuery) IDsX(ctx context.Context) []int { 282 ids, err := aq.IDs(ctx) 283 if err != nil { 284 panic(err) 285 } 286 return ids 287 } 288 289 // Count returns the count of the given query. 290 func (aq *AlertQuery) Count(ctx context.Context) (int, error) { 291 if err := aq.prepareQuery(ctx); err != nil { 292 return 0, err 293 } 294 return aq.sqlCount(ctx) 295 } 296 297 // CountX is like Count, but panics if an error occurs. 298 func (aq *AlertQuery) CountX(ctx context.Context) int { 299 count, err := aq.Count(ctx) 300 if err != nil { 301 panic(err) 302 } 303 return count 304 } 305 306 // Exist returns true if the query has elements in the graph. 307 func (aq *AlertQuery) Exist(ctx context.Context) (bool, error) { 308 if err := aq.prepareQuery(ctx); err != nil { 309 return false, err 310 } 311 return aq.sqlExist(ctx) 312 } 313 314 // ExistX is like Exist, but panics if an error occurs. 315 func (aq *AlertQuery) ExistX(ctx context.Context) bool { 316 exist, err := aq.Exist(ctx) 317 if err != nil { 318 panic(err) 319 } 320 return exist 321 } 322 323 // Clone returns a duplicate of the AlertQuery builder, including all associated steps. It can be 324 // used to prepare common query builders and use them differently after the clone is made. 325 func (aq *AlertQuery) Clone() *AlertQuery { 326 if aq == nil { 327 return nil 328 } 329 return &AlertQuery{ 330 config: aq.config, 331 limit: aq.limit, 332 offset: aq.offset, 333 order: append([]OrderFunc{}, aq.order...), 334 predicates: append([]predicate.Alert{}, aq.predicates...), 335 withOwner: aq.withOwner.Clone(), 336 withDecisions: aq.withDecisions.Clone(), 337 withEvents: aq.withEvents.Clone(), 338 withMetas: aq.withMetas.Clone(), 339 // clone intermediate query. 340 sql: aq.sql.Clone(), 341 path: aq.path, 342 } 343 } 344 345 // WithOwner tells the query-builder to eager-load the nodes that are connected to 346 // the "owner" edge. The optional arguments are used to configure the query builder of the edge. 347 func (aq *AlertQuery) WithOwner(opts ...func(*MachineQuery)) *AlertQuery { 348 query := &MachineQuery{config: aq.config} 349 for _, opt := range opts { 350 opt(query) 351 } 352 aq.withOwner = query 353 return aq 354 } 355 356 // WithDecisions tells the query-builder to eager-load the nodes that are connected to 357 // the "decisions" edge. The optional arguments are used to configure the query builder of the edge. 358 func (aq *AlertQuery) WithDecisions(opts ...func(*DecisionQuery)) *AlertQuery { 359 query := &DecisionQuery{config: aq.config} 360 for _, opt := range opts { 361 opt(query) 362 } 363 aq.withDecisions = query 364 return aq 365 } 366 367 // WithEvents tells the query-builder to eager-load the nodes that are connected to 368 // the "events" edge. The optional arguments are used to configure the query builder of the edge. 369 func (aq *AlertQuery) WithEvents(opts ...func(*EventQuery)) *AlertQuery { 370 query := &EventQuery{config: aq.config} 371 for _, opt := range opts { 372 opt(query) 373 } 374 aq.withEvents = query 375 return aq 376 } 377 378 // WithMetas tells the query-builder to eager-load the nodes that are connected to 379 // the "metas" edge. The optional arguments are used to configure the query builder of the edge. 380 func (aq *AlertQuery) WithMetas(opts ...func(*MetaQuery)) *AlertQuery { 381 query := &MetaQuery{config: aq.config} 382 for _, opt := range opts { 383 opt(query) 384 } 385 aq.withMetas = query 386 return aq 387 } 388 389 // GroupBy is used to group vertices by one or more fields/columns. 390 // It is often used with aggregate functions, like: count, max, mean, min, sum. 391 // 392 // Example: 393 // 394 // var v []struct { 395 // CreatedAt time.Time `json:"created_at,omitempty"` 396 // Count int `json:"count,omitempty"` 397 // } 398 // 399 // client.Alert.Query(). 400 // GroupBy(alert.FieldCreatedAt). 401 // Aggregate(ent.Count()). 402 // Scan(ctx, &v) 403 // 404 func (aq *AlertQuery) GroupBy(field string, fields ...string) *AlertGroupBy { 405 group := &AlertGroupBy{config: aq.config} 406 group.fields = append([]string{field}, fields...) 407 group.path = func(ctx context.Context) (prev *sql.Selector, err error) { 408 if err := aq.prepareQuery(ctx); err != nil { 409 return nil, err 410 } 411 return aq.sqlQuery(ctx), nil 412 } 413 return group 414 } 415 416 // Select allows the selection one or more fields/columns for the given query, 417 // instead of selecting all fields in the entity. 418 // 419 // Example: 420 // 421 // var v []struct { 422 // CreatedAt time.Time `json:"created_at,omitempty"` 423 // } 424 // 425 // client.Alert.Query(). 426 // Select(alert.FieldCreatedAt). 427 // Scan(ctx, &v) 428 // 429 func (aq *AlertQuery) Select(field string, fields ...string) *AlertSelect { 430 aq.fields = append([]string{field}, fields...) 431 return &AlertSelect{AlertQuery: aq} 432 } 433 434 func (aq *AlertQuery) prepareQuery(ctx context.Context) error { 435 for _, f := range aq.fields { 436 if !alert.ValidColumn(f) { 437 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 438 } 439 } 440 if aq.path != nil { 441 prev, err := aq.path(ctx) 442 if err != nil { 443 return err 444 } 445 aq.sql = prev 446 } 447 return nil 448 } 449 450 func (aq *AlertQuery) sqlAll(ctx context.Context) ([]*Alert, error) { 451 var ( 452 nodes = []*Alert{} 453 withFKs = aq.withFKs 454 _spec = aq.querySpec() 455 loadedTypes = [4]bool{ 456 aq.withOwner != nil, 457 aq.withDecisions != nil, 458 aq.withEvents != nil, 459 aq.withMetas != nil, 460 } 461 ) 462 if aq.withOwner != nil { 463 withFKs = true 464 } 465 if withFKs { 466 _spec.Node.Columns = append(_spec.Node.Columns, alert.ForeignKeys...) 467 } 468 _spec.ScanValues = func(columns []string) ([]interface{}, error) { 469 node := &Alert{config: aq.config} 470 nodes = append(nodes, node) 471 return node.scanValues(columns) 472 } 473 _spec.Assign = func(columns []string, values []interface{}) error { 474 if len(nodes) == 0 { 475 return fmt.Errorf("ent: Assign called without calling ScanValues") 476 } 477 node := nodes[len(nodes)-1] 478 node.Edges.loadedTypes = loadedTypes 479 return node.assignValues(columns, values) 480 } 481 if err := sqlgraph.QueryNodes(ctx, aq.driver, _spec); err != nil { 482 return nil, err 483 } 484 if len(nodes) == 0 { 485 return nodes, nil 486 } 487 488 if query := aq.withOwner; query != nil { 489 ids := make([]int, 0, len(nodes)) 490 nodeids := make(map[int][]*Alert) 491 for i := range nodes { 492 fk := nodes[i].machine_alerts 493 if fk != nil { 494 ids = append(ids, *fk) 495 nodeids[*fk] = append(nodeids[*fk], nodes[i]) 496 } 497 } 498 query.Where(machine.IDIn(ids...)) 499 neighbors, err := query.All(ctx) 500 if err != nil { 501 return nil, err 502 } 503 for _, n := range neighbors { 504 nodes, ok := nodeids[n.ID] 505 if !ok { 506 return nil, fmt.Errorf(`unexpected foreign-key "machine_alerts" returned %v`, n.ID) 507 } 508 for i := range nodes { 509 nodes[i].Edges.Owner = n 510 } 511 } 512 } 513 514 if query := aq.withDecisions; query != nil { 515 fks := make([]driver.Value, 0, len(nodes)) 516 nodeids := make(map[int]*Alert) 517 for i := range nodes { 518 fks = append(fks, nodes[i].ID) 519 nodeids[nodes[i].ID] = nodes[i] 520 nodes[i].Edges.Decisions = []*Decision{} 521 } 522 query.withFKs = true 523 query.Where(predicate.Decision(func(s *sql.Selector) { 524 s.Where(sql.InValues(alert.DecisionsColumn, fks...)) 525 })) 526 neighbors, err := query.All(ctx) 527 if err != nil { 528 return nil, err 529 } 530 for _, n := range neighbors { 531 fk := n.alert_decisions 532 if fk == nil { 533 return nil, fmt.Errorf(`foreign-key "alert_decisions" is nil for node %v`, n.ID) 534 } 535 node, ok := nodeids[*fk] 536 if !ok { 537 return nil, fmt.Errorf(`unexpected foreign-key "alert_decisions" returned %v for node %v`, *fk, n.ID) 538 } 539 node.Edges.Decisions = append(node.Edges.Decisions, n) 540 } 541 } 542 543 if query := aq.withEvents; query != nil { 544 fks := make([]driver.Value, 0, len(nodes)) 545 nodeids := make(map[int]*Alert) 546 for i := range nodes { 547 fks = append(fks, nodes[i].ID) 548 nodeids[nodes[i].ID] = nodes[i] 549 nodes[i].Edges.Events = []*Event{} 550 } 551 query.withFKs = true 552 query.Where(predicate.Event(func(s *sql.Selector) { 553 s.Where(sql.InValues(alert.EventsColumn, fks...)) 554 })) 555 neighbors, err := query.All(ctx) 556 if err != nil { 557 return nil, err 558 } 559 for _, n := range neighbors { 560 fk := n.alert_events 561 if fk == nil { 562 return nil, fmt.Errorf(`foreign-key "alert_events" is nil for node %v`, n.ID) 563 } 564 node, ok := nodeids[*fk] 565 if !ok { 566 return nil, fmt.Errorf(`unexpected foreign-key "alert_events" returned %v for node %v`, *fk, n.ID) 567 } 568 node.Edges.Events = append(node.Edges.Events, n) 569 } 570 } 571 572 if query := aq.withMetas; query != nil { 573 fks := make([]driver.Value, 0, len(nodes)) 574 nodeids := make(map[int]*Alert) 575 for i := range nodes { 576 fks = append(fks, nodes[i].ID) 577 nodeids[nodes[i].ID] = nodes[i] 578 nodes[i].Edges.Metas = []*Meta{} 579 } 580 query.withFKs = true 581 query.Where(predicate.Meta(func(s *sql.Selector) { 582 s.Where(sql.InValues(alert.MetasColumn, fks...)) 583 })) 584 neighbors, err := query.All(ctx) 585 if err != nil { 586 return nil, err 587 } 588 for _, n := range neighbors { 589 fk := n.alert_metas 590 if fk == nil { 591 return nil, fmt.Errorf(`foreign-key "alert_metas" is nil for node %v`, n.ID) 592 } 593 node, ok := nodeids[*fk] 594 if !ok { 595 return nil, fmt.Errorf(`unexpected foreign-key "alert_metas" returned %v for node %v`, *fk, n.ID) 596 } 597 node.Edges.Metas = append(node.Edges.Metas, n) 598 } 599 } 600 601 return nodes, nil 602 } 603 604 func (aq *AlertQuery) sqlCount(ctx context.Context) (int, error) { 605 _spec := aq.querySpec() 606 return sqlgraph.CountNodes(ctx, aq.driver, _spec) 607 } 608 609 func (aq *AlertQuery) sqlExist(ctx context.Context) (bool, error) { 610 n, err := aq.sqlCount(ctx) 611 if err != nil { 612 return false, fmt.Errorf("ent: check existence: %w", err) 613 } 614 return n > 0, nil 615 } 616 617 func (aq *AlertQuery) querySpec() *sqlgraph.QuerySpec { 618 _spec := &sqlgraph.QuerySpec{ 619 Node: &sqlgraph.NodeSpec{ 620 Table: alert.Table, 621 Columns: alert.Columns, 622 ID: &sqlgraph.FieldSpec{ 623 Type: field.TypeInt, 624 Column: alert.FieldID, 625 }, 626 }, 627 From: aq.sql, 628 Unique: true, 629 } 630 if fields := aq.fields; len(fields) > 0 { 631 _spec.Node.Columns = make([]string, 0, len(fields)) 632 _spec.Node.Columns = append(_spec.Node.Columns, alert.FieldID) 633 for i := range fields { 634 if fields[i] != alert.FieldID { 635 _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) 636 } 637 } 638 } 639 if ps := aq.predicates; len(ps) > 0 { 640 _spec.Predicate = func(selector *sql.Selector) { 641 for i := range ps { 642 ps[i](selector) 643 } 644 } 645 } 646 if limit := aq.limit; limit != nil { 647 _spec.Limit = *limit 648 } 649 if offset := aq.offset; offset != nil { 650 _spec.Offset = *offset 651 } 652 if ps := aq.order; len(ps) > 0 { 653 _spec.Order = func(selector *sql.Selector) { 654 for i := range ps { 655 ps[i](selector, alert.ValidColumn) 656 } 657 } 658 } 659 return _spec 660 } 661 662 func (aq *AlertQuery) sqlQuery(ctx context.Context) *sql.Selector { 663 builder := sql.Dialect(aq.driver.Dialect()) 664 t1 := builder.Table(alert.Table) 665 selector := builder.Select(t1.Columns(alert.Columns...)...).From(t1) 666 if aq.sql != nil { 667 selector = aq.sql 668 selector.Select(selector.Columns(alert.Columns...)...) 669 } 670 for _, p := range aq.predicates { 671 p(selector) 672 } 673 for _, p := range aq.order { 674 p(selector, alert.ValidColumn) 675 } 676 if offset := aq.offset; offset != nil { 677 // limit is mandatory for offset clause. We start 678 // with default value, and override it below if needed. 679 selector.Offset(*offset).Limit(math.MaxInt32) 680 } 681 if limit := aq.limit; limit != nil { 682 selector.Limit(*limit) 683 } 684 return selector 685 } 686 687 // AlertGroupBy is the group-by builder for Alert entities. 688 type AlertGroupBy struct { 689 config 690 fields []string 691 fns []AggregateFunc 692 // intermediate query (i.e. traversal path). 693 sql *sql.Selector 694 path func(context.Context) (*sql.Selector, error) 695 } 696 697 // Aggregate adds the given aggregation functions to the group-by query. 698 func (agb *AlertGroupBy) Aggregate(fns ...AggregateFunc) *AlertGroupBy { 699 agb.fns = append(agb.fns, fns...) 700 return agb 701 } 702 703 // Scan applies the group-by query and scans the result into the given value. 704 func (agb *AlertGroupBy) Scan(ctx context.Context, v interface{}) error { 705 query, err := agb.path(ctx) 706 if err != nil { 707 return err 708 } 709 agb.sql = query 710 return agb.sqlScan(ctx, v) 711 } 712 713 // ScanX is like Scan, but panics if an error occurs. 714 func (agb *AlertGroupBy) ScanX(ctx context.Context, v interface{}) { 715 if err := agb.Scan(ctx, v); err != nil { 716 panic(err) 717 } 718 } 719 720 // Strings returns list of strings from group-by. 721 // It is only allowed when executing a group-by query with one field. 722 func (agb *AlertGroupBy) Strings(ctx context.Context) ([]string, error) { 723 if len(agb.fields) > 1 { 724 return nil, errors.New("ent: AlertGroupBy.Strings is not achievable when grouping more than 1 field") 725 } 726 var v []string 727 if err := agb.Scan(ctx, &v); err != nil { 728 return nil, err 729 } 730 return v, nil 731 } 732 733 // StringsX is like Strings, but panics if an error occurs. 734 func (agb *AlertGroupBy) StringsX(ctx context.Context) []string { 735 v, err := agb.Strings(ctx) 736 if err != nil { 737 panic(err) 738 } 739 return v 740 } 741 742 // String returns a single string from a group-by query. 743 // It is only allowed when executing a group-by query with one field. 744 func (agb *AlertGroupBy) String(ctx context.Context) (_ string, err error) { 745 var v []string 746 if v, err = agb.Strings(ctx); err != nil { 747 return 748 } 749 switch len(v) { 750 case 1: 751 return v[0], nil 752 case 0: 753 err = &NotFoundError{alert.Label} 754 default: 755 err = fmt.Errorf("ent: AlertGroupBy.Strings returned %d results when one was expected", len(v)) 756 } 757 return 758 } 759 760 // StringX is like String, but panics if an error occurs. 761 func (agb *AlertGroupBy) StringX(ctx context.Context) string { 762 v, err := agb.String(ctx) 763 if err != nil { 764 panic(err) 765 } 766 return v 767 } 768 769 // Ints returns list of ints from group-by. 770 // It is only allowed when executing a group-by query with one field. 771 func (agb *AlertGroupBy) Ints(ctx context.Context) ([]int, error) { 772 if len(agb.fields) > 1 { 773 return nil, errors.New("ent: AlertGroupBy.Ints is not achievable when grouping more than 1 field") 774 } 775 var v []int 776 if err := agb.Scan(ctx, &v); err != nil { 777 return nil, err 778 } 779 return v, nil 780 } 781 782 // IntsX is like Ints, but panics if an error occurs. 783 func (agb *AlertGroupBy) IntsX(ctx context.Context) []int { 784 v, err := agb.Ints(ctx) 785 if err != nil { 786 panic(err) 787 } 788 return v 789 } 790 791 // Int returns a single int from a group-by query. 792 // It is only allowed when executing a group-by query with one field. 793 func (agb *AlertGroupBy) Int(ctx context.Context) (_ int, err error) { 794 var v []int 795 if v, err = agb.Ints(ctx); err != nil { 796 return 797 } 798 switch len(v) { 799 case 1: 800 return v[0], nil 801 case 0: 802 err = &NotFoundError{alert.Label} 803 default: 804 err = fmt.Errorf("ent: AlertGroupBy.Ints returned %d results when one was expected", len(v)) 805 } 806 return 807 } 808 809 // IntX is like Int, but panics if an error occurs. 810 func (agb *AlertGroupBy) IntX(ctx context.Context) int { 811 v, err := agb.Int(ctx) 812 if err != nil { 813 panic(err) 814 } 815 return v 816 } 817 818 // Float64s returns list of float64s from group-by. 819 // It is only allowed when executing a group-by query with one field. 820 func (agb *AlertGroupBy) Float64s(ctx context.Context) ([]float64, error) { 821 if len(agb.fields) > 1 { 822 return nil, errors.New("ent: AlertGroupBy.Float64s is not achievable when grouping more than 1 field") 823 } 824 var v []float64 825 if err := agb.Scan(ctx, &v); err != nil { 826 return nil, err 827 } 828 return v, nil 829 } 830 831 // Float64sX is like Float64s, but panics if an error occurs. 832 func (agb *AlertGroupBy) Float64sX(ctx context.Context) []float64 { 833 v, err := agb.Float64s(ctx) 834 if err != nil { 835 panic(err) 836 } 837 return v 838 } 839 840 // Float64 returns a single float64 from a group-by query. 841 // It is only allowed when executing a group-by query with one field. 842 func (agb *AlertGroupBy) Float64(ctx context.Context) (_ float64, err error) { 843 var v []float64 844 if v, err = agb.Float64s(ctx); err != nil { 845 return 846 } 847 switch len(v) { 848 case 1: 849 return v[0], nil 850 case 0: 851 err = &NotFoundError{alert.Label} 852 default: 853 err = fmt.Errorf("ent: AlertGroupBy.Float64s returned %d results when one was expected", len(v)) 854 } 855 return 856 } 857 858 // Float64X is like Float64, but panics if an error occurs. 859 func (agb *AlertGroupBy) Float64X(ctx context.Context) float64 { 860 v, err := agb.Float64(ctx) 861 if err != nil { 862 panic(err) 863 } 864 return v 865 } 866 867 // Bools returns list of bools from group-by. 868 // It is only allowed when executing a group-by query with one field. 869 func (agb *AlertGroupBy) Bools(ctx context.Context) ([]bool, error) { 870 if len(agb.fields) > 1 { 871 return nil, errors.New("ent: AlertGroupBy.Bools is not achievable when grouping more than 1 field") 872 } 873 var v []bool 874 if err := agb.Scan(ctx, &v); err != nil { 875 return nil, err 876 } 877 return v, nil 878 } 879 880 // BoolsX is like Bools, but panics if an error occurs. 881 func (agb *AlertGroupBy) BoolsX(ctx context.Context) []bool { 882 v, err := agb.Bools(ctx) 883 if err != nil { 884 panic(err) 885 } 886 return v 887 } 888 889 // Bool returns a single bool from a group-by query. 890 // It is only allowed when executing a group-by query with one field. 891 func (agb *AlertGroupBy) Bool(ctx context.Context) (_ bool, err error) { 892 var v []bool 893 if v, err = agb.Bools(ctx); err != nil { 894 return 895 } 896 switch len(v) { 897 case 1: 898 return v[0], nil 899 case 0: 900 err = &NotFoundError{alert.Label} 901 default: 902 err = fmt.Errorf("ent: AlertGroupBy.Bools returned %d results when one was expected", len(v)) 903 } 904 return 905 } 906 907 // BoolX is like Bool, but panics if an error occurs. 908 func (agb *AlertGroupBy) BoolX(ctx context.Context) bool { 909 v, err := agb.Bool(ctx) 910 if err != nil { 911 panic(err) 912 } 913 return v 914 } 915 916 func (agb *AlertGroupBy) sqlScan(ctx context.Context, v interface{}) error { 917 for _, f := range agb.fields { 918 if !alert.ValidColumn(f) { 919 return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)} 920 } 921 } 922 selector := agb.sqlQuery() 923 if err := selector.Err(); err != nil { 924 return err 925 } 926 rows := &sql.Rows{} 927 query, args := selector.Query() 928 if err := agb.driver.Query(ctx, query, args, rows); err != nil { 929 return err 930 } 931 defer rows.Close() 932 return sql.ScanSlice(rows, v) 933 } 934 935 func (agb *AlertGroupBy) sqlQuery() *sql.Selector { 936 selector := agb.sql 937 columns := make([]string, 0, len(agb.fields)+len(agb.fns)) 938 columns = append(columns, agb.fields...) 939 for _, fn := range agb.fns { 940 columns = append(columns, fn(selector, alert.ValidColumn)) 941 } 942 return selector.Select(columns...).GroupBy(agb.fields...) 943 } 944 945 // AlertSelect is the builder for selecting fields of Alert entities. 946 type AlertSelect struct { 947 *AlertQuery 948 // intermediate query (i.e. traversal path). 949 sql *sql.Selector 950 } 951 952 // Scan applies the selector query and scans the result into the given value. 953 func (as *AlertSelect) Scan(ctx context.Context, v interface{}) error { 954 if err := as.prepareQuery(ctx); err != nil { 955 return err 956 } 957 as.sql = as.AlertQuery.sqlQuery(ctx) 958 return as.sqlScan(ctx, v) 959 } 960 961 // ScanX is like Scan, but panics if an error occurs. 962 func (as *AlertSelect) ScanX(ctx context.Context, v interface{}) { 963 if err := as.Scan(ctx, v); err != nil { 964 panic(err) 965 } 966 } 967 968 // Strings returns list of strings from a selector. It is only allowed when selecting one field. 969 func (as *AlertSelect) Strings(ctx context.Context) ([]string, error) { 970 if len(as.fields) > 1 { 971 return nil, errors.New("ent: AlertSelect.Strings is not achievable when selecting more than 1 field") 972 } 973 var v []string 974 if err := as.Scan(ctx, &v); err != nil { 975 return nil, err 976 } 977 return v, nil 978 } 979 980 // StringsX is like Strings, but panics if an error occurs. 981 func (as *AlertSelect) StringsX(ctx context.Context) []string { 982 v, err := as.Strings(ctx) 983 if err != nil { 984 panic(err) 985 } 986 return v 987 } 988 989 // String returns a single string from a selector. It is only allowed when selecting one field. 990 func (as *AlertSelect) String(ctx context.Context) (_ string, err error) { 991 var v []string 992 if v, err = as.Strings(ctx); err != nil { 993 return 994 } 995 switch len(v) { 996 case 1: 997 return v[0], nil 998 case 0: 999 err = &NotFoundError{alert.Label} 1000 default: 1001 err = fmt.Errorf("ent: AlertSelect.Strings returned %d results when one was expected", len(v)) 1002 } 1003 return 1004 } 1005 1006 // StringX is like String, but panics if an error occurs. 1007 func (as *AlertSelect) StringX(ctx context.Context) string { 1008 v, err := as.String(ctx) 1009 if err != nil { 1010 panic(err) 1011 } 1012 return v 1013 } 1014 1015 // Ints returns list of ints from a selector. It is only allowed when selecting one field. 1016 func (as *AlertSelect) Ints(ctx context.Context) ([]int, error) { 1017 if len(as.fields) > 1 { 1018 return nil, errors.New("ent: AlertSelect.Ints is not achievable when selecting more than 1 field") 1019 } 1020 var v []int 1021 if err := as.Scan(ctx, &v); err != nil { 1022 return nil, err 1023 } 1024 return v, nil 1025 } 1026 1027 // IntsX is like Ints, but panics if an error occurs. 1028 func (as *AlertSelect) IntsX(ctx context.Context) []int { 1029 v, err := as.Ints(ctx) 1030 if err != nil { 1031 panic(err) 1032 } 1033 return v 1034 } 1035 1036 // Int returns a single int from a selector. It is only allowed when selecting one field. 1037 func (as *AlertSelect) Int(ctx context.Context) (_ int, err error) { 1038 var v []int 1039 if v, err = as.Ints(ctx); err != nil { 1040 return 1041 } 1042 switch len(v) { 1043 case 1: 1044 return v[0], nil 1045 case 0: 1046 err = &NotFoundError{alert.Label} 1047 default: 1048 err = fmt.Errorf("ent: AlertSelect.Ints returned %d results when one was expected", len(v)) 1049 } 1050 return 1051 } 1052 1053 // IntX is like Int, but panics if an error occurs. 1054 func (as *AlertSelect) IntX(ctx context.Context) int { 1055 v, err := as.Int(ctx) 1056 if err != nil { 1057 panic(err) 1058 } 1059 return v 1060 } 1061 1062 // Float64s returns list of float64s from a selector. It is only allowed when selecting one field. 1063 func (as *AlertSelect) Float64s(ctx context.Context) ([]float64, error) { 1064 if len(as.fields) > 1 { 1065 return nil, errors.New("ent: AlertSelect.Float64s is not achievable when selecting more than 1 field") 1066 } 1067 var v []float64 1068 if err := as.Scan(ctx, &v); err != nil { 1069 return nil, err 1070 } 1071 return v, nil 1072 } 1073 1074 // Float64sX is like Float64s, but panics if an error occurs. 1075 func (as *AlertSelect) Float64sX(ctx context.Context) []float64 { 1076 v, err := as.Float64s(ctx) 1077 if err != nil { 1078 panic(err) 1079 } 1080 return v 1081 } 1082 1083 // Float64 returns a single float64 from a selector. It is only allowed when selecting one field. 1084 func (as *AlertSelect) Float64(ctx context.Context) (_ float64, err error) { 1085 var v []float64 1086 if v, err = as.Float64s(ctx); err != nil { 1087 return 1088 } 1089 switch len(v) { 1090 case 1: 1091 return v[0], nil 1092 case 0: 1093 err = &NotFoundError{alert.Label} 1094 default: 1095 err = fmt.Errorf("ent: AlertSelect.Float64s returned %d results when one was expected", len(v)) 1096 } 1097 return 1098 } 1099 1100 // Float64X is like Float64, but panics if an error occurs. 1101 func (as *AlertSelect) Float64X(ctx context.Context) float64 { 1102 v, err := as.Float64(ctx) 1103 if err != nil { 1104 panic(err) 1105 } 1106 return v 1107 } 1108 1109 // Bools returns list of bools from a selector. It is only allowed when selecting one field. 1110 func (as *AlertSelect) Bools(ctx context.Context) ([]bool, error) { 1111 if len(as.fields) > 1 { 1112 return nil, errors.New("ent: AlertSelect.Bools is not achievable when selecting more than 1 field") 1113 } 1114 var v []bool 1115 if err := as.Scan(ctx, &v); err != nil { 1116 return nil, err 1117 } 1118 return v, nil 1119 } 1120 1121 // BoolsX is like Bools, but panics if an error occurs. 1122 func (as *AlertSelect) BoolsX(ctx context.Context) []bool { 1123 v, err := as.Bools(ctx) 1124 if err != nil { 1125 panic(err) 1126 } 1127 return v 1128 } 1129 1130 // Bool returns a single bool from a selector. It is only allowed when selecting one field. 1131 func (as *AlertSelect) Bool(ctx context.Context) (_ bool, err error) { 1132 var v []bool 1133 if v, err = as.Bools(ctx); err != nil { 1134 return 1135 } 1136 switch len(v) { 1137 case 1: 1138 return v[0], nil 1139 case 0: 1140 err = &NotFoundError{alert.Label} 1141 default: 1142 err = fmt.Errorf("ent: AlertSelect.Bools returned %d results when one was expected", len(v)) 1143 } 1144 return 1145 } 1146 1147 // BoolX is like Bool, but panics if an error occurs. 1148 func (as *AlertSelect) BoolX(ctx context.Context) bool { 1149 v, err := as.Bool(ctx) 1150 if err != nil { 1151 panic(err) 1152 } 1153 return v 1154 } 1155 1156 func (as *AlertSelect) sqlScan(ctx context.Context, v interface{}) error { 1157 rows := &sql.Rows{} 1158 query, args := as.sqlQuery().Query() 1159 if err := as.driver.Query(ctx, query, args, rows); err != nil { 1160 return err 1161 } 1162 defer rows.Close() 1163 return sql.ScanSlice(rows, v) 1164 } 1165 1166 func (as *AlertSelect) sqlQuery() sql.Querier { 1167 selector := as.sql 1168 selector.Select(selector.Columns(as.fields...)...) 1169 return selector 1170 }