Repository navigation
Expand file tree
/
Copy pathoffsetlimit.go
More file actions
63 lines (53 loc) · 1.73 KB
/
Copy pathoffsetlimit.go
File metadata and controls
63 lines (53 loc) · 1.73 KB
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
58
59
60
61
62
63
package sqlite
import (
"fmt"
"net/url"
// Packages
types "github.com/mutablelogic/go-server/pkg/types"
)
////////////////////////////////////////////////////////////////////////////////
// TYPES
type OffsetLimit struct {
Offset uint64 `json:"offset,omitempty" help:"Offset within the result set" jsonschema:"Offset for pagination" example:"0"`
Limit *uint64 `json:"limit,omitempty" help:"Number of items to return" jsonschema:"Limit for pagination" example:"100"`
}
////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Bind sets the offset and limit SQL fragment on the bind object.
func (r *OffsetLimit) Bind(bind *Bind, max uint64) {
if (r.Limit != nil && *r.Limit > max) || r.Limit == nil {
r.Limit = &max
}
if r.Offset != 0 && r.Limit != nil {
bind.Set("offsetlimit", fmt.Sprintf("LIMIT %v OFFSET %v", *r.Limit, r.Offset))
} else if r.Limit != nil {
bind.Set("offsetlimit", fmt.Sprintf("LIMIT %v", *r.Limit))
} else if r.Offset != 0 {
bind.Set("offsetlimit", fmt.Sprintf("OFFSET %v", r.Offset))
} else {
bind.Set("offsetlimit", "")
}
}
// Query sets offset and limit query parameters on the provided map.
func (r *OffsetLimit) Query(params url.Values) {
if r.Limit != nil {
params.Set("limit", fmt.Sprintf("%v", types.Value(r.Limit)))
}
if r.Offset != 0 {
params.Set("offset", fmt.Sprintf("%v", r.Offset))
}
}
// Clamp restricts the limit to the number of rows available after offset.
func (r *OffsetLimit) Clamp(total uint64) {
if r.Limit == nil {
return
}
// Available rows are constrained by total count and current offset.
available := total
if r.Offset >= total {
available = 0
} else {
available = total - r.Offset
}
*r.Limit = min(*r.Limit, available)
}