@@ -133,27 +133,28 @@ type FindOptionsOrder interface {
133
133
134
134
// Find represents a common find function which accept an options interface
135
135
func Find [T any ](ctx context.Context , opts FindOptions ) ([]* T , error ) {
136
- sess := GetEngine (ctx )
136
+ sess := GetEngine (ctx ). Where ( opts . ToConds ())
137
137
138
- if joinOpt , ok := opts .(FindOptionsJoin ); ok && len ( joinOpt . ToJoins ()) > 0 {
138
+ if joinOpt , ok := opts .(FindOptionsJoin ); ok {
139
139
for _ , joinFunc := range joinOpt .ToJoins () {
140
140
if err := joinFunc (sess ); err != nil {
141
141
return nil , err
142
142
}
143
143
}
144
144
}
145
+ if orderOpt , ok := opts .(FindOptionsOrder ); ok {
146
+ if order := orderOpt .ToOrders (); order != "" {
147
+ sess .OrderBy (order )
148
+ }
149
+ }
145
150
146
- sess = sess .Where (opts .ToConds ())
147
151
page , pageSize := opts .GetPage (), opts .GetPageSize ()
148
152
if ! opts .IsListAll () && pageSize > 0 {
149
153
if page == 0 {
150
154
page = 1
151
155
}
152
156
sess .Limit (pageSize , (page - 1 )* pageSize )
153
157
}
154
- if newOpt , ok := opts .(FindOptionsOrder ); ok && newOpt .ToOrders () != "" {
155
- sess .OrderBy (newOpt .ToOrders ())
156
- }
157
158
158
159
findPageSize := defaultFindSliceSize
159
160
if pageSize > 0 {
@@ -168,8 +169,8 @@ func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error) {
168
169
169
170
// Count represents a common count function which accept an options interface
170
171
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 {
172
+ sess := GetEngine (ctx ). Where ( opts . ToConds ())
173
+ if joinOpt , ok := opts .(FindOptionsJoin ); ok {
173
174
for _ , joinFunc := range joinOpt .ToJoins () {
174
175
if err := joinFunc (sess ); err != nil {
175
176
return 0 , err
@@ -178,7 +179,7 @@ func Count[T any](ctx context.Context, opts FindOptions) (int64, error) {
178
179
}
179
180
180
181
var object T
181
- return sess .Where ( opts . ToConds ()). Count (& object )
182
+ return sess .Count (& object )
182
183
}
183
184
184
185
// FindAndCount represents a common findandcount function which accept an options interface
@@ -188,8 +189,17 @@ func FindAndCount[T any](ctx context.Context, opts FindOptions) ([]*T, int64, er
188
189
if ! opts .IsListAll () && pageSize > 0 && page >= 1 {
189
190
sess .Limit (pageSize , (page - 1 )* pageSize )
190
191
}
191
- if newOpt , ok := opts .(FindOptionsOrder ); ok && newOpt .ToOrders () != "" {
192
- sess .OrderBy (newOpt .ToOrders ())
192
+ if joinOpt , ok := opts .(FindOptionsJoin ); ok {
193
+ for _ , joinFunc := range joinOpt .ToJoins () {
194
+ if err := joinFunc (sess ); err != nil {
195
+ return nil , 0 , err
196
+ }
197
+ }
198
+ }
199
+ if orderOpt , ok := opts .(FindOptionsOrder ); ok {
200
+ if order := orderOpt .ToOrders (); order != "" {
201
+ sess .OrderBy (order )
202
+ }
193
203
}
194
204
195
205
findPageSize := defaultFindSliceSize
0 commit comments