mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.8 KiB
153 lines
3.8 KiB
5 years ago
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
5 years ago
|
|
||
3 years ago
|
package db
|
||
5 years ago
|
|
||
|
import (
|
||
2 years ago
|
"context"
|
||
|
|
||
5 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
|
|
||
2 years ago
|
"xorm.io/builder"
|
||
5 years ago
|
"xorm.io/xorm"
|
||
|
)
|
||
|
|
||
2 years ago
|
const (
|
||
|
// DefaultMaxInSize represents default variables number on IN () in SQL
|
||
|
DefaultMaxInSize = 50
|
||
|
)
|
||
|
|
||
3 years ago
|
// Paginator is the base for different ListOptions types
|
||
|
type Paginator interface {
|
||
|
GetSkipTake() (skip, take int)
|
||
|
GetStartEnd() (start, end int)
|
||
2 years ago
|
IsListAll() bool
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
// GetPaginatedSession creates a paginated database session
|
||
|
func GetPaginatedSession(p Paginator) *xorm.Session {
|
||
3 years ago
|
skip, take := p.GetSkipTake()
|
||
5 years ago
|
|
||
3 years ago
|
return x.Limit(take, skip)
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
// SetSessionPagination sets pagination for a database session
|
||
3 years ago
|
func SetSessionPagination(sess Engine, p Paginator) *xorm.Session {
|
||
3 years ago
|
skip, take := p.GetSkipTake()
|
||
5 years ago
|
|
||
3 years ago
|
return sess.Limit(take, skip)
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
// SetEnginePagination sets pagination for a database engine
|
||
|
func SetEnginePagination(e Engine, p Paginator) Engine {
|
||
3 years ago
|
skip, take := p.GetSkipTake()
|
||
|
|
||
|
return e.Limit(take, skip)
|
||
|
}
|
||
|
|
||
|
// ListOptions options to paginate results
|
||
|
type ListOptions struct {
|
||
|
PageSize int
|
||
2 years ago
|
Page int // start from 1
|
||
|
ListAll bool // if true, then PageSize and Page will not be taken
|
||
3 years ago
|
}
|
||
5 years ago
|
|
||
2 years ago
|
var _ Paginator = &ListOptions{}
|
||
|
|
||
3 years ago
|
// GetSkipTake returns the skip and take values
|
||
|
func (opts *ListOptions) GetSkipTake() (skip, take int) {
|
||
3 years ago
|
opts.SetDefaultValues()
|
||
3 years ago
|
return (opts.Page - 1) * opts.PageSize, opts.PageSize
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
// GetStartEnd returns the start and end of the ListOptions
|
||
4 years ago
|
func (opts *ListOptions) GetStartEnd() (start, end int) {
|
||
3 years ago
|
start, take := opts.GetSkipTake()
|
||
|
end = start + take
|
||
2 years ago
|
return start, end
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
// IsListAll indicates PageSize and Page will be ignored
|
||
|
func (opts *ListOptions) IsListAll() bool {
|
||
|
return opts.ListAll
|
||
|
}
|
||
|
|
||
3 years ago
|
// SetDefaultValues sets default values
|
||
|
func (opts *ListOptions) SetDefaultValues() {
|
||
5 years ago
|
if opts.PageSize <= 0 {
|
||
|
opts.PageSize = setting.API.DefaultPagingNum
|
||
|
}
|
||
|
if opts.PageSize > setting.API.MaxResponseItems {
|
||
5 years ago
|
opts.PageSize = setting.API.MaxResponseItems
|
||
|
}
|
||
|
if opts.Page <= 0 {
|
||
|
opts.Page = 1
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
// AbsoluteListOptions absolute options to paginate results
|
||
|
type AbsoluteListOptions struct {
|
||
|
skip int
|
||
|
take int
|
||
|
}
|
||
|
|
||
2 years ago
|
var _ Paginator = &AbsoluteListOptions{}
|
||
|
|
||
3 years ago
|
// 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}
|
||
|
}
|
||
|
|
||
2 years ago
|
// IsListAll will always return false
|
||
|
func (opts *AbsoluteListOptions) IsListAll() bool {
|
||
|
return false
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetSkipTake returns the skip and take values
|
||
|
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
|
||
|
}
|
||
2 years ago
|
|
||
|
// 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())
|
||
|
}
|
||
2 years ago
|
return sess.Find(objects)
|
||
2 years ago
|
}
|
||
|
|
||
|
// 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())
|
||
|
}
|
||
2 years ago
|
return sess.FindAndCount(objects)
|
||
2 years ago
|
}
|