This commit is contained in:
aozhiwei 2024-07-04 01:30:20 +08:00
parent 5d16a05656
commit 1d17c0c75a
2 changed files with 73 additions and 0 deletions

View File

@ -408,6 +408,78 @@ func (this *dbPool) StreamPageQuery(
})
}
func (this *dbPool) StreamPageQuery1(
dataSource string,
pageSize int32,
cursor int64,
sql string,
params []string,
filter DbQueryFilter,
orderBy string,
cb SteamPageQueryCb,
fillCb func(*DataSet)) {
if (pageSize <= 0) {
pageSize = 1
}
if (pageSize > 1000) {
pageSize = 1000
}
var pagination StreamPagination
finalySql := sql
if filter != nil {
finalySql += filter.GenSql()
}
if orderBy != "" {
finalySql += " " + orderBy + " "
}
//finalySql += fmt.Sprintf(" LIMIT %d ", pageSize + 1)
//GetSysLog().Info("finalySql:%s", finalySql)
this.queryOne(
dataSource,
fmt.Sprintf("SELECT COUNT(*) FROM (%s) as t", finalySql),
params,
func(err error, rows *DataSet) {
if err != nil {
cb(err, &pagination)
return
}
var total int32
var totalPages int32
if rows != nil && rows.Next() {
total = q5.ToInt32(rows.GetByIndex(0))
totalPages = int32(math.Ceil(q5.ToFloat64(rows.GetByIndex(0)) /
float64(pageSize)))
}
if cursor <= 0 {
cursor = 1
}
start := pageSize * (int32(cursor) - 1)
limit := pageSize
this.query(
dataSource,
fmt.Sprintf("%s LIMIT %d, %d", finalySql, start, limit),
params,
func(err error, rows *DataSet) {
if err != nil {
cb(err, &pagination)
return
}
//pagination.Rows = rows
pagination.PreviousCursor = cursor
for rows.Next() {
fillCb(rows)
pagination.Count += 1
}
if int32(cursor) < totalPages {
pagination.NextCursor = cursor + 1
pagination.Remaining = 1
}
pagination.TotalCount = total
cb(nil, &pagination)
})
})
}
func (this *dbPool) borrowConn(name string) *dataSource {
tryCount := 0
for tryCount < 5 {

View File

@ -33,6 +33,7 @@ type StreamPagination struct {
PreviousCursor int64
Remaining int32
Count int32
TotalCount int32
}
type GinHandlerFunc func(*gin.Context)