Skip to content

Commit

Permalink
don't expose db functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Sep 24, 2019
1 parent 1dcd33c commit 46e236f
Showing 1 changed file with 0 additions and 40 deletions.
40 changes: 0 additions & 40 deletions models/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,11 @@

package models

import "xorm.io/builder"

// DBContext represents a db context
type DBContext struct {
e Engine
}

// Insert inserts a object to database
func (ctx *DBContext) Insert(obj interface{}) error {
_, err := ctx.e.Insert(obj)
return err
}

// LoadByID loads record from database according id, if it's not exist return ErrNotExist
func (ctx *DBContext) LoadByID(id int64, obj interface{}) error {
has, err := ctx.e.ID(id).Get(obj)
if err != nil {
return err
} else if !has {
return ErrNotExist{ID: id}
}
return nil
}

// FindByConditions loads records by conditions
func (ctx *DBContext) FindByConditions(conds builder.Cond, objs interface{}) error {
return ctx.e.Where(conds).Find(objs)
}

// DeleteByID deletes a object by id
func (ctx *DBContext) DeleteByID(id int64, obj interface{}) error {
_, err := ctx.e.ID(id).NoAutoCondition().Delete(obj)
return err
}

// UpdateByID updates a record
func (ctx *DBContext) UpdateByID(id int64, obj interface{}, cols ...string) error {
sess := ctx.e.ID(id)
if len(cols) > 0 {
sess.Cols(cols...)
}
_, err := sess.Update(obj)
return err
}

// WithContext represents executing database operations
func WithContext(f func(ctx DBContext) error) error {
return f(DBContext{x})
Expand Down

0 comments on commit 46e236f

Please sign in to comment.