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