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