go项目脚手架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sorbet/pkg/db/update_builder.go

156 lines
3.5 KiB

package db
import (
"errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type UpdateBuilder[T any] struct {
db *gorm.DB
selects []string
omits []string
onConflict *clause.OnConflict
expr *Expr
}
func NewUpdateBuilder[T any](db *gorm.DB) *UpdateBuilder[T] {
return &UpdateBuilder[T]{expr: &Expr{}, db: db}
}
func (u *UpdateBuilder[T]) Select(columns ...string) *UpdateBuilder[T] {
u.selects = append(u.selects, columns...)
return u
}
func (u *UpdateBuilder[T]) Omit(columns ...string) *UpdateBuilder[T] {
u.omits = append(u.omits, columns...)
return u
}
func (u *UpdateBuilder[T]) Eq(col string, val any) *UpdateBuilder[T] {
u.expr.Eq(col, val)
return u
}
func (u *UpdateBuilder[T]) Neq(col string, val any) *UpdateBuilder[T] {
u.expr.Neq(col, val)
return u
}
func (u *UpdateBuilder[T]) Lt(col string, val any) *UpdateBuilder[T] {
u.expr.Lt(col, val)
return u
}
func (u *UpdateBuilder[T]) Lte(col string, val any) *UpdateBuilder[T] {
u.expr.Lte(col, val)
return u
}
func (u *UpdateBuilder[T]) Gt(col string, val any) *UpdateBuilder[T] {
u.expr.Gt(col, val)
return u
}
func (u *UpdateBuilder[T]) Gte(col string, val any) *UpdateBuilder[T] {
u.expr.Gte(col, val)
return u
}
func (u *UpdateBuilder[T]) Between(col string, less, more any) *UpdateBuilder[T] {
u.expr.Between(col, less, more)
return u
}
func (u *UpdateBuilder[T]) NotBetween(col string, less, more any) *UpdateBuilder[T] {
u.expr.NotBetween(col, less, more)
return u
}
func (u *UpdateBuilder[T]) IsNull(col string) *UpdateBuilder[T] {
u.expr.IsNull(col)
return u
}
func (u *UpdateBuilder[T]) NotNull(col string) *UpdateBuilder[T] {
u.expr.NotNull(col)
return u
}
func (u *UpdateBuilder[T]) Like(col, tpl string) *UpdateBuilder[T] {
u.expr.Like(col, tpl)
return u
}
func (u *UpdateBuilder[T]) NotLike(col, tpl string) *UpdateBuilder[T] {
u.expr.NotLike(col, tpl)
return u
}
func (u *UpdateBuilder[T]) In(col string, values ...any) *UpdateBuilder[T] {
u.expr.In(col, values...)
return u
}
func (u *UpdateBuilder[T]) NotIn(col string, values ...any) *UpdateBuilder[T] {
u.expr.NotIn(col, values...)
return u
}
func (u *UpdateBuilder[T]) When(condition bool, then func(ex *Expr), elses ...func(ex *Expr)) *UpdateBuilder[T] {
u.expr.When(condition, then, elses...)
return u
}
func (u *UpdateBuilder[T]) Or(or func(ex *Expr)) *UpdateBuilder[T] {
u.expr.Or(or)
return u
}
func (u *UpdateBuilder[T]) And(and func(ex *Expr)) *UpdateBuilder[T] {
u.expr.And(and)
return u
}
func (u *UpdateBuilder[T]) Not(not func(ex *Expr)) *UpdateBuilder[T] {
u.expr.Not(not)
return u
}
func (u *UpdateBuilder[T]) OnConflict(conflict clause.OnConflict) *UpdateBuilder[T] {
if u.onConflict == nil {
u.onConflict = &conflict
} else {
u.onConflict.Columns = conflict.Columns
u.onConflict.Where = conflict.Where
u.onConflict.TargetWhere = conflict.TargetWhere
u.onConflict.OnConstraint = conflict.OnConstraint
u.onConflict.DoNothing = conflict.DoNothing
u.onConflict.DoUpdates = conflict.DoUpdates
u.onConflict.UpdateAll = conflict.UpdateAll
}
return u
}
func (u *UpdateBuilder[T]) Scopes(tx *gorm.DB) *gorm.DB {
if u.selects != nil {
tx = tx.Select(u.selects)
}
if u.omits != nil {
tx = tx.Omit(u.omits...)
}
return u.expr.Scopes(tx)
}
func (u *UpdateBuilder[T]) Commit(values map[string]any) (int64, error) {
var entity T
res := u.db.Model(&entity).Scopes(u.Scopes).Updates(values)
if err := res.Error; err != nil {
return res.RowsAffected, err
}
if res.RowsAffected == 0 {
return 0, errors.New("no record updated")
}
return res.RowsAffected, nil
}