summaryrefslogtreecommitdiffstats
path: root/models/db/list_options.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/db/list_options.go')
-rw-r--r--models/db/list_options.go50
1 files changed, 49 insertions, 1 deletions
diff --git a/models/db/list_options.go b/models/db/list_options.go
index b9ee360b1b..2456f90f3b 100644
--- a/models/db/list_options.go
+++ b/models/db/list_options.go
@@ -4,8 +4,11 @@
package db
import (
+ "context"
+
"code.gitea.io/gitea/modules/setting"
+ "xorm.io/builder"
"xorm.io/xorm"
)
@@ -18,6 +21,7 @@ const (
type Paginator interface {
GetSkipTake() (skip, take int)
GetStartEnd() (start, end int)
+ IsListAll() bool
}
// GetPaginatedSession creates a paginated database session
@@ -44,9 +48,12 @@ func SetEnginePagination(e Engine, p Paginator) Engine {
// ListOptions options to paginate results
type ListOptions struct {
PageSize int
- Page int // start from 1
+ Page int // start from 1
+ ListAll bool // if true, then PageSize and Page will not be taken
}
+var _ Paginator = &ListOptions{}
+
// GetSkipTake returns the skip and take values
func (opts *ListOptions) GetSkipTake() (skip, take int) {
opts.SetDefaultValues()
@@ -60,6 +67,11 @@ func (opts *ListOptions) GetStartEnd() (start, end int) {
return start, end
}
+// IsListAll indicates PageSize and Page will be ignored
+func (opts *ListOptions) IsListAll() bool {
+ return opts.ListAll
+}
+
// SetDefaultValues sets default values
func (opts *ListOptions) SetDefaultValues() {
if opts.PageSize <= 0 {
@@ -79,6 +91,8 @@ type AbsoluteListOptions struct {
take int
}
+var _ Paginator = &AbsoluteListOptions{}
+
// NewAbsoluteListOptions creates a list option with applied limits
func NewAbsoluteListOptions(skip, take int) *AbsoluteListOptions {
if skip < 0 {
@@ -93,6 +107,11 @@ func NewAbsoluteListOptions(skip, take int) *AbsoluteListOptions {
return &AbsoluteListOptions{skip, take}
}
+// IsListAll will always return false
+func (opts *AbsoluteListOptions) IsListAll() bool {
+ return false
+}
+
// GetSkipTake returns the skip and take values
func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
return opts.skip, opts.take
@@ -102,3 +121,32 @@ func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
func (opts *AbsoluteListOptions) GetStartEnd() (start, end int) {
return opts.skip, opts.skip + opts.take
}
+
+// FindOptions represents a find options
+type FindOptions interface {
+ Paginator
+ ToConds() builder.Cond
+}
+
+// Find represents a common find function which accept an options interface
+func Find[T any](ctx context.Context, opts FindOptions, objects *[]T) error {
+ sess := GetEngine(ctx).Where(opts.ToConds())
+ if !opts.IsListAll() {
+ sess.Limit(opts.GetSkipTake())
+ }
+ return sess.Find(&objects)
+}
+
+// Count represents a common count function which accept an options interface
+func Count[T any](ctx context.Context, opts FindOptions, object T) (int64, error) {
+ return GetEngine(ctx).Where(opts.ToConds()).Count(object)
+}
+
+// FindAndCount represents a common findandcount function which accept an options interface
+func FindAndCount[T any](ctx context.Context, opts FindOptions, objects *[]T) (int64, error) {
+ sess := GetEngine(ctx).Where(opts.ToConds())
+ if !opts.IsListAll() {
+ sess.Limit(opts.GetSkipTake())
+ }
+ return sess.FindAndCount(&objects)
+}