2016-04-07 09:20:35 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 (
|
|
|
|
pInt int
|
|
|
|
lInt int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if page == "" {
|
|
|
|
pInt = 1
|
|
|
|
} else {
|
|
|
|
pInt, err = strconv.Atoi(page)
|
|
|
|
if err != nil {
|
|
|
|
pInt = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit == "" {
|
|
|
|
lInt = 50
|
|
|
|
} else {
|
|
|
|
lInt, err = strconv.Atoi(limit)
|
|
|
|
if err != nil {
|
|
|
|
lInt = 50
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 10:08:44 +00:00
|
|
|
if pInt < 1 {
|
|
|
|
pInt = 1
|
|
|
|
}
|
|
|
|
if lInt < 1 {
|
|
|
|
lInt = 50
|
|
|
|
}
|
2016-05-19 15:15:17 +00:00
|
|
|
if lInt > maxLimit {
|
|
|
|
lInt = maxLimit
|
|
|
|
}
|
2016-04-07 09:20:35 +00:00
|
|
|
start := (pInt - 1) * lInt
|
|
|
|
return fmt.Sprintf(" LIMIT %d,%d ", start, lInt)
|
|
|
|
}
|