Files
jx-callback/business/model/dao/dao.go

120 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package dao
import (
"reflect"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"github.com/astaxie/beego/orm"
)
type DaoDB struct {
db orm.Ormer
}
// func GetRows(db *gorm.DB, inPtr interface{}, sql string, values ...interface{}) (err error) {
// if db == nil {
// db = gormdb.GetDB()
// }
// topTypeInfo := reflect.TypeOf(inPtr)
// if topTypeInfo.Kind() != reflect.Ptr {
// panic("SelectEntities inPtr should be slice ptr (*[]Type)")
// }
// typeInfo := topTypeInfo.Elem()
// if typeInfo.Kind() != reflect.Slice {
// panic("SelectEntities inPtr should be slice ptr (*[]Type)")
// }
// elmType := typeInfo.Elem()
// valueInfo := reflect.ValueOf(inPtr)
// rows, err := db.Raw(sql, values...).Rows()
// if err == nil {
// defer rows.Close()
// for rows.Next() {
// var value reflect.Value
// if elmType.Kind() == reflect.Ptr {
// value = reflect.New(elmType.Elem())
// } else {
// value = reflect.New(elmType)
// }
// db.ScanRows(rows, value.Interface())
// if elmType.Kind() != reflect.Ptr {
// value = value.Elem()
// }
// valueInfo.Elem().Set(reflect.Append(valueInfo.Elem(), value))
// }
// return nil
// }
// return err
// }
func GetDB() *DaoDB {
return &DaoDB{db: orm.NewOrm()}
}
func GetRow(db *DaoDB, inPtr interface{}, sql string, values ...interface{}) (err error) {
if db == nil {
db = GetDB()
}
return db.db.Raw(sql, values).QueryRow(inPtr)
}
func GetRows(db *DaoDB, inPtr interface{}, sql string, values ...interface{}) (err error) {
if db == nil {
db = GetDB()
}
_, err = db.db.Raw(sql, values).QueryRows(inPtr)
return err
}
func GetEntity(db *DaoDB, item interface{}, cols ...string) error {
if db == nil {
db = GetDB()
}
err := utils.CallFuncLogError(func() error {
return db.db.Read(item, cols...)
}, reflect.TypeOf(item).Name())
return err
}
func UpdateEntity(db *DaoDB, item interface{}, cols ...string) error {
if db == nil {
db = GetDB()
}
err := utils.CallFuncLogError(func() error {
_, err2 := db.db.Update(item, cols...)
return err2
}, reflect.TypeOf(item).Name())
return err
}
func UpdateEntityByKV(db *DaoDB, item interface{}, kvs map[string]interface{}, conditions map[string]interface{}) error {
if db == nil {
db = GetDB()
}
err := utils.CallFuncLogError(func() error {
qs := db.db.QueryTable(item)
if conditions == nil {
qs = qs.Filter("id", jxutils.GetObjFieldByName(item, "ID"))
} else {
for k, v := range conditions {
qs = qs.Filter(k, v)
}
}
_, err2 := qs.Update(kvs)
return err2
}, reflect.TypeOf(item).Name())
return err
}
func CreateEntity(db *DaoDB, item interface{}) error {
if db == nil {
db = GetDB()
}
err := utils.CallFuncLogError(func() error {
_, err2 := db.db.Insert(item) // todo 这里需要将ID赋值么
return err2
}, reflect.TypeOf(item).Name())
return err
}