/
slimit.go
57 lines (50 loc) · 1.05 KB
/
slimit.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package slimit
import (
"gorm.io/gorm/clause"
"strconv"
)
// SLimit limit clause
type SLimit struct {
Limit int
Offset int
}
// Name SLIMIT clause name
func (limit SLimit) Name() string {
return "SLIMIT"
}
// Build SLIMIT clause
func (limit SLimit) Build(builder clause.Builder) {
if limit.Limit > 0 {
builder.WriteString("SLIMIT ")
builder.WriteString(strconv.Itoa(limit.Limit))
}
if limit.Offset > 0 {
if limit.Limit > 0 {
builder.WriteString(" ")
}
builder.WriteString("SOFFSET ")
builder.WriteString(strconv.Itoa(limit.Offset))
}
}
// MergeClause merge SLIMIT by clauses
func (limit SLimit) MergeClause(clause *clause.Clause) {
clause.Name = ""
if v, ok := clause.Expression.(SLimit); ok {
if limit.Limit == 0 && v.Limit != 0 {
limit.Limit = v.Limit
}
if limit.Offset == 0 && v.Offset > 0 {
limit.Offset = v.Offset
} else if limit.Offset < 0 {
limit.Offset = 0
}
}
clause.Expression = limit
}
//SetSLimit SLimit clause
func SetSLimit(limit, offset int) SLimit {
return SLimit{
Limit: limit,
Offset: offset,
}
}