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