Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
f1f91f4cfa
|
|||
2afb265ea4
|
@@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.370"
|
||||
const GoextVersion = "0.0.372"
|
||||
|
||||
const GoextVersionTimestamp = "2024-01-13T14:10:25+0100"
|
||||
const GoextVersionTimestamp = "2024-01-13T21:36:47+0100"
|
||||
|
@@ -5,7 +5,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type Filter interface {
|
||||
type MongoFilter interface {
|
||||
FilterQuery() mongo.Pipeline
|
||||
Sort() bson.D
|
||||
}
|
||||
@@ -23,6 +23,6 @@ func (d dynamicFilter) Sort() bson.D {
|
||||
return d.sort
|
||||
}
|
||||
|
||||
func CreateFilter(pipeline mongo.Pipeline, sort bson.D) Filter {
|
||||
func CreateFilter(pipeline mongo.Pipeline, sort bson.D) MongoFilter {
|
||||
return dynamicFilter{pipeline: pipeline, sort: sort}
|
||||
}
|
||||
|
@@ -46,7 +46,7 @@ func (db *database) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Resul
|
||||
for _, v := range db.lstr {
|
||||
err := v.PreExec(ctx, nil, &sqlstr, &prep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, exerr.Wrap(err, "failed to call SQL pre-exec listener").Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func (db *database) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Ro
|
||||
for _, v := range db.lstr {
|
||||
err := v.PreQuery(ctx, nil, &sqlstr, &prep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, exerr.Wrap(err, "failed to call SQL pre-query listener").Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
|
||||
}
|
||||
}
|
||||
|
||||
|
126
sq/paginate.go
Normal file
126
sq/paginate.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package sq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
pag "gogs.mikescher.com/BlackForestBytes/goext/pagination"
|
||||
)
|
||||
|
||||
type PaginateFilter interface {
|
||||
SQL(params PP) (filterClause string, joinClause string, joinTables []string)
|
||||
Sort() []FilterSort
|
||||
}
|
||||
|
||||
type FilterSort struct {
|
||||
Field string
|
||||
Direction ct.SortDirection
|
||||
}
|
||||
|
||||
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) {
|
||||
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
|
||||
sqlQueryCount := "SELECT " + "COUNT(*)" + " FROM " + table + " " + joinCond + " WHERE ( " + filterCond + " ) "
|
||||
|
||||
rows, err := q.Query(ctx, sqlQueryData, prepParams)
|
||||
if err != nil {
|
||||
return nil, pag.Pagination{}, exerr.Wrap(err, "failed to list paginated entries from DB").Str("table", table).Any("filter", filter).Int("page", page).Any("limit", limit).Build()
|
||||
}
|
||||
|
||||
entities, err := ScanAll[TData](ctx, q, rows, scanMode, scanSec, true)
|
||||
if err != nil {
|
||||
return nil, pag.Pagination{}, exerr.Wrap(err, "failed to decode paginated entries from DB").Str("table", table).Int("page", page).Any("limit", limit).Str("scanMode", string(scanMode)).Str("scanSec", string(scanSec)).Build()
|
||||
}
|
||||
|
||||
if page == 1 && (limit == nil || len(entities) <= *limit) {
|
||||
return entities, pag.Pagination{
|
||||
Page: 1,
|
||||
Limit: langext.Coalesce(limit, len(entities)),
|
||||
TotalPages: 1,
|
||||
TotalItems: len(entities),
|
||||
CurrentPageCount: 1,
|
||||
}, nil
|
||||
} else {
|
||||
|
||||
countRows, err := q.Query(ctx, sqlQueryCount, prepParams)
|
||||
if err != nil {
|
||||
return nil, pag.Pagination{}, exerr.Wrap(err, "failed to query total-count of paginated entries from DB").Str("table", table).Build()
|
||||
}
|
||||
|
||||
if !countRows.Next() {
|
||||
return nil, pag.Pagination{}, exerr.New(exerr.TypeSQLQuery, "SQL COUNT(*) query returned no rows").Str("table", table).Any("filter", filter).Build() //TODO TypeSQLDecode
|
||||
}
|
||||
|
||||
var countRes int
|
||||
err = countRows.Scan(&countRes)
|
||||
if err != nil {
|
||||
return nil, pag.Pagination{}, exerr.Wrap(err, "failed to decode total-count of paginated entries from DB").Str("table", table).Build()
|
||||
}
|
||||
|
||||
if len(entities) > *limit {
|
||||
entities = entities[:*limit]
|
||||
}
|
||||
|
||||
paginationObj := pag.Pagination{
|
||||
Page: page,
|
||||
Limit: langext.Coalesce(limit, countRes),
|
||||
TotalPages: pag.CalcPaginationTotalPages(countRes, langext.Coalesce(limit, countRes)),
|
||||
TotalItems: countRes,
|
||||
CurrentPageCount: len(entities),
|
||||
}
|
||||
|
||||
return entities, paginationObj, nil
|
||||
}
|
||||
}
|
||||
|
||||
func Count(ctx context.Context, q Queryable, table string, filter PaginateFilter) (int, error) {
|
||||
prepParams := PP{}
|
||||
|
||||
filterCond, joinCond, _ := filter.SQL(prepParams)
|
||||
|
||||
sqlQueryCount := "SELECT " + "COUNT(*)" + " FROM " + table + " " + joinCond + " WHERE ( " + filterCond + " )"
|
||||
|
||||
countRows, err := q.Query(ctx, sqlQueryCount, prepParams)
|
||||
if err != nil {
|
||||
return 0, exerr.Wrap(err, "failed to query count of entries from DB").Str("table", table).Build()
|
||||
}
|
||||
|
||||
if !countRows.Next() {
|
||||
return 0, exerr.New(exerr.TypeSQLQuery, "SQL COUNT(*) query returned no rows").Str("table", table).Any("filter", filter).Build() //TODO TypeSQLDecode
|
||||
}
|
||||
|
||||
var countRes int
|
||||
err = countRows.Scan(&countRes)
|
||||
if err != nil {
|
||||
return 0, exerr.Wrap(err, "failed to decode count of entries from DB").Str("table", table).Build()
|
||||
}
|
||||
|
||||
return countRes, nil
|
||||
}
|
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
)
|
||||
|
||||
@@ -48,7 +49,7 @@ func (tx *transaction) Rollback() error {
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreTxRollback(tx.id)
|
||||
if err != nil {
|
||||
return err
|
||||
return exerr.Wrap(err, "failed to call SQL pre-rollback listener").Int("tx.id", int(tx.id)).Build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +70,7 @@ func (tx *transaction) Commit() error {
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreTxCommit(tx.id)
|
||||
if err != nil {
|
||||
return err
|
||||
return exerr.Wrap(err, "failed to call SQL pre-commit listener").Int("tx.id", int(tx.id)).Build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +92,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreExec(ctx, langext.Ptr(tx.id), &sqlstr, &prep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, exerr.Wrap(err, "failed to call SQL pre-exec listener").Int("tx.id", int(tx.id)).Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +107,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, exerr.Wrap(err, "Failed to [exec] sql statement").Int("tx.id", int(tx.id)).Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
@@ -116,7 +117,7 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreQuery(ctx, langext.Ptr(tx.id), &sqlstr, &prep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, exerr.Wrap(err, "failed to call SQL pre-query listener").Int("tx.id", int(tx.id)).Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +132,7 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, exerr.Wrap(err, "Failed to [query] sql statement").Int("tx.id", int(tx.id)).Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@ import (
|
||||
pag "gogs.mikescher.com/BlackForestBytes/goext/pagination"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.Filter, page int, limit *int) ([]TData, pag.Pagination, error) {
|
||||
func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.MongoFilter, page int, limit *int) ([]TData, pag.Pagination, error) {
|
||||
type totalCountResult struct {
|
||||
Count int `bson:"count"`
|
||||
}
|
||||
|
Reference in New Issue
Block a user