@@ -21,31 +21,16 @@ const (
2121// Paginator is the base for different ListOptions types
2222type Paginator interface {
2323 GetSkipTake () (skip , take int )
24- GetStartEnd () (start , end int )
2524 IsListAll () bool
2625}
2726
28- // GetPaginatedSession creates a paginated database session
29- func GetPaginatedSession (p Paginator ) * xorm.Session {
30- skip , take := p .GetSkipTake ()
31-
32- return x .Limit (take , skip )
33- }
34-
3527// SetSessionPagination sets pagination for a database session
3628func SetSessionPagination (sess Engine , p Paginator ) * xorm.Session {
3729 skip , take := p .GetSkipTake ()
3830
3931 return sess .Limit (take , skip )
4032}
4133
42- // SetEnginePagination sets pagination for a database engine
43- func SetEnginePagination (e Engine , p Paginator ) Engine {
44- skip , take := p .GetSkipTake ()
45-
46- return e .Limit (take , skip )
47- }
48-
4934// ListOptions options to paginate results
5035type ListOptions struct {
5136 PageSize int
@@ -66,13 +51,6 @@ func (opts *ListOptions) GetSkipTake() (skip, take int) {
6651 return (opts .Page - 1 ) * opts .PageSize , opts .PageSize
6752}
6853
69- // GetStartEnd returns the start and end of the ListOptions
70- func (opts * ListOptions ) GetStartEnd () (start , end int ) {
71- start , take := opts .GetSkipTake ()
72- end = start + take
73- return start , end
74- }
75-
7654func (opts ListOptions ) GetPage () int {
7755 return opts .Page
7856}
@@ -135,11 +113,6 @@ func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
135113 return opts .skip , opts .take
136114}
137115
138- // GetStartEnd returns the start and end values
139- func (opts * AbsoluteListOptions ) GetStartEnd () (start , end int ) {
140- return opts .skip , opts .skip + opts .take
141- }
142-
143116// FindOptions represents a find options
144117type FindOptions interface {
145118 GetPage () int
@@ -148,15 +121,34 @@ type FindOptions interface {
148121 ToConds () builder.Cond
149122}
150123
124+ type JoinFunc func (sess Engine ) error
125+
126+ type FindOptionsJoin interface {
127+ ToJoins () []JoinFunc
128+ }
129+
151130type FindOptionsOrder interface {
152131 ToOrders () string
153132}
154133
155134// Find represents a common find function which accept an options interface
156135func Find [T any ](ctx context.Context , opts FindOptions ) ([]* T , error ) {
157- sess := GetEngine (ctx ).Where (opts .ToConds ())
136+ sess := GetEngine (ctx )
137+
138+ if joinOpt , ok := opts .(FindOptionsJoin ); ok && len (joinOpt .ToJoins ()) > 0 {
139+ for _ , joinFunc := range joinOpt .ToJoins () {
140+ if err := joinFunc (sess ); err != nil {
141+ return nil , err
142+ }
143+ }
144+ }
145+
146+ sess = sess .Where (opts .ToConds ())
158147 page , pageSize := opts .GetPage (), opts .GetPageSize ()
159- if ! opts .IsListAll () && pageSize > 0 && page >= 1 {
148+ if ! opts .IsListAll () && pageSize > 0 {
149+ if page == 0 {
150+ page = 1
151+ }
160152 sess .Limit (pageSize , (page - 1 )* pageSize )
161153 }
162154 if newOpt , ok := opts .(FindOptionsOrder ); ok && newOpt .ToOrders () != "" {
@@ -176,8 +168,17 @@ func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error) {
176168
177169// Count represents a common count function which accept an options interface
178170func Count [T any ](ctx context.Context , opts FindOptions ) (int64 , error ) {
171+ sess := GetEngine (ctx )
172+ if joinOpt , ok := opts .(FindOptionsJoin ); ok && len (joinOpt .ToJoins ()) > 0 {
173+ for _ , joinFunc := range joinOpt .ToJoins () {
174+ if err := joinFunc (sess ); err != nil {
175+ return 0 , err
176+ }
177+ }
178+ }
179+
179180 var object T
180- return GetEngine ( ctx ) .Where (opts .ToConds ()).Count (& object )
181+ return sess .Where (opts .ToConds ()).Count (& object )
181182}
182183
183184// FindAndCount represents a common findandcount function which accept an options interface
0 commit comments