From 4a33986b6a94c5dce187f8a6a90492a83b0822cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 11 Mar 2024 16:40:41 +0100 Subject: [PATCH] v0.0.407 sq.Iterate --- goextVersion.go | 4 +-- sq/filter.go | 7 +++++ sq/list.go | 48 +++++++++++++++++++++++++++++++ sq/paginate.go | 8 ++++++ sq/scanner.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 sq/list.go diff --git a/goextVersion.go b/goextVersion.go index 45a97df..62cd23c 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.406" +const GoextVersion = "0.0.407" -const GoextVersionTimestamp = "2024-03-10T16:44:21+0100" +const GoextVersionTimestamp = "2024-03-11T16:40:41+0100" diff --git a/sq/filter.go b/sq/filter.go index fa6b97c..c486292 100644 --- a/sq/filter.go +++ b/sq/filter.go @@ -47,3 +47,10 @@ func NewSimplePaginateFilter(filterClause string, filterParams PP, sort []Filter }, } } + +func NewEmptyPaginateFilter() PaginateFilter { + return genericPaginateFilter{ + sql: func(params PP) (string, string, []string) { return "1=1", "", nil }, + sort: func() []FilterSort { return make([]FilterSort, 0) }, + } +} diff --git a/sq/list.go b/sq/list.go new file mode 100644 index 0000000..affee37 --- /dev/null +++ b/sq/list.go @@ -0,0 +1,48 @@ +package sq + +import ( + "context" + "fmt" + "gogs.mikescher.com/BlackForestBytes/goext/exerr" +) + +func Iterate[TData any](ctx context.Context, q Queryable, table string, filter PaginateFilter, scanMode StructScanMode, scanSec StructScanSafety, page int, limit *int, consumer func(v TData) error) (int, error) { + if filter == nil { + filter = NewEmptyPaginateFilter() + } + + prepParams := PP{} + + sortOrder := filter.Sort() + sortCond := "" + if len(sortOrder) > 0 { + sortCond = "ORDER BY " + for i, v := range sortOrder { + if i > 0 { + sortCond += ", " + } + sortCond += v.Field + " " + string(v.Direction) + } + } + + pageCond := "" + if limit != nil { + pageCond += fmt.Sprintf("LIMIT :%s OFFSET :%s", prepParams.Add(*limit+1), prepParams.Add(*limit*(page-1))) + } + + filterCond, joinCond, joinTables := filter.SQL(prepParams) + + selectCond := table + ".*" + for _, v := range joinTables { + selectCond += ", " + v + ".*" + } + + sqlQueryData := "SELECT " + selectCond + " FROM " + table + " " + joinCond + " WHERE ( " + filterCond + " ) " + sortCond + " " + pageCond + + rows, err := q.Query(ctx, sqlQueryData, prepParams) + if err != nil { + return 0, exerr.Wrap(err, "failed to list paginated entries from DB").Str("table", table).Any("filter", filter).Int("page", page).Any("limit", limit).Build() + } + + return IterateAll[TData](ctx, q, rows, scanMode, scanSec, true, consumer) +} diff --git a/sq/paginate.go b/sq/paginate.go index d5dea99..e0fac2f 100644 --- a/sq/paginate.go +++ b/sq/paginate.go @@ -9,6 +9,10 @@ import ( ) func Paginate[TData any](ctx context.Context, q Queryable, table string, filter PaginateFilter, scanMode StructScanMode, scanSec StructScanSafety, page int, limit *int) ([]TData, pag.Pagination, error) { + if filter == nil { + filter = NewEmptyPaginateFilter() + } + prepParams := PP{} sortOrder := filter.Sort() @@ -90,6 +94,10 @@ func Paginate[TData any](ctx context.Context, q Queryable, table string, filter } func Count(ctx context.Context, q Queryable, table string, filter PaginateFilter) (int, error) { + if filter == nil { + filter = NewEmptyPaginateFilter() + } + prepParams := PP{} filterCond, joinCond, _ := filter.SQL(prepParams) diff --git a/sq/scanner.go b/sq/scanner.go index 9ff33ef..7a422ab 100644 --- a/sq/scanner.go +++ b/sq/scanner.go @@ -333,3 +333,79 @@ func ScanAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode } return res, nil } + +func IterateAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool, consumer func(v TData) error) (int, error) { + var strscan *StructScanner + + if sec == Safe { + strscan = NewStructScanner(rows, false) + var data TData + err := strscan.Start(&data) + if err != nil { + return 0, err + } + } else if sec == Unsafe { + strscan = NewStructScanner(rows, true) + var data TData + err := strscan.Start(&data) + if err != nil { + return 0, err + } + } else { + return 0, errors.New("unknown value for ") + } + + rcount := 0 + + for rows.Next() { + + if err := ctx.Err(); err != nil { + return rcount, err + } + + if mode == SModeFast { + var data TData + err := strscan.StructScanBase(&data) + if err != nil { + return rcount, err + } + + err = consumer(data) + if err != nil { + return rcount, exerr.Wrap(err, "").Build() + } + + rcount++ + + } else if mode == SModeExtended { + var data TData + err := strscan.StructScanExt(q, &data) + if err != nil { + return rcount, err + } + + err = consumer(data) + if err != nil { + return rcount, exerr.Wrap(err, "").Build() + } + + rcount++ + + } else { + return rcount, errors.New("unknown value for ") + } + } + + if close { + err := strscan.rows.Close() + if err != nil { + return rcount, err + } + } + + if err := rows.Err(); err != nil { + return rcount, err + } + + return rcount, nil +}