- try to fix SQL_CALC_FOUND_ROWS (use transaction)

This commit is contained in:
gazebo
2018-09-15 23:04:46 +08:00
parent 04e105d8d1
commit a8ae37c641
3 changed files with 5 additions and 5 deletions

View File

@@ -10,7 +10,6 @@ import (
"git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
) )
type SkuNamesInfo struct { type SkuNamesInfo struct {
@@ -266,13 +265,10 @@ func GetSkuNames(keyword string, params map[string]interface{}, offset, pageSize
} }
sqlParams = append(sqlParams, pageSize, offset) sqlParams = append(sqlParams, pageSize, offset)
skuNamesInfo = &SkuNamesInfo{} skuNamesInfo = &SkuNamesInfo{}
globals.SugarLogger.Debug(sqlData) dao.Begin(db) // todo 这里用事务的原因是SQL_CALC_FOUND_ROWS会出错
globals.SugarLogger.Debug(utils.Format4Output(sqlParams, false))
dao.Begin(db)
if err = dao.GetRows(db, &skuNamesInfo.SkuNames, sqlData, sqlParams...); err == nil { if err = dao.GetRows(db, &skuNamesInfo.SkuNames, sqlData, sqlParams...); err == nil {
countInfo := &struct{ Ct int }{} countInfo := &struct{ Ct int }{}
if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil { if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil {
globals.SugarLogger.Debug(utils.Format4Output(countInfo, false))
skuNamesInfo.TotalCount = countInfo.Ct skuNamesInfo.TotalCount = countInfo.Ct
for _, skuName := range skuNamesInfo.SkuNames { for _, skuName := range skuNamesInfo.SkuNames {
if skuName.SkusStr != "" { if skuName.SkusStr != "" {

View File

@@ -183,12 +183,14 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i
sqlParams = append(sqlParams, pageSize, offset) sqlParams = append(sqlParams, pageSize, offset)
retVal = &StoresInfo{} retVal = &StoresInfo{}
db := dao.GetDB() db := dao.GetDB()
dao.Begin(db)
if err = dao.GetRows(db, &retVal.Stores, sql, sqlParams...); err == nil { if err = dao.GetRows(db, &retVal.Stores, sql, sqlParams...); err == nil {
countInfo := &struct{ Ct int }{} countInfo := &struct{ Ct int }{}
if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil { if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil {
retVal.TotalCount = countInfo.Ct retVal.TotalCount = countInfo.Ct
} }
} }
dao.Commit(db)
return retVal, err return retVal, err
} }

View File

@@ -155,6 +155,7 @@ func GetStoreSkus(storeID int, keyword string, params map[string]interface{}, of
sqlParams = append(sqlParams, pageSize, offset) sqlParams = append(sqlParams, pageSize, offset)
skuNamesInfo = &StoreSkuNamesInfo{} skuNamesInfo = &StoreSkuNamesInfo{}
// globals.SugarLogger.Debug(sqlData) // globals.SugarLogger.Debug(sqlData)
dao.Begin(db)
if err = dao.GetRows(db, &skuNamesInfo.SkuNames, sqlData, sqlParams...); err == nil { if err = dao.GetRows(db, &skuNamesInfo.SkuNames, sqlData, sqlParams...); err == nil {
countInfo := &struct{ Ct int }{} countInfo := &struct{ Ct int }{}
if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil { if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil {
@@ -168,6 +169,7 @@ func GetStoreSkus(storeID int, keyword string, params map[string]interface{}, of
} }
} }
} }
dao.Commit(db)
return skuNamesInfo, err return skuNamesInfo, err
} }