From ee715d672d83932a6a20f810cc490d737fad01e6 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 13 Jun 2024 17:51:05 +0800 Subject: [PATCH] 1 --- dbpool.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 7 +++++++ 2 files changed, 55 insertions(+) diff --git a/dbpool.go b/dbpool.go index 82b6e1a..0206815 100644 --- a/dbpool.go +++ b/dbpool.go @@ -266,6 +266,54 @@ func (this *dbPool) PageQuery( }) } +func (this *dbPool) StreamPageQuery( + 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 + " " + } + //GetSysLog().Info("finalySql:%s", finalySql) + this.queryOne( + dataSource, + finalySql, + params, + func(err error, rows *DataSet) { + if err != nil { + cb(err, &pagination) + return + } + pagination.PreviousCursor = cursor + for rows.Next() { + if (rows.NumOfReaded() < int64(pageSize)) { + fillCb(rows) + } else if (rows.NumOfReaded() == int64(pageSize)) { + pagination.NextCursor = q5.ToInt64(rows.GetByName("idx")) + } else if (rows.NumOfReaded() > int64(pageSize)) { + pagination.Remaining = 1 + } + } + cb(nil, &pagination) + }) +} + func (this *dbPool) borrowConn(name string) *dataSource { tryCount := 0 for tryCount < 5 { diff --git a/types.go b/types.go index 83cedbc..59a22d7 100644 --- a/types.go +++ b/types.go @@ -29,12 +29,19 @@ type Pagination struct { Rows *DataSet } +type StreamPagination struct { + NextCursor int64 + PreviousCursor int64 + Remaining int32 +} + type HandlerFunc func(*Context) type GinHandlerFunc func(*gin.Context) type QueryResultCb func(error, *DataSet) type QueryOneCb func(error, *DataSet) type PageQueryCb func(error, *Pagination) +type SteamPageQueryCb func(error, *StreamPagination) type ExecResultCb func(error, int64, int64) type middleware struct {