ripple-api/common/paginate.go

23 lines
360 B
Go
Raw Normal View History

2016-04-07 09:20:35 +00:00
package common
2016-11-21 15:27:21 +00:00
import "fmt"
2016-04-07 09:20:35 +00:00
// Paginate creates an additional SQL LIMIT clause for paginating.
2016-05-19 15:15:17 +00:00
func Paginate(page, limit string, maxLimit int) string {
2016-04-07 09:20:35 +00:00
var (
2016-11-21 15:27:21 +00:00
p = Int(page)
l = Int(limit)
2016-04-07 09:20:35 +00:00
)
2016-11-21 15:27:21 +00:00
if p < 1 {
p = 1
2016-04-07 09:20:35 +00:00
}
2016-11-21 15:27:21 +00:00
if l < 1 {
l = 50
2016-04-07 09:20:35 +00:00
}
2016-11-21 15:27:21 +00:00
if l > maxLimit {
l = maxLimit
2016-04-07 10:08:44 +00:00
}
2016-11-21 15:27:21 +00:00
start := uint(p-1) * uint(l)
return fmt.Sprintf(" LIMIT %d,%d ", start, l)
2016-04-07 09:20:35 +00:00
}