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