From b784fa92cad8fcb9c2cab69d248cbd3829230510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Wed, 31 Mar 2021 16:42:26 +0800 Subject: [PATCH] aa --- business/model/dao/dao.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/business/model/dao/dao.go b/business/model/dao/dao.go index 2cd81ba95..4356535e5 100644 --- a/business/model/dao/dao.go +++ b/business/model/dao/dao.go @@ -139,6 +139,26 @@ func GetRow(db *DaoDB, inPtr interface{}, sql string, values ...interface{}) (er return err } +func GetRowTx(txDB orm.TxOrmer, inPtr interface{}, sql string, values ...interface{}) (err error) { + if txDB == nil { + return + } + typeInfo := reflect.TypeOf(inPtr) + if typeInfo.Kind() != reflect.Ptr { + return errors.New("inPtr must be ptr") + } + slice := reflect.New(reflect.SliceOf(typeInfo.Elem())) + if err = GetRowsTx(txDB, slice.Interface(), sql, values...); err == nil { + slice = slice.Elem() + if slice.Len() > 0 { + reflect.ValueOf(inPtr).Elem().Set(slice.Index(0)) + } else { + return orm.ErrNoRows + } + } + return err +} + func GetRows(db *DaoDB, inPtr interface{}, sql string, values ...interface{}) (err error) { if db == nil { db = GetDB() @@ -245,11 +265,8 @@ func GetLastTotalRowCount(db *DaoDB) int { func GetLastTotalRowCount2(db *DaoDB, txDB orm.TxOrmer) int { countInfo := &struct{ Ct int }{} - if err := txDB.Raw("SELECT FOUND_ROWS() ct", nil).QueryRow(countInfo); err == nil { + if err := GetRowTx(txDB, countInfo, "SELECT FOUND_ROWS() ct"); err == nil { return countInfo.Ct } - // if err := GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil { - // return countInfo.Ct - // } return 0 }