diff --git a/go.mod b/go.mod index 4960a7e..4a1cfc0 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/google/uuid v1.5.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.6 // indirect - github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 2cc29c2..3e83146 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6K github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= diff --git a/goextVersion.go b/goextVersion.go index 398452d..df297d5 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.388" +const GoextVersion = "0.0.389" -const GoextVersionTimestamp = "2024-02-20T09:19:06+0100" +const GoextVersionTimestamp = "2024-02-21T16:10:28+0100" diff --git a/sq/filter.go b/sq/filter.go new file mode 100644 index 0000000..d04af70 --- /dev/null +++ b/sq/filter.go @@ -0,0 +1,49 @@ +package sq + +import ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken" + +type FilterSort struct { + Field string + Direction ct.SortDirection +} + +type PaginateFilter interface { + SQL(params PP) (filterClause string, joinClause string, joinTables []string) + Sort() []FilterSort +} + +type genericPaginateFilter struct { + sql func(params PP) (filterClause string, joinClause string, joinTables []string) + sort func() []FilterSort +} + +func (g genericPaginateFilter) SQL(params PP) (filterClause string, joinClause string, joinTables []string) { + return g.sql(params) +} + +func (g genericPaginateFilter) Sort() []FilterSort { + return g.sort() +} + +func NewPaginateFilter(sql func(params PP) (filterClause string, joinClause string, joinTables []string), sort []FilterSort) PaginateFilter { + return genericPaginateFilter{ + sql: func(params PP) (filterClause string, joinClause string, joinTables []string) { + return sql(params) + }, + sort: func() []FilterSort { + return sort + }, + } +} + +func NewSimplePaginateFilter(filterClause string, params PP, sort []FilterSort) PaginateFilter { + return genericPaginateFilter{ + sql: func(params PP) (filterClause string, joinClause string, joinTables []string) { + params.AddAll(params) + return filterClause, "", nil + }, + sort: func() []FilterSort { + return sort + }, + } +} diff --git a/sq/paginate.go b/sq/paginate.go index 8bdfe99..d5dea99 100644 --- a/sq/paginate.go +++ b/sq/paginate.go @@ -3,22 +3,11 @@ 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{} diff --git a/sq/params.go b/sq/params.go index 6f0242f..2e4c915 100644 --- a/sq/params.go +++ b/sq/params.go @@ -20,6 +20,12 @@ func (pp *PP) Add(v any) string { return id } +func (pp *PP) AddAll(other PP) { + for id, v := range other { + (*pp)[id] = v + } +} + func PPID() string { return "p_" + langext.RandBase62(8) }