| 664 | } |
| 665 | |
| 666 | func (eq *EntityQuery) sqlQuery(ctx context.Context) *sql.Selector { |
| 667 | builder := sql.Dialect(eq.driver.Dialect()) |
| 668 | t1 := builder.Table(entity.Table) |
| 669 | columns := eq.ctx.Fields |
| 670 | if len(columns) == 0 { |
| 671 | columns = entity.Columns |
| 672 | } |
| 673 | selector := builder.Select(t1.Columns(columns...)...).From(t1) |
| 674 | if eq.sql != nil { |
| 675 | selector = eq.sql |
| 676 | selector.Select(selector.Columns(columns...)...) |
| 677 | } |
| 678 | if eq.ctx.Unique != nil && *eq.ctx.Unique { |
| 679 | selector.Distinct() |
| 680 | } |
| 681 | for _, p := range eq.predicates { |
| 682 | p(selector) |
| 683 | } |
| 684 | for _, p := range eq.order { |
| 685 | p(selector) |
| 686 | } |
| 687 | if offset := eq.ctx.Offset; offset != nil { |
| 688 | // limit is mandatory for offset clause. We start |
| 689 | // with default value, and override it below if needed. |
| 690 | selector.Offset(*offset).Limit(math.MaxInt32) |
| 691 | } |
| 692 | if limit := eq.ctx.Limit; limit != nil { |
| 693 | selector.Limit(*limit) |
| 694 | } |
| 695 | return selector |
| 696 | } |
| 697 | |
| 698 | // EntityGroupBy is the group-by builder for Entity entities. |
| 699 | type EntityGroupBy struct { |