aboutsummaryrefslogtreecommitdiffstats
path: root/models/db
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2024-01-30 03:37:24 +0100
committerGitHub <noreply@github.com>2024-01-30 02:37:24 +0000
commit27d4c11ec3bd1658c07d0bb9b0b7f3712aafb0bb (patch)
treeac48555170af5901ec37feb0834b9365464c4e86 /models/db
parent8ef53c871bcb5c007b3640a347c7868585c9e4de (diff)
downloadgitea-27d4c11ec3bd1658c07d0bb9b0b7f3712aafb0bb.tar.gz
gitea-27d4c11ec3bd1658c07d0bb9b0b7f3712aafb0bb.zip
Fix joins in `db.Find(AndCount)` (#28978)
Diffstat (limited to 'models/db')
-rw-r--r--models/db/list.go32
1 files changed, 21 insertions, 11 deletions
diff --git a/models/db/list.go b/models/db/list.go
index 4aeaf3e084..5c005a0350 100644
--- a/models/db/list.go
+++ b/models/db/list.go
@@ -133,17 +133,21 @@ type FindOptionsOrder interface {
// 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)
+ sess := GetEngine(ctx).Where(opts.ToConds())
- if joinOpt, ok := opts.(FindOptionsJoin); ok && len(joinOpt.ToJoins()) > 0 {
+ if joinOpt, ok := opts.(FindOptionsJoin); ok {
for _, joinFunc := range joinOpt.ToJoins() {
if err := joinFunc(sess); err != nil {
return nil, err
}
}
}
+ if orderOpt, ok := opts.(FindOptionsOrder); ok {
+ if order := orderOpt.ToOrders(); order != "" {
+ sess.OrderBy(order)
+ }
+ }
- sess = sess.Where(opts.ToConds())
page, pageSize := opts.GetPage(), opts.GetPageSize()
if !opts.IsListAll() && pageSize > 0 {
if page == 0 {
@@ -151,9 +155,6 @@ func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error) {
}
sess.Limit(pageSize, (page-1)*pageSize)
}
- if newOpt, ok := opts.(FindOptionsOrder); ok && newOpt.ToOrders() != "" {
- sess.OrderBy(newOpt.ToOrders())
- }
findPageSize := defaultFindSliceSize
if pageSize > 0 {
@@ -168,8 +169,8 @@ 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 {
+ sess := GetEngine(ctx).Where(opts.ToConds())
+ if joinOpt, ok := opts.(FindOptionsJoin); ok {
for _, joinFunc := range joinOpt.ToJoins() {
if err := joinFunc(sess); err != nil {
return 0, err
@@ -178,7 +179,7 @@ func Count[T any](ctx context.Context, opts FindOptions) (int64, error) {
}
var object T
- return sess.Where(opts.ToConds()).Count(&object)
+ return sess.Count(&object)
}
// FindAndCount represents a common findandcount function which accept an options interface
@@ -188,8 +189,17 @@ func FindAndCount[T any](ctx context.Context, opts FindOptions) ([]*T, int64, er
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())
+ if joinOpt, ok := opts.(FindOptionsJoin); ok {
+ for _, joinFunc := range joinOpt.ToJoins() {
+ if err := joinFunc(sess); err != nil {
+ return nil, 0, err
+ }
+ }
+ }
+ if orderOpt, ok := opts.(FindOptionsOrder); ok {
+ if order := orderOpt.ToOrders(); order != "" {
+ sess.OrderBy(order)
+ }
}
findPageSize := defaultFindSliceSize