From a8d3a3561df98214dbf2559249eacc38335ffe6e Mon Sep 17 00:00:00 2001 From: riverchu Date: Wed, 22 Sep 2021 19:34:58 +0800 Subject: [PATCH] feat: implement relation --- do.go | 12 ++++++------ field/export.go | 2 ++ field/relation.go | 17 +++++++++++++++++ interface.go | 4 ++-- internal/template/method.go | 12 ++++++------ 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 field/relation.go diff --git a/do.go b/do.go index bc6d7741..a985ad50 100644 --- a/do.go +++ b/do.go @@ -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) } @@ -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 { diff --git a/field/export.go b/field/export.go index 952c82a7..739c6d39 100644 --- a/field/export.go +++ b/field/export.go @@ -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} } diff --git a/field/relation.go b/field/relation.go new file mode 100644 index 00000000..6930ea74 --- /dev/null +++ b/field/relation.go @@ -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) +} diff --git a/interface.go b/interface.go index 094f4630..c5bab4bb 100644 --- a/interface.go +++ b/interface.go @@ -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 @@ -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 diff --git a/internal/template/method.go b/internal/template/method.go index 3ae6c164..bf6d8ba0 100644 --- a/internal/template/method.go +++ b/internal/template/method.go @@ -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...)) } @@ -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...)) }