diff --git a/dbpool.go b/dbpool.go index b9931b3..1faf999 100644 --- a/dbpool.go +++ b/dbpool.go @@ -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 { diff --git a/types.go b/types.go index 7ca5e2e..b95b388 100644 --- a/types.go +++ b/types.go @@ -33,6 +33,7 @@ type StreamPagination struct { PreviousCursor int64 Remaining int32 Count int32 + TotalCount int32 } type GinHandlerFunc func(*gin.Context)