Skip to content

Commit

Permalink
add method Hints
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r authored Aug 16, 2021
2 parents 5d39c05 + cd9b5f6 commit cb3860a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
43 changes: 28 additions & 15 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/hints"

"gorm.io/gen/field"
)
Expand Down Expand Up @@ -131,18 +132,22 @@ func (d *DO) Debug() Dao {
return NewDO(d.db.Debug())
}

func (d *DO) Hints(hs ...hints.Hints) Dao {
return NewDO(d.db.Clauses(hintToExpression(hs)...))
}

// As alias cannot be heired, As must used on tail
func (d *DO) As(alias string) Dao {
return &DO{db: d.db, alias: alias}
}

// ======================== chainable api ========================
func (d *DO) Not(conds ...Condition) Dao {
return NewDO(d.db.Clauses(clause.Where{Exprs: []clause.Expression{clause.Not(condToExpression(conds...)...)}}))
return NewDO(d.db.Clauses(clause.Where{Exprs: []clause.Expression{clause.Not(condToExpression(conds)...)}}))
}

func (d *DO) Or(conds ...Condition) Dao {
return NewDO(d.db.Clauses(clause.Where{Exprs: []clause.Expression{clause.Or(clause.And(condToExpression(conds...)...))}}))
return NewDO(d.db.Clauses(clause.Where{Exprs: []clause.Expression{clause.Or(clause.And(condToExpression(conds)...))}}))
}

func (d *DO) Select(columns ...field.Expr) Dao {
Expand All @@ -153,11 +158,11 @@ func (d *DO) Select(columns ...field.Expr) Dao {
}

func (d *DO) Where(conds ...Condition) Dao {
return NewDO(d.db.Clauses(clause.Where{Exprs: condToExpression(conds...)}))
return NewDO(d.db.Clauses(clause.Where{Exprs: condToExpression(conds)}))
}

func (d *DO) Order(columns ...field.Expr) Dao {
return NewDO(d.db.Clauses(clause.OrderBy{Expression: clause.CommaExpression{Exprs: toExpression(columns...)}}))
return NewDO(d.db.Clauses(clause.OrderBy{Expression: clause.CommaExpression{Exprs: toExpression(columns)}}))
}

func (d *DO) Distinct(columns ...field.Expr) Dao {
Expand All @@ -173,7 +178,7 @@ func (d *DO) Group(column field.Expr) Dao {
}

func (d *DO) Having(conds ...Condition) Dao {
return NewDO(d.db.Clauses(clause.GroupBy{Having: condToExpression(conds...)}))
return NewDO(d.db.Clauses(clause.GroupBy{Having: condToExpression(conds)}))
}

func (d *DO) Limit(limit int) Dao {
Expand Down Expand Up @@ -213,7 +218,7 @@ func (d *DO) join(table schema.Tabler, joinType clause.JoinType, conds ...Condit
from.Joins = append(from.Joins, clause.Join{
Type: joinType,
Table: clause.Table{Name: table.TableName()},
ON: clause.Where{Exprs: condToExpression(conds...)},
ON: clause.Where{Exprs: condToExpression(conds)},
})
return NewDO(d.db.Clauses(from))
}
Expand Down Expand Up @@ -247,31 +252,31 @@ func (d *DO) Save(value interface{}) error {
}

func (d *DO) First(dest interface{}, conds ...field.Expr) error {
return d.db.Clauses(toExpression(conds...)...).First(dest).Error
return d.db.Clauses(toExpression(conds)...).First(dest).Error
}

func (d *DO) Take(dest interface{}, conds ...field.Expr) error {
return d.db.Clauses(toExpression(conds...)...).Take(dest).Error
return d.db.Clauses(toExpression(conds)...).Take(dest).Error
}

func (d *DO) Last(dest interface{}, conds ...field.Expr) error {
return d.db.Clauses(toExpression(conds...)...).Last(dest).Error
return d.db.Clauses(toExpression(conds)...).Last(dest).Error
}

func (d *DO) Find(dest interface{}, conds ...field.Expr) error {
return d.db.Clauses(toExpression(conds...)...).Find(dest).Error
return d.db.Clauses(toExpression(conds)...).Find(dest).Error
}

func (d *DO) FindInBatches(dest interface{}, batchSize int, fc func(tx Dao, batch int) error) error {
return d.db.FindInBatches(dest, batchSize, func(tx *gorm.DB, batch int) error { return fc(NewDO(tx), batch) }).Error
}

func (d *DO) FirstOrInit(dest interface{}, conds ...field.Expr) error {
return d.db.Clauses(toExpression(conds...)...).FirstOrInit(dest).Error
return d.db.Clauses(toExpression(conds)...).FirstOrInit(dest).Error
}

func (d *DO) FirstOrCreate(dest interface{}, conds ...field.Expr) error {
return d.db.Clauses(toExpression(conds...)...).FirstOrCreate(dest).Error
return d.db.Clauses(toExpression(conds)...).FirstOrCreate(dest).Error
}

func (d *DO) Update(column field.Expr, value interface{}) error {
Expand Down Expand Up @@ -315,7 +320,7 @@ func (d *DO) UpdateColumns(values interface{}) error {
}

func (d *DO) Delete(value interface{}, conds ...field.Expr) error {
return d.db.Clauses(toExpression(conds...)...).Delete(value).Error
return d.db.Clauses(toExpression(conds)...).Delete(value).Error
}

func (d *DO) Count(count *int64) error {
Expand Down Expand Up @@ -366,15 +371,23 @@ func (d *DO) RollBackTo(name string) Dao {
return NewDO(d.db.RollbackTo(name))
}

func toExpression(conds ...field.Expr) []clause.Expression {
func toExpression(conds []field.Expr) []clause.Expression {
exprs := make([]clause.Expression, len(conds))
for i, cond := range conds {
exprs[i] = cond
}
return exprs
}

func condToExpression(conds ...Condition) []clause.Expression {
func hintToExpression(hs []hints.Hints) []clause.Expression {
exprs := make([]clause.Expression, len(hs))
for i, hint := range hs {
exprs[i] = hint
}
return exprs
}

func condToExpression(conds []Condition) []clause.Expression {
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
switch cond := cond.(type) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
golang.org/x/tools v0.1.5
gorm.io/driver/mysql v1.1.1
gorm.io/gorm v1.21.13
gorm.io/hints v0.0.0-20210809061251-a20b3c9afa2b
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfC
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -33,6 +36,12 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gorm.io/driver/mysql v1.1.1 h1:yr1bpyqiwuSPJ4aGGUX9nu46RHXlF8RASQVb1QQNcvo=
gorm.io/driver/mysql v1.1.1/go.mod h1:KdrTanmfLPPyAOeYGyG+UpDys7/7eeWT1zCq+oekYnU=
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.12/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.13 h1:JU5A4yVemRjdMndJ0oZU7VX+Nr2ICE3C60U5bgR6mHE=
gorm.io/gorm v1.21.13/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/hints v0.0.0-20210809061251-a20b3c9afa2b h1:asIGciWLhbjgrMwbN+3e+Bwom41q430iGSTyZCY4qz8=
gorm.io/hints v0.0.0-20210809061251-a20b3c9afa2b/go.mod h1:maPPgx/jalkq7ejIv2F0UmOkz9h/9Caj9nKeoLOzzjE=
7 changes: 7 additions & 0 deletions internal/template/tmpl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb3860a

Please sign in to comment.