Skip to content

Commit

Permalink
feat(generate): preload with offset and limit
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Dec 12, 2021
1 parent fe851bf commit 5d8fc03
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
9 changes: 7 additions & 2 deletions README.ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
- [`If` 子句](#if-clause)
- [`Where` 子句](#where-clause)
- [`Set` 子句](#set-clause)
- [`For` 子句](#for-clause)
- [方法接口示例](#method-interface-example)
- [单元测试](#unit-test)
- [智能选择字段](#smart-select-fields)
Expand Down Expand Up @@ -1424,7 +1425,7 @@ users, err := u.WithContext(ctx).Preload(u.Orders.OrderItems.Product).Find()

###### <span id="nested-preloading">根据条件预加载</span>

GORM 允许预加载与条件关联,它的工作原理类似于内联条件。
GEN 允许预加载与条件关联,它的工作原理类似于内联条件。

```go
q := query.Use(db)
Expand All @@ -1451,6 +1452,10 @@ users, err := u.WithContext(ctx).Preload(u.Orders.On(o.State.Eq("on")).Order(o.I
users, err := u.WithContext(ctx).Preload(u.Orders.Clauses(hints.UseIndex("idx_order_id"))).Find()
// SELECT * FROM users;
// SELECT * FROM orders WHERE user_id IN (1,2) USE INDEX (`idx_order_id`);

user, err := u.WithContext(ctx).Where(u.ID.Eq(1)).Preload(u.Orders.Offset(100).Limit(20)).Take()
// SELECT * FROM users WHERE `user_id` = 1 LIMIT 20 OFFSET 100;
// SELECT * FROM `users` WHERE `users`.`id` = 1 LIMIT 1
```

###### <span id="nested-preloading">嵌套预加载</span>
Expand Down Expand Up @@ -1762,7 +1767,7 @@ update @@table
where id=@id
```

###### `For` 子句
###### <span id="for-clause">`For` 子句</span>

```
{{for _,name:=range names}}
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Gen: Friendly & Safer [GORM](https://github.com/go-gorm/gorm) powered by Code Ge
- [`If` clause](#if-clause)
- [`Where` clause](#where-clause)
- [`Set` clause](#set-clause)
- [`For` clause](#for-clause)
- [Method interface example](#method-interface-example)
- [Unit Test](#unit-test)
- [Smart select fields](#smart-select-fields)
Expand Down Expand Up @@ -1428,7 +1429,7 @@ users, err := u.WithContext(ctx).Preload(u.Orders.OrderItems.Product).Find()

###### Preload with conditions

GORM allows Preload associations with conditions, it works similar to Inline Conditions.
GEN allows Preload associations with conditions, it works similar to Inline Conditions.

```go
q := query.Use(db)
Expand All @@ -1455,6 +1456,10 @@ users, err := u.WithContext(ctx).Preload(u.Orders.On(o.State.Eq("on")).Order(o.I
users, err := u.WithContext(ctx).Preload(u.Orders.Clauses(hints.UseIndex("idx_order_id"))).Find()
// SELECT * FROM users;
// SELECT * FROM orders WHERE user_id IN (1,2) USE INDEX (`idx_order_id`);

user, err := u.WithContext(ctx).Where(u.ID.Eq(1)).Preload(u.Orders.Offset(100).Limit(20)).Take()
// SELECT * FROM users WHERE `user_id` = 1 LIMIT 20 OFFSET 100;
// SELECT * FROM `users` WHERE `users`.`id` = 1 LIMIT 1
```

###### Nested Preloading
Expand Down
5 changes: 5 additions & 0 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ func (d *DO) Preload(field field.RelationField) Dao {
return db.Clauses(clauses...)
})
}
if offset, limit := field.GetPage(); offset|limit != 0 {
args = append(args, func(db *gorm.DB) *gorm.DB {
return db.Offset(offset).Limit(limit)
})
}
return d.getInstance(d.db.Preload(field.Path(), args...))
}

Expand Down
27 changes: 18 additions & 9 deletions field/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ type RelationField interface {
On(conds ...Expr) RelationField
Order(columns ...Expr) RelationField
Clauses(hints ...clause.Expression) RelationField
Offset(offset int) RelationField
Limit(limit int) RelationField

GetConds() []Expr
GetOrderCol() []Expr
GetClauses() []clause.Expression
GetPage() (offset, limit int)
}

type Relation struct {
Expand All @@ -43,9 +46,10 @@ type Relation struct {

childRelations []Relation

conds []Expr
order []Expr
clauses []clause.Expression
conds []Expr
order []Expr
clauses []clause.Expression
limit, offset int
}

func (r Relation) Name() string { return r.fieldName }
Expand Down Expand Up @@ -77,22 +81,27 @@ func (r Relation) On(conds ...Expr) RelationField {
r.conds = append(r.conds, conds...)
return &r
}

func (r Relation) Order(columns ...Expr) RelationField {
r.order = append(r.order, columns...)
return &r
}

func (r Relation) Clauses(hints ...clause.Expression) RelationField {
r.clauses = append(r.clauses, hints...)
return &r
}
func (r Relation) Offset(offset int) RelationField {
r.offset = offset
return &r
}
func (r Relation) Limit(limit int) RelationField {
r.limit = limit
return &r
}

func (r *Relation) GetConds() []Expr { return r.conds }

func (r *Relation) GetOrderCol() []Expr { return r.order }

func (r *Relation) GetConds() []Expr { return r.conds }
func (r *Relation) GetOrderCol() []Expr { return r.order }
func (r *Relation) GetClauses() []clause.Expression { return r.clauses }
func (r *Relation) GetPage() (offset, limit int) { return r.offset, r.limit }

func (r *Relation) StructMember() string {
var memberStr string
Expand Down

0 comments on commit 5d8fc03

Please sign in to comment.