-
Notifications
You must be signed in to change notification settings - Fork 0
/
limit.go
51 lines (43 loc) · 945 Bytes
/
limit.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
package clause
import (
"gorm.io/gorm/clause"
"strconv"
)
// Limit limit clause
type Limit struct {
Limit int
Offset int
}
// Name where clause name
func (limit Limit) Name() string {
return "LIMIT"
}
// Build build where clause
func (limit Limit) Build(builder clause.Builder) {
if limit.Limit > 0 {
builder.WriteString("LIMIT ")
builder.WriteString(strconv.Itoa(limit.Limit))
}
if limit.Offset > 0 {
if limit.Limit > 0 {
builder.WriteByte(' ')
}
builder.WriteString("OFFSET ")
builder.WriteString(strconv.Itoa(limit.Offset))
}
}
// MergeClause merge order by clauses
func (limit Limit) MergeClause(clause *clause.Clause) {
clause.Name = ""
if v, ok := clause.Expression.(Limit); 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
}