v0.0.407 sq.Iterate
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
This commit is contained in:
parent
c1c8c64c76
commit
4a33986b6a
@ -1,5 +1,5 @@
|
|||||||
package goext
|
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"
|
||||||
|
@ -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) },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
48
sq/list.go
Normal file
48
sq/list.go
Normal file
@ -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)
|
||||||
|
}
|
@ -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) {
|
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{}
|
prepParams := PP{}
|
||||||
|
|
||||||
sortOrder := filter.Sort()
|
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) {
|
func Count(ctx context.Context, q Queryable, table string, filter PaginateFilter) (int, error) {
|
||||||
|
if filter == nil {
|
||||||
|
filter = NewEmptyPaginateFilter()
|
||||||
|
}
|
||||||
|
|
||||||
prepParams := PP{}
|
prepParams := PP{}
|
||||||
|
|
||||||
filterCond, joinCond, _ := filter.SQL(prepParams)
|
filterCond, joinCond, _ := filter.SQL(prepParams)
|
||||||
|
@ -333,3 +333,79 @@ func ScanAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode
|
|||||||
}
|
}
|
||||||
return res, nil
|
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 <sec>")
|
||||||
|
}
|
||||||
|
|
||||||
|
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 <mode>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if close {
|
||||||
|
err := strscan.rows.Close()
|
||||||
|
if err != nil {
|
||||||
|
return rcount, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return rcount, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rcount, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user