| 581 | } |
| 582 | |
| 583 | func (_q *ParentQuery) sqlQuery(ctx context.Context) *sql.Selector { |
| 584 | builder := sql.Dialect(_q.driver.Dialect()) |
| 585 | t1 := builder.Table(parent.Table) |
| 586 | columns := _q.ctx.Fields |
| 587 | if len(columns) == 0 { |
| 588 | columns = parent.Columns |
| 589 | } |
| 590 | selector := builder.Select(t1.Columns(columns...)...).From(t1) |
| 591 | if _q.sql != nil { |
| 592 | selector = _q.sql |
| 593 | selector.Select(selector.Columns(columns...)...) |
| 594 | } |
| 595 | if _q.ctx.Unique != nil && *_q.ctx.Unique { |
| 596 | selector.Distinct() |
| 597 | } |
| 598 | t1.Schema(_q.schemaConfig.Parent) |
| 599 | ctx = internal.NewSchemaConfigContext(ctx, _q.schemaConfig) |
| 600 | selector.WithContext(ctx) |
| 601 | for _, m := range _q.modifiers { |
| 602 | m(selector) |
| 603 | } |
| 604 | for _, p := range _q.predicates { |
| 605 | p(selector) |
| 606 | } |
| 607 | for _, p := range _q.order { |
| 608 | p(selector) |
| 609 | } |
| 610 | if offset := _q.ctx.Offset; offset != nil { |
| 611 | // limit is mandatory for offset clause. We start |
| 612 | // with default value, and override it below if needed. |
| 613 | selector.Offset(*offset).Limit(math.MaxInt32) |
| 614 | } |
| 615 | if limit := _q.ctx.Limit; limit != nil { |
| 616 | selector.Limit(*limit) |
| 617 | } |
| 618 | return selector |
| 619 | } |
| 620 | |
| 621 | // Modify adds a query modifier for attaching custom logic to queries. |
| 622 | func (_q *ParentQuery) Modify(modifiers ...func(s *sql.Selector)) *ParentSelect { |