验证中...
Languages: Go
Categories: 数据库相关
Latest update 2019-05-22 15:29
xorm 扩展
Raw Copy
var engine *xorm.Engine
type Page struct {
Total int `json:"total"` //总页数
Current int `json:"current"` //当前页码
Size int `json:"size"` //页记录数
Pages int `json:"pages"` //实际页记录数
Records interface{} `json:"records"`
}
type Join struct {
Oper string `json:"oper"`
TableName interface{} `json:"tableName"`
Condition string `json:"condition"`
Args []interface{} `json:"args"`
}
type BaseDao struct {
}
func (dao BaseDao) SelectOneBy(m interface{}) error {
return dao.SelectOneByCondition(m, "")
}
func (dao BaseDao) Exist(m interface{}) (bool, error) {
dao.setDefaultDelFlag(m, "0")
return dao.Exist(m)
}
func (dao BaseDao) setDefaultDelFlag(m interface{}, val string) {
rv := reflect.Indirect(reflect.ValueOf(m))
delFlag := rv.FieldByName("DelFlag").String()
if delFlag == "" {
rv.FieldByName("DelFlag").Set(reflect.ValueOf(val))
}
}
func (dao BaseDao) setDefaultCreateTime(m interface{}) {
rv := reflect.Indirect(reflect.ValueOf(m))
delFlag := rv.FieldByName("CreateTime").String()
if delFlag == "" {
rv.FieldByName("CreateTime").Set(reflect.ValueOf(time.Now()))
}
}
func (dao BaseDao) setDefaultUpdateTime(m interface{}) {
rv := reflect.Indirect(reflect.ValueOf(m))
delFlag := rv.FieldByName("UpdateTime").String()
if delFlag == "" {
rv.FieldByName("UpdateTime").Set(reflect.ValueOf(time.Now()))
}
}
func (dao BaseDao) SelectOneByCondition(m interface{}, query string, args ...interface{}) error {
dao.setDefaultDelFlag(m, "0")
_, err := engine.Where(query, args).Get(m)
if err != nil {
return err
}
return nil
}
func (dao BaseDao) SelectPage(p *Page, m interface{}, selectCaluse string, alias string, join []Join, obm map[string]bool, queryOrargs ...interface{}) (*Page, error) {
var err error
pageIsNil := false
pageIsNil = p == nil
if !pageIsNil {
if p.Current <= 0 || p.Size <= 0 {
return nil, errors.New("Page params is error")
}
} else {
p = &Page{}
}
if selectCaluse != "" && (strings.Index(selectCaluse, "SELECT") >= 0 || strings.Index(selectCaluse, "select") >= 0) {
panic("Select caluse has word 'SELECT' or 'select'")
}
dao.setDefaultDelFlag(m, "0")
s := engine.NewSession()
defer s.Close()
rv := reflect.Indirect(reflect.ValueOf(m))
rt := rv.Type()
mlv := reflect.MakeSlice(reflect.SliceOf(rt), 0, p.Size)
ml := reflect.New(mlv.Type())
//select
if selectCaluse != "" {
s.Select(selectCaluse)
}
//table name
s = s.Table(m).Alias(alias)
//join
if len(join) > 0 {
for _, v := range join {
s = s.Join(v.Oper, v.TableName, v.Condition, v.Args...)
}
}
//condition
if len(queryOrargs) > 0 {
s = s.Where(queryOrargs[0], queryOrargs[1:]...)
}
//order by
if len(obm) > 0 {
for k, v := range obm {
if v {
s = s.Desc(k)
} else {
s = s.Asc(k)
}
}
}
//page
if !pageIsNil {
sp := p.Current - 1
s = s.Limit(p.Pages, sp)
}
//find
mlst := ml.Interface()
rs := int64(0)
if !pageIsNil {
rs, err = s.FindAndCount(mlst, m)
//set page info
p.Total = int(rs)
p.Pages = p.Total / p.Size
if p.Total%p.Size != 0 {
p.Pages = p.Pages + 1
}
} else {
err = s.Find(mlst, m)
}
if err != nil {
return nil, err
}
//set records
p.Records = mlst
return p, nil
}
func (dao BaseDao) SelectList(m interface{}, selectCaluse string, alias string, join []Join, obm map[string]bool, queryOrargs ...interface{}) (interface{}, error) {
p, err := dao.SelectPage(nil, m, selectCaluse, alias, join, obm, queryOrargs)
if err != nil {
return nil, err
}
return p.Records, nil
}
func (dao BaseDao) toXormSession(s interface{}) (*xorm.Session, error) {
if s == nil {
return nil, nil
}
switch s.(type) {
case *xorm.Session:
return s.(*xorm.Session), nil
default:
return nil, errors.New("Input param type error")
}
}
func (dao BaseDao) Insert(ss interface{}, m interface{}) error {
var err error
s, err := dao.toXormSession(ss)
if err != nil {
return err
}
if s == nil {
s = engine.NewSession()
defer s.Close()
}
dao.setDefaultCreateTime(m)
dao.setDefaultUpdateTime(m)
dao.setDefaultDelFlag(m, "0")
_, err = s.Insert(m)
if err != nil {
return err
}
return nil
}
func (dao BaseDao) Update(ss interface{}, m interface{}, c interface{}, queryOrargs ...interface{}) error {
var err error
s, err := dao.toXormSession(ss)
if err != nil {
return err
}
if s == nil {
s = engine.NewSession()
defer s.Close()
}
dao.setDefaultUpdateTime(m)
if len(queryOrargs) > 0 {
s = s.Where(queryOrargs[0], queryOrargs[1:]...)
}
_, err = s.Update(m, c)
if err != nil {
return err
}
return nil
}
func (dao BaseDao) Delete(ss interface{}, c interface{}, queryOrargs ...interface{}) error {
rv := reflect.Indirect(reflect.ValueOf(c))
rt := rv.Type()
mv := reflect.New(rt)
m := mv.Interface()
dao.setDefaultDelFlag(m, "1")
dao.setDefaultDelFlag(c, "0")
return dao.Update(ss, m, c, queryOrargs)
}
func (dao BaseDao) BeginTran() (interface{}, error) {
s := engine.NewSession()
err := s.Begin()
if err != nil {
return nil, err
}
return s, nil
}
func (dao BaseDao) Commit(s interface{}) error {
switch s.(type) {
case *xorm.Session:
return s.(*xorm.Session).Commit()
default:
return errors.New("Input param type error")
}
}
func (dao BaseDao) Rollback(s interface{}) error {
switch s.(type) {
case *xorm.Session:
return s.(*xorm.Session).Rollback()
default:
return errors.New("Input param type error")
}
}
func (dao BaseDao) Close(s interface{}) {
switch s.(type) {
case *xorm.Session:
s.(*xorm.Session).Close()
break
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_you_jiang_zheng_wen Zheng_wen_close