Skip to content

Commit

Permalink
feat: implement relation
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Sep 22, 2021
1 parent 7a3677a commit a8d3a35
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
12 changes: 6 additions & 6 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,6 @@ func (d *DO) Unscoped() Dao {
return d.getInstance(d.db.Unscoped())
}

func (d *DO) Joins(table schema.Tabler, on ...field.Expr) Dao {
return d.getInstance(d.db.Joins(table.TableName(), toExpressionInterface(on...)...))
}

func (d *DO) Join(table schema.Tabler, conds ...field.Expr) Dao {
return d.join(table, clause.InnerJoin, conds)
}
Expand Down Expand Up @@ -320,8 +316,12 @@ func (d *DO) Assign(attrs ...field.Expr) Dao {
return d.getInstance(d.db.Assign(toExpressionInterface(attrs...)...))
}

func (d *DO) Preload(column field.Expr, on ...field.Expr) Dao {
return d.getInstance(d.db.Preload(column.ColumnName().String(), toExpressionInterface(on...)...))
func (d *DO) Joins(column field.RelationPath, on ...field.Expr) Dao {
return d.getInstance(d.db.Joins(string(column.Path()), toExpressionInterface(on...)...))
}

func (d *DO) Preload(column field.RelationPath, on ...field.Expr) Dao {
return d.getInstance(d.db.Preload(string(column.Path()), toExpressionInterface(on...)...))
}

func getFromClause(db *gorm.DB) *clause.From {
Expand Down
2 changes: 2 additions & 0 deletions field/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,5 @@ func ContainsValue(columns []Expr, value Value) Expr {
}

func EmptyExpr() Expr { return expr{e: clause.Expr{}} }

func NewRelation(varName string) Relation { return Relation{varName: varName} }
17 changes: 17 additions & 0 deletions field/relation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package field

type RelationPath interface {
Path() relationPath
}

type relationPath string

func (p relationPath) Path() relationPath { return p }

type Relation struct {
varName string
}

func (r Relation) Path() relationPath {
return relationPath(r.varName)
}
4 changes: 2 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type Dao interface {
Order(columns ...field.Expr) Dao
Distinct(columns ...field.Expr) Dao
Omit(columns ...field.Expr) Dao
Joins(table schema.Tabler, on ...field.Expr) Dao
Join(table schema.Tabler, conds ...field.Expr) Dao
LeftJoin(table schema.Tabler, conds ...field.Expr) Dao
RightJoin(table schema.Tabler, conds ...field.Expr) Dao
Expand All @@ -60,7 +59,8 @@ type Dao interface {
Unscoped() Dao
Attrs(attrs ...field.Expr) Dao
Assign(attrs ...field.Expr) Dao
Preload(column field.Expr, on ...field.Expr) Dao
Joins(column field.RelationPath, on ...field.Expr) Dao
Preload(column field.RelationPath, on ...field.Expr) Dao

Create(value interface{}) error
CreateInBatches(value interface{}, batchSize int) error
Expand Down
12 changes: 6 additions & 6 deletions internal/template/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ func ({{.S}} {{.NewStructName}}Do) Omit(cols ...field.Expr) *{{.NewStructName}}D
return {{.S}}.withDO({{.S}}.DO.Omit(cols...))
}
// Joins eager preloading
func ({{.S}} {{.NewStructName}}Do) Joins(table schema.Tabler, on ...field.Expr) *{{.NewStructName}}Do {
return {{.S}}.withDO({{.S}}.DO.Joins(table, on...))
}
func ({{.S}} {{.NewStructName}}Do) Join(table schema.Tabler, on ...field.Expr) *{{.NewStructName}}Do {
return {{.S}}.withDO({{.S}}.DO.Join(table, on...))
}
Expand Down Expand Up @@ -165,7 +160,12 @@ func ({{.S}} {{.NewStructName}}Do) Assign(attrs ...field.Expr) *{{.NewStructName
return {{.S}}.withDO({{.S}}.DO.Assign(attrs...))
}
func ({{.S}} {{.NewStructName}}Do) Preload(column field.Expr, on ...field.Expr) *{{.NewStructName}}Do {
// Joins eager preloading
func ({{.S}} {{.NewStructName}}Do) Joins(column field.RelationPath, on ...field.Expr) *{{.NewStructName}}Do {
return {{.S}}.withDO({{.S}}.DO.Joins(column, on...))
}
func ({{.S}} {{.NewStructName}}Do) Preload(column field.RelationPath, on ...field.Expr) *{{.NewStructName}}Do {
return {{.S}}.withDO({{.S}}.DO.Preload(column, on...))
}
Expand Down

0 comments on commit a8d3a35

Please sign in to comment.