2020-01-24 22:00:29 +03:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2020-01-24 22:00:29 +03:00
2021-09-24 14:32:56 +03:00
package db
2020-01-24 22:00:29 +03:00
import (
2023-01-18 00:03:44 +03:00
"context"
2020-01-24 22:00:29 +03:00
"code.gitea.io/gitea/modules/setting"
2023-01-18 00:03:44 +03:00
"xorm.io/builder"
2020-01-24 22:00:29 +03:00
"xorm.io/xorm"
)
2022-06-13 12:37:59 +03:00
const (
// DefaultMaxInSize represents default variables number on IN () in SQL
2023-11-24 06:49:41 +03:00
DefaultMaxInSize = 50
defaultFindSliceSize = 10
2022-06-13 12:37:59 +03:00
)
2021-09-14 20:48:27 +03:00
// Paginator is the base for different ListOptions types
type Paginator interface {
GetSkipTake ( ) ( skip , take int )
2023-01-18 00:03:44 +03:00
IsListAll ( ) bool
2020-01-24 22:00:29 +03:00
}
2021-09-24 14:32:56 +03:00
// SetSessionPagination sets pagination for a database session
2021-11-21 18:41:00 +03:00
func SetSessionPagination ( sess Engine , p Paginator ) * xorm . Session {
2021-09-14 20:48:27 +03:00
skip , take := p . GetSkipTake ( )
2020-01-24 22:00:29 +03:00
2021-09-14 20:48:27 +03:00
return sess . Limit ( take , skip )
2020-01-24 22:00:29 +03:00
}
2021-09-14 20:48:27 +03:00
// ListOptions options to paginate results
type ListOptions struct {
PageSize int
2023-01-18 00:03:44 +03:00
Page int // start from 1
ListAll bool // if true, then PageSize and Page will not be taken
2021-09-14 20:48:27 +03:00
}
2020-01-24 22:00:29 +03:00
2023-11-24 06:49:41 +03:00
var ListOptionsAll = ListOptions { ListAll : true }
var (
_ Paginator = & ListOptions { }
_ FindOptions = ListOptions { }
)
2023-01-18 00:03:44 +03:00
2021-09-14 20:48:27 +03:00
// GetSkipTake returns the skip and take values
func ( opts * ListOptions ) GetSkipTake ( ) ( skip , take int ) {
2021-09-24 14:32:56 +03:00
opts . SetDefaultValues ( )
2021-09-14 20:48:27 +03:00
return ( opts . Page - 1 ) * opts . PageSize , opts . PageSize
2020-01-24 22:00:29 +03:00
}
2023-11-24 06:49:41 +03:00
func ( opts ListOptions ) GetPage ( ) int {
return opts . Page
}
func ( opts ListOptions ) GetPageSize ( ) int {
return opts . PageSize
}
2023-01-18 00:03:44 +03:00
// IsListAll indicates PageSize and Page will be ignored
2023-11-24 06:49:41 +03:00
func ( opts ListOptions ) IsListAll ( ) bool {
2023-01-18 00:03:44 +03:00
return opts . ListAll
}
2021-09-24 14:32:56 +03:00
// SetDefaultValues sets default values
func ( opts * ListOptions ) SetDefaultValues ( ) {
2020-06-10 19:23:22 +03:00
if opts . PageSize <= 0 {
opts . PageSize = setting . API . DefaultPagingNum
}
if opts . PageSize > setting . API . MaxResponseItems {
2020-01-24 22:00:29 +03:00
opts . PageSize = setting . API . MaxResponseItems
}
if opts . Page <= 0 {
opts . Page = 1
}
}
2021-09-14 20:48:27 +03:00
2023-11-24 06:49:41 +03:00
func ( opts ListOptions ) ToConds ( ) builder . Cond {
return builder . NewCond ( )
}
2021-09-14 20:48:27 +03:00
// AbsoluteListOptions absolute options to paginate results
type AbsoluteListOptions struct {
skip int
take int
}
2023-01-18 00:03:44 +03:00
var _ Paginator = & AbsoluteListOptions { }
2021-09-14 20:48:27 +03:00
// NewAbsoluteListOptions creates a list option with applied limits
func NewAbsoluteListOptions ( skip , take int ) * AbsoluteListOptions {
if skip < 0 {
skip = 0
}
if take <= 0 {
take = setting . API . DefaultPagingNum
}
if take > setting . API . MaxResponseItems {
take = setting . API . MaxResponseItems
}
return & AbsoluteListOptions { skip , take }
}
2023-01-18 00:03:44 +03:00
// IsListAll will always return false
func ( opts * AbsoluteListOptions ) IsListAll ( ) bool {
return false
}
2021-09-14 20:48:27 +03:00
// GetSkipTake returns the skip and take values
func ( opts * AbsoluteListOptions ) GetSkipTake ( ) ( skip , take int ) {
return opts . skip , opts . take
}
2023-01-18 00:03:44 +03:00
// FindOptions represents a find options
type FindOptions interface {
2023-11-24 06:49:41 +03:00
GetPage ( ) int
GetPageSize ( ) int
IsListAll ( ) bool
2023-01-18 00:03:44 +03:00
ToConds ( ) builder . Cond
}
2024-01-15 05:19:25 +03:00
type JoinFunc func ( sess Engine ) error
type FindOptionsJoin interface {
ToJoins ( ) [ ] JoinFunc
}
2023-11-24 06:49:41 +03:00
type FindOptionsOrder interface {
ToOrders ( ) string
}
2023-01-18 00:03:44 +03:00
// Find represents a common find function which accept an options interface
2023-11-24 06:49:41 +03:00
func Find [ T any ] ( ctx context . Context , opts FindOptions ) ( [ ] * T , error ) {
2024-01-30 05:37:24 +03:00
sess := GetEngine ( ctx ) . Where ( opts . ToConds ( ) )
2024-01-15 05:19:25 +03:00
2024-01-30 05:37:24 +03:00
if joinOpt , ok := opts . ( FindOptionsJoin ) ; ok {
2024-01-15 05:19:25 +03:00
for _ , joinFunc := range joinOpt . ToJoins ( ) {
if err := joinFunc ( sess ) ; err != nil {
return nil , err
}
}
}
2024-01-30 05:37:24 +03:00
if orderOpt , ok := opts . ( FindOptionsOrder ) ; ok {
if order := orderOpt . ToOrders ( ) ; order != "" {
sess . OrderBy ( order )
}
}
2024-01-15 05:19:25 +03:00
2023-11-24 06:49:41 +03:00
page , pageSize := opts . GetPage ( ) , opts . GetPageSize ( )
2024-01-15 05:19:25 +03:00
if ! opts . IsListAll ( ) && pageSize > 0 {
if page == 0 {
page = 1
}
2023-11-24 06:49:41 +03:00
sess . Limit ( pageSize , ( page - 1 ) * pageSize )
}
findPageSize := defaultFindSliceSize
if pageSize > 0 {
findPageSize = pageSize
}
objects := make ( [ ] * T , 0 , findPageSize )
if err := sess . Find ( & objects ) ; err != nil {
return nil , err
2023-01-18 00:03:44 +03:00
}
2023-11-24 06:49:41 +03:00
return objects , nil
2023-01-18 00:03:44 +03:00
}
// Count represents a common count function which accept an options interface
2023-11-24 06:49:41 +03:00
func Count [ T any ] ( ctx context . Context , opts FindOptions ) ( int64 , error ) {
2024-01-30 05:37:24 +03:00
sess := GetEngine ( ctx ) . Where ( opts . ToConds ( ) )
if joinOpt , ok := opts . ( FindOptionsJoin ) ; ok {
2024-01-15 05:19:25 +03:00
for _ , joinFunc := range joinOpt . ToJoins ( ) {
if err := joinFunc ( sess ) ; err != nil {
return 0 , err
}
}
}
2023-11-24 06:49:41 +03:00
var object T
2024-01-30 05:37:24 +03:00
return sess . Count ( & object )
2023-01-18 00:03:44 +03:00
}
// FindAndCount represents a common findandcount function which accept an options interface
2023-11-24 06:49:41 +03:00
func FindAndCount [ T any ] ( ctx context . Context , opts FindOptions ) ( [ ] * T , int64 , error ) {
2023-01-18 00:03:44 +03:00
sess := GetEngine ( ctx ) . Where ( opts . ToConds ( ) )
2023-11-24 06:49:41 +03:00
page , pageSize := opts . GetPage ( ) , opts . GetPageSize ( )
if ! opts . IsListAll ( ) && pageSize > 0 && page >= 1 {
sess . Limit ( pageSize , ( page - 1 ) * pageSize )
}
2024-01-30 05:37:24 +03:00
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 )
}
2023-11-24 06:49:41 +03:00
}
findPageSize := defaultFindSliceSize
if pageSize > 0 {
findPageSize = pageSize
}
objects := make ( [ ] * T , 0 , findPageSize )
cnt , err := sess . FindAndCount ( & objects )
if err != nil {
return nil , 0 , err
2023-01-18 00:03:44 +03:00
}
2023-11-24 06:49:41 +03:00
return objects , cnt , nil
2023-01-18 00:03:44 +03:00
}