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.

list.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package db
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/modules/setting"
  7. "xorm.io/builder"
  8. "xorm.io/xorm"
  9. )
  10. const (
  11. // DefaultMaxInSize represents default variables number on IN () in SQL
  12. DefaultMaxInSize = 50
  13. )
  14. // Paginator is the base for different ListOptions types
  15. type Paginator interface {
  16. GetSkipTake() (skip, take int)
  17. GetStartEnd() (start, end int)
  18. IsListAll() bool
  19. }
  20. // GetPaginatedSession creates a paginated database session
  21. func GetPaginatedSession(p Paginator) *xorm.Session {
  22. skip, take := p.GetSkipTake()
  23. return x.Limit(take, skip)
  24. }
  25. // SetSessionPagination sets pagination for a database session
  26. func SetSessionPagination(sess Engine, p Paginator) *xorm.Session {
  27. skip, take := p.GetSkipTake()
  28. return sess.Limit(take, skip)
  29. }
  30. // SetEnginePagination sets pagination for a database engine
  31. func SetEnginePagination(e Engine, p Paginator) Engine {
  32. skip, take := p.GetSkipTake()
  33. return e.Limit(take, skip)
  34. }
  35. // ListOptions options to paginate results
  36. type ListOptions struct {
  37. PageSize int
  38. Page int // start from 1
  39. ListAll bool // if true, then PageSize and Page will not be taken
  40. }
  41. var _ Paginator = &ListOptions{}
  42. // GetSkipTake returns the skip and take values
  43. func (opts *ListOptions) GetSkipTake() (skip, take int) {
  44. opts.SetDefaultValues()
  45. return (opts.Page - 1) * opts.PageSize, opts.PageSize
  46. }
  47. // GetStartEnd returns the start and end of the ListOptions
  48. func (opts *ListOptions) GetStartEnd() (start, end int) {
  49. start, take := opts.GetSkipTake()
  50. end = start + take
  51. return start, end
  52. }
  53. // IsListAll indicates PageSize and Page will be ignored
  54. func (opts *ListOptions) IsListAll() bool {
  55. return opts.ListAll
  56. }
  57. // SetDefaultValues sets default values
  58. func (opts *ListOptions) SetDefaultValues() {
  59. if opts.PageSize <= 0 {
  60. opts.PageSize = setting.API.DefaultPagingNum
  61. }
  62. if opts.PageSize > setting.API.MaxResponseItems {
  63. opts.PageSize = setting.API.MaxResponseItems
  64. }
  65. if opts.Page <= 0 {
  66. opts.Page = 1
  67. }
  68. }
  69. // AbsoluteListOptions absolute options to paginate results
  70. type AbsoluteListOptions struct {
  71. skip int
  72. take int
  73. }
  74. var _ Paginator = &AbsoluteListOptions{}
  75. // NewAbsoluteListOptions creates a list option with applied limits
  76. func NewAbsoluteListOptions(skip, take int) *AbsoluteListOptions {
  77. if skip < 0 {
  78. skip = 0
  79. }
  80. if take <= 0 {
  81. take = setting.API.DefaultPagingNum
  82. }
  83. if take > setting.API.MaxResponseItems {
  84. take = setting.API.MaxResponseItems
  85. }
  86. return &AbsoluteListOptions{skip, take}
  87. }
  88. // IsListAll will always return false
  89. func (opts *AbsoluteListOptions) IsListAll() bool {
  90. return false
  91. }
  92. // GetSkipTake returns the skip and take values
  93. func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
  94. return opts.skip, opts.take
  95. }
  96. // GetStartEnd returns the start and end values
  97. func (opts *AbsoluteListOptions) GetStartEnd() (start, end int) {
  98. return opts.skip, opts.skip + opts.take
  99. }
  100. // FindOptions represents a find options
  101. type FindOptions interface {
  102. Paginator
  103. ToConds() builder.Cond
  104. }
  105. // Find represents a common find function which accept an options interface
  106. func Find[T any](ctx context.Context, opts FindOptions, objects *[]T) error {
  107. sess := GetEngine(ctx).Where(opts.ToConds())
  108. if !opts.IsListAll() {
  109. sess.Limit(opts.GetSkipTake())
  110. }
  111. return sess.Find(objects)
  112. }
  113. // Count represents a common count function which accept an options interface
  114. func Count[T any](ctx context.Context, opts FindOptions, object T) (int64, error) {
  115. return GetEngine(ctx).Where(opts.ToConds()).Count(object)
  116. }
  117. // FindAndCount represents a common findandcount function which accept an options interface
  118. func FindAndCount[T any](ctx context.Context, opts FindOptions, objects *[]T) (int64, error) {
  119. sess := GetEngine(ctx).Where(opts.ToConds())
  120. if !opts.IsListAll() {
  121. sess.Limit(opts.GetSkipTake())
  122. }
  123. return sess.FindAndCount(objects)
  124. }