1
This commit is contained in:
parent
ee715d672d
commit
e64899e3e8
@ -103,10 +103,10 @@ func (this *dbFilter) GE(fieldName string, val string) DbQueryFilter {
|
||||
return f
|
||||
}
|
||||
|
||||
func (this *dbFilter) Custom(fieldName string, val string) DbQueryFilter {
|
||||
func (this *dbFilter) Custom(val string) DbQueryFilter {
|
||||
f := &PageQueryOne{
|
||||
cond: QC_CUSTOM,
|
||||
fieldName: fieldName,
|
||||
fieldName: "",
|
||||
val: val}
|
||||
return f
|
||||
}
|
||||
|
31
dbpool.go
31
dbpool.go
@ -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(
|
||||
dataSource string,
|
||||
perPage int32,
|
||||
@ -287,10 +313,11 @@ func (this *dbPool) StreamPageQuery(
|
||||
if filter != nil {
|
||||
finalySql += filter.GenSql()
|
||||
}
|
||||
finalySql += fmt.Sprintf(" LIMIT %d ", pageSize + 1)
|
||||
if orderBy != "" {
|
||||
finalySql += " " + orderBy + " "
|
||||
}
|
||||
//GetSysLog().Info("finalySql:%s", finalySql)
|
||||
GetSysLog().Info("finalySql:%s", finalySql)
|
||||
this.queryOne(
|
||||
dataSource,
|
||||
finalySql,
|
||||
@ -302,7 +329,7 @@ func (this *dbPool) StreamPageQuery(
|
||||
}
|
||||
pagination.PreviousCursor = cursor
|
||||
for rows.Next() {
|
||||
if (rows.NumOfReaded() < int64(pageSize)) {
|
||||
if (rows.NumOfReaded() <= int64(pageSize)) {
|
||||
fillCb(rows)
|
||||
} else if (rows.NumOfReaded() == int64(pageSize)) {
|
||||
pagination.NextCursor = q5.ToInt64(rows.GetByName("idx"))
|
||||
|
29
sysutils.go
29
sysutils.go
@ -3,6 +3,8 @@ package f5
|
||||
import (
|
||||
"q5"
|
||||
"os"
|
||||
"time"
|
||||
"sync"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -14,6 +16,8 @@ const (
|
||||
)
|
||||
|
||||
var serverEnv int32
|
||||
var globalLock sync.Mutex
|
||||
var globalLockHash map[string]*sync.Mutex
|
||||
|
||||
func IsTestEnv() bool {
|
||||
return serverEnv == TEST_ENV
|
||||
@ -45,6 +49,30 @@ func IsOrmErrRecordNotFound(err error) bool {
|
||||
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() {
|
||||
switch os.Getenv("SERVER_ENV") {
|
||||
case "TEST":
|
||||
@ -54,4 +82,5 @@ func init() {
|
||||
default:
|
||||
serverEnv = ONLINE_ENV
|
||||
}
|
||||
globalLockHash = map[string]*sync.Mutex{}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user