summaryrefslogtreecommitdiffstats
path: root/models/db
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-11-24 11:49:41 +0800
committerGitHub <noreply@github.com>2023-11-24 03:49:41 +0000
commitdf1e7d0067bb39913eb681ccc920649884fb1938 (patch)
tree2419feab5c28658adb7f71878df646bdc9bdc50e /models/db
parentd24a8223ce1e47a0c9b103aae07f67c3112ca048 (diff)
downloadgitea-df1e7d0067bb39913eb681ccc920649884fb1938.tar.gz
gitea-df1e7d0067bb39913eb681ccc920649884fb1938.zip
Use db.Find instead of writing methods for every object (#28084)
For those simple objects, it's unnecessary to write the find and count methods again and again.
Diffstat (limited to 'models/db')
-rw-r--r--models/db/context.go5
-rw-r--r--models/db/list.go80
-rw-r--r--models/db/list_test.go13
3 files changed, 77 insertions, 21 deletions
diff --git a/models/db/context.go b/models/db/context.go
index 521857fae8..45765ef7d3 100644
--- a/models/db/context.go
+++ b/models/db/context.go
@@ -264,3 +264,8 @@ func inTransaction(ctx context.Context) (*xorm.Session, bool) {
return nil, false
}
}
+
+func Exists[T any](ctx context.Context, opts FindOptions) (bool, error) {
+ var bean T
+ return GetEngine(ctx).Where(opts.ToConds()).Exist(&bean)
+}
diff --git a/models/db/list.go b/models/db/list.go
index 9fb4d0741f..b2f932e89b 100644
--- a/models/db/list.go
+++ b/models/db/list.go
@@ -14,7 +14,8 @@ import (
const (
// DefaultMaxInSize represents default variables number on IN () in SQL
- DefaultMaxInSize = 50
+ DefaultMaxInSize = 50
+ defaultFindSliceSize = 10
)
// Paginator is the base for different ListOptions types
@@ -52,7 +53,12 @@ type ListOptions struct {
ListAll bool // if true, then PageSize and Page will not be taken
}
-var _ Paginator = &ListOptions{}
+var ListOptionsAll = ListOptions{ListAll: true}
+
+var (
+ _ Paginator = &ListOptions{}
+ _ FindOptions = ListOptions{}
+)
// GetSkipTake returns the skip and take values
func (opts *ListOptions) GetSkipTake() (skip, take int) {
@@ -67,8 +73,16 @@ func (opts *ListOptions) GetStartEnd() (start, end int) {
return start, end
}
+func (opts ListOptions) GetPage() int {
+ return opts.Page
+}
+
+func (opts ListOptions) GetPageSize() int {
+ return opts.PageSize
+}
+
// IsListAll indicates PageSize and Page will be ignored
-func (opts *ListOptions) IsListAll() bool {
+func (opts ListOptions) IsListAll() bool {
return opts.ListAll
}
@@ -85,6 +99,10 @@ func (opts *ListOptions) SetDefaultValues() {
}
}
+func (opts ListOptions) ToConds() builder.Cond {
+ return builder.NewCond()
+}
+
// AbsoluteListOptions absolute options to paginate results
type AbsoluteListOptions struct {
skip int
@@ -124,29 +142,63 @@ func (opts *AbsoluteListOptions) GetStartEnd() (start, end int) {
// FindOptions represents a find options
type FindOptions interface {
- Paginator
+ GetPage() int
+ GetPageSize() int
+ IsListAll() bool
ToConds() builder.Cond
}
+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, objects *[]T) error {
+func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error) {
sess := GetEngine(ctx).Where(opts.ToConds())
- if !opts.IsListAll() {
- sess.Limit(opts.GetSkipTake())
+ page, pageSize := opts.GetPage(), opts.GetPageSize()
+ if !opts.IsListAll() && pageSize > 0 && page >= 1 {
+ sess.Limit(pageSize, (page-1)*pageSize)
+ }
+ if newOpt, ok := opts.(FindOptionsOrder); ok && newOpt.ToOrders() != "" {
+ sess.OrderBy(newOpt.ToOrders())
+ }
+
+ findPageSize := defaultFindSliceSize
+ if pageSize > 0 {
+ findPageSize = pageSize
+ }
+ objects := make([]*T, 0, findPageSize)
+ if err := sess.Find(&objects); err != nil {
+ return nil, err
}
- return sess.Find(objects)
+ return objects, nil
}
// 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)
+func Count[T any](ctx context.Context, opts FindOptions) (int64, error) {
+ var object T
+ 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) {
+func FindAndCount[T any](ctx context.Context, opts FindOptions) ([]*T, int64, error) {
sess := GetEngine(ctx).Where(opts.ToConds())
- if !opts.IsListAll() {
- sess.Limit(opts.GetSkipTake())
+ page, pageSize := opts.GetPage(), opts.GetPageSize()
+ if !opts.IsListAll() && pageSize > 0 && page >= 1 {
+ sess.Limit(pageSize, (page-1)*pageSize)
+ }
+ if newOpt, ok := opts.(FindOptionsOrder); ok && newOpt.ToOrders() != "" {
+ sess.OrderBy(newOpt.ToOrders())
+ }
+
+ findPageSize := defaultFindSliceSize
+ if pageSize > 0 {
+ findPageSize = pageSize
+ }
+ objects := make([]*T, 0, findPageSize)
+ cnt, err := sess.FindAndCount(&objects)
+ if err != nil {
+ return nil, 0, err
}
- return sess.FindAndCount(objects)
+ return objects, cnt, nil
}
diff --git a/models/db/list_test.go b/models/db/list_test.go
index b923dc9c22..45194611f8 100644
--- a/models/db/list_test.go
+++ b/models/db/list_test.go
@@ -18,11 +18,11 @@ type mockListOptions struct {
db.ListOptions
}
-func (opts *mockListOptions) IsListAll() bool {
+func (opts mockListOptions) IsListAll() bool {
return true
}
-func (opts *mockListOptions) ToConds() builder.Cond {
+func (opts mockListOptions) ToConds() builder.Cond {
return builder.NewCond()
}
@@ -37,17 +37,16 @@ func TestFind(t *testing.T) {
assert.NotEmpty(t, repoUnitCount)
opts := mockListOptions{}
- var repoUnits []repo_model.RepoUnit
- err = db.Find(db.DefaultContext, &opts, &repoUnits)
+ repoUnits, err := db.Find[repo_model.RepoUnit](db.DefaultContext, opts)
assert.NoError(t, err)
assert.Len(t, repoUnits, repoUnitCount)
- cnt, err := db.Count(db.DefaultContext, &opts, new(repo_model.RepoUnit))
+ cnt, err := db.Count[repo_model.RepoUnit](db.DefaultContext, opts)
assert.NoError(t, err)
assert.EqualValues(t, repoUnitCount, cnt)
- repoUnits = make([]repo_model.RepoUnit, 0, 10)
- newCnt, err := db.FindAndCount(db.DefaultContext, &opts, &repoUnits)
+ repoUnits, newCnt, err := db.FindAndCount[repo_model.RepoUnit](db.DefaultContext, opts)
assert.NoError(t, err)
assert.EqualValues(t, cnt, newCnt)
+ assert.Len(t, repoUnits, repoUnitCount)
}