This commit is contained in:
aozhiwei 2024-06-16 12:57:35 +08:00
parent ee715d672d
commit e64899e3e8
4 changed files with 60 additions and 5 deletions

View File

@ -103,10 +103,10 @@ func (this *dbFilter) GE(fieldName string, val string) DbQueryFilter {
return f return f
} }
func (this *dbFilter) Custom(fieldName string, val string) DbQueryFilter { func (this *dbFilter) Custom(val string) DbQueryFilter {
f := &PageQueryOne{ f := &PageQueryOne{
cond: QC_CUSTOM, cond: QC_CUSTOM,
fieldName: fieldName, fieldName: "",
val: val} val: val}
return f return f
} }

View File

@ -215,6 +215,32 @@ func (this *dbPool) Upsert(
}) })
} }
func (this *dbPool) UpsertEx(
dataSource string,
tblName string,
whereKv [][]string,
updateKv [][]string,
insertKv [][]string,
cb ExecResultCb,
updateCb func(*DataSet) bool) {
this.OrmSelectOne(dataSource, tblName, whereKv,
func(err error, ds *DataSet) {
if err != nil {
cb(err, 0, 0)
return
}
if ds.Next() {
if len(updateKv) > 0 {
if updateCb(ds) {
this.Update(dataSource, tblName, whereKv, updateKv, cb)
}
}
} else {
this.Insert(dataSource, tblName, insertKv, cb)
}
})
}
func (this *dbPool) PageQuery( func (this *dbPool) PageQuery(
dataSource string, dataSource string,
perPage int32, perPage int32,
@ -287,10 +313,11 @@ func (this *dbPool) StreamPageQuery(
if filter != nil { if filter != nil {
finalySql += filter.GenSql() finalySql += filter.GenSql()
} }
finalySql += fmt.Sprintf(" LIMIT %d ", pageSize + 1)
if orderBy != "" { if orderBy != "" {
finalySql += " " + orderBy + " " finalySql += " " + orderBy + " "
} }
//GetSysLog().Info("finalySql:%s", finalySql) GetSysLog().Info("finalySql:%s", finalySql)
this.queryOne( this.queryOne(
dataSource, dataSource,
finalySql, finalySql,
@ -302,7 +329,7 @@ func (this *dbPool) StreamPageQuery(
} }
pagination.PreviousCursor = cursor pagination.PreviousCursor = cursor
for rows.Next() { for rows.Next() {
if (rows.NumOfReaded() < int64(pageSize)) { if (rows.NumOfReaded() <= int64(pageSize)) {
fillCb(rows) fillCb(rows)
} else if (rows.NumOfReaded() == int64(pageSize)) { } else if (rows.NumOfReaded() == int64(pageSize)) {
pagination.NextCursor = q5.ToInt64(rows.GetByName("idx")) pagination.NextCursor = q5.ToInt64(rows.GetByName("idx"))

View File

@ -3,6 +3,8 @@ package f5
import ( import (
"q5" "q5"
"os" "os"
"time"
"sync"
"errors" "errors"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -14,6 +16,8 @@ const (
) )
var serverEnv int32 var serverEnv int32
var globalLock sync.Mutex
var globalLockHash map[string]*sync.Mutex
func IsTestEnv() bool { func IsTestEnv() bool {
return serverEnv == TEST_ENV return serverEnv == TEST_ENV
@ -45,6 +49,30 @@ func IsOrmErrRecordNotFound(err error) bool {
return errors.Is(err, gorm.ErrRecordNotFound) return errors.Is(err, gorm.ErrRecordNotFound)
} }
func AllocLock(key string) *sync.Mutex {
var l *sync.Mutex
globalLock.Lock()
if p, ok := globalLockHash[key]; ok {
l = p
} else {
l = new(sync.Mutex)
globalLockHash[key] = l
}
globalLock.Unlock()
go func () {
time.Sleep(time.Second * 30)
globalLock.Lock()
delete(globalLockHash, key)
globalLock.Unlock()
}()
l.Lock()
return l
}
func ReleaseLock(l *sync.Mutex) {
l.Unlock()
}
func init() { func init() {
switch os.Getenv("SERVER_ENV") { switch os.Getenv("SERVER_ENV") {
case "TEST": case "TEST":
@ -54,4 +82,5 @@ func init() {
default: default:
serverEnv = ONLINE_ENV serverEnv = ONLINE_ENV
} }
globalLockHash = map[string]*sync.Mutex{}
} }

View File

@ -2,7 +2,6 @@ package f5
import ( import (
"q5" "q5"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )