@@ -21,31 +21,16 @@ const (
21
21
// Paginator is the base for different ListOptions types
22
22
type Paginator interface {
23
23
GetSkipTake () (skip , take int )
24
- GetStartEnd () (start , end int )
25
24
IsListAll () bool
26
25
}
27
26
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
-
35
27
// SetSessionPagination sets pagination for a database session
36
28
func SetSessionPagination (sess Engine , p Paginator ) * xorm.Session {
37
29
skip , take := p .GetSkipTake ()
38
30
39
31
return sess .Limit (take , skip )
40
32
}
41
33
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
-
49
34
// ListOptions options to paginate results
50
35
type ListOptions struct {
51
36
PageSize int
@@ -66,13 +51,6 @@ func (opts *ListOptions) GetSkipTake() (skip, take int) {
66
51
return (opts .Page - 1 ) * opts .PageSize , opts .PageSize
67
52
}
68
53
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
-
76
54
func (opts ListOptions ) GetPage () int {
77
55
return opts .Page
78
56
}
@@ -135,11 +113,6 @@ func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
135
113
return opts .skip , opts .take
136
114
}
137
115
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
-
143
116
// FindOptions represents a find options
144
117
type FindOptions interface {
145
118
GetPage () int
@@ -148,15 +121,34 @@ type FindOptions interface {
148
121
ToConds () builder.Cond
149
122
}
150
123
124
+ type JoinFunc func (sess Engine ) error
125
+
126
+ type FindOptionsJoin interface {
127
+ ToJoins () []JoinFunc
128
+ }
129
+
151
130
type FindOptionsOrder interface {
152
131
ToOrders () string
153
132
}
154
133
155
134
// Find represents a common find function which accept an options interface
156
135
func 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 ())
158
147
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
+ }
160
152
sess .Limit (pageSize , (page - 1 )* pageSize )
161
153
}
162
154
if newOpt , ok := opts .(FindOptionsOrder ); ok && newOpt .ToOrders () != "" {
@@ -176,8 +168,17 @@ func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error) {
176
168
177
169
// Count represents a common count function which accept an options interface
178
170
func 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
+
179
180
var object T
180
- return GetEngine ( ctx ) .Where (opts .ToConds ()).Count (& object )
181
+ return sess .Where (opts .ToConds ()).Count (& object )
181
182
}
182
183
183
184
// FindAndCount represents a common findandcount function which accept an options interface
0 commit comments