-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.go
40 lines (36 loc) · 1.09 KB
/
range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package clause
import (
dbi "github.com/hopeio/cherry/utils/dao/database"
"github.com/hopeio/cherry/utils/types/request"
"gorm.io/gorm/clause"
)
type RangeReq[T request.Ordered] request.RangeReq[T]
func (req *RangeReq[T]) Clause() clause.Expression {
if req == nil || req.RangeField == "" {
return nil
}
var zero T
operation := dbi.Between
if req.RangeEnd == zero && req.RangeBegin != zero {
operation = dbi.Greater
if req.Include {
operation = dbi.GreaterOrEqual
}
return NewWhereClause(req.RangeField, operation, req.RangeBegin)
}
if req.RangeBegin == zero && req.RangeEnd != zero {
operation = dbi.Less
if req.Include {
operation = dbi.LessOrEqual
}
return NewWhereClause(req.RangeField, operation, req.RangeBegin)
}
if req.RangeBegin != zero && req.RangeEnd != zero {
if req.Include {
return NewWhereClause(req.RangeField, operation, req.RangeBegin, req.RangeEnd)
} else {
return clause.Where{Exprs: []clause.Expression{NewWhereClause(req.RangeField, dbi.Greater, req.RangeBegin), NewWhereClause(req.RangeField, dbi.Less, req.RangeBegin)}}
}
}
return nil
}