aboutsummaryrefslogtreecommitdiffstats
path: root/models/db
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-01-15 10:19:25 +0800
committerGitHub <noreply@github.com>2024-01-15 02:19:25 +0000
commit70c4aad8e1cbc46b049b015dcd6f2e5be5a69e72 (patch)
tree5c9f049437c1a84a402aeef3be0cb4b95dea667e /models/db
parente5313248a8ed967a915f072d3991b6d046badf02 (diff)
downloadgitea-70c4aad8e1cbc46b049b015dcd6f2e5be5a69e72.tar.gz
gitea-70c4aad8e1cbc46b049b015dcd6f2e5be5a69e72.zip
Move more functions to db.Find (#28419)
Following #28220 This PR move more functions to use `db.Find`. --------- Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'models/db')
-rw-r--r--models/db/list.go61
-rw-r--r--models/db/paginator/paginator_test.go3
2 files changed, 31 insertions, 33 deletions
diff --git a/models/db/list.go b/models/db/list.go
index b2f932e89b..4aeaf3e084 100644
--- a/models/db/list.go
+++ b/models/db/list.go
@@ -21,17 +21,9 @@ const (
// Paginator is the base for different ListOptions types
type Paginator interface {
GetSkipTake() (skip, take int)
- GetStartEnd() (start, end int)
IsListAll() bool
}
-// GetPaginatedSession creates a paginated database session
-func GetPaginatedSession(p Paginator) *xorm.Session {
- skip, take := p.GetSkipTake()
-
- return x.Limit(take, skip)
-}
-
// SetSessionPagination sets pagination for a database session
func SetSessionPagination(sess Engine, p Paginator) *xorm.Session {
skip, take := p.GetSkipTake()
@@ -39,13 +31,6 @@ func SetSessionPagination(sess Engine, p Paginator) *xorm.Session {
return sess.Limit(take, skip)
}
-// SetEnginePagination sets pagination for a database engine
-func SetEnginePagination(e Engine, p Paginator) Engine {
- skip, take := p.GetSkipTake()
-
- return e.Limit(take, skip)
-}
-
// ListOptions options to paginate results
type ListOptions struct {
PageSize int
@@ -66,13 +51,6 @@ func (opts *ListOptions) GetSkipTake() (skip, take int) {
return (opts.Page - 1) * opts.PageSize, opts.PageSize
}
-// GetStartEnd returns the start and end of the ListOptions
-func (opts *ListOptions) GetStartEnd() (start, end int) {
- start, take := opts.GetSkipTake()
- end = start + take
- return start, end
-}
-
func (opts ListOptions) GetPage() int {
return opts.Page
}
@@ -135,11 +113,6 @@ func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
return opts.skip, opts.take
}
-// GetStartEnd returns the start and end values
-func (opts *AbsoluteListOptions) GetStartEnd() (start, end int) {
- return opts.skip, opts.skip + opts.take
-}
-
// FindOptions represents a find options
type FindOptions interface {
GetPage() int
@@ -148,15 +121,34 @@ type FindOptions interface {
ToConds() builder.Cond
}
+type JoinFunc func(sess Engine) error
+
+type FindOptionsJoin interface {
+ ToJoins() []JoinFunc
+}
+
type FindOptionsOrder interface {
ToOrders() string
}
// Find represents a common find function which accept an options interface
func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error) {
- sess := GetEngine(ctx).Where(opts.ToConds())
+ sess := GetEngine(ctx)
+
+ if joinOpt, ok := opts.(FindOptionsJoin); ok && len(joinOpt.ToJoins()) > 0 {
+ for _, joinFunc := range joinOpt.ToJoins() {
+ if err := joinFunc(sess); err != nil {
+ return nil, err
+ }
+ }
+ }
+
+ sess = sess.Where(opts.ToConds())
page, pageSize := opts.GetPage(), opts.GetPageSize()
- if !opts.IsListAll() && pageSize > 0 && page >= 1 {
+ if !opts.IsListAll() && pageSize > 0 {
+ if page == 0 {
+ page = 1
+ }
sess.Limit(pageSize, (page-1)*pageSize)
}
if newOpt, ok := opts.(FindOptionsOrder); ok && newOpt.ToOrders() != "" {
@@ -176,8 +168,17 @@ func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error) {
// Count represents a common count function which accept an options interface
func Count[T any](ctx context.Context, opts FindOptions) (int64, error) {
+ sess := GetEngine(ctx)
+ if joinOpt, ok := opts.(FindOptionsJoin); ok && len(joinOpt.ToJoins()) > 0 {
+ for _, joinFunc := range joinOpt.ToJoins() {
+ if err := joinFunc(sess); err != nil {
+ return 0, err
+ }
+ }
+ }
+
var object T
- return GetEngine(ctx).Where(opts.ToConds()).Count(&object)
+ return sess.Where(opts.ToConds()).Count(&object)
}
// FindAndCount represents a common findandcount function which accept an options interface
diff --git a/models/db/paginator/paginator_test.go b/models/db/paginator/paginator_test.go
index a1117fc7a4..20602212d9 100644
--- a/models/db/paginator/paginator_test.go
+++ b/models/db/paginator/paginator_test.go
@@ -52,11 +52,8 @@ func TestPaginator(t *testing.T) {
for _, c := range cases {
skip, take := c.Paginator.GetSkipTake()
- start, end := c.Paginator.GetStartEnd()
assert.Equal(t, c.Skip, skip)
assert.Equal(t, c.Take, take)
- assert.Equal(t, c.Start, start)
- assert.Equal(t, c.End, end)
}
}