This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/helpers.go
2015-06-18 13:08:54 -08:00

47 lines
1.1 KiB
Go

package main
import "fmt"
// ListOptions specifies general pagination options for fetching a list of results
type ListOptions struct {
PerPage int64 `url:",omitempty" json:",omitempty"`
Page int64 `url:",omitempty" json:",omitempty"`
Ids []int64 `url:",omitempty" json:",omitempty" schema:"ids[]"`
Genus string
}
func (o ListOptions) PageOrDefault() int64 {
if o.Page <= 0 {
return 1
}
return o.Page
}
func (o ListOptions) Offset() int64 {
return (o.PageOrDefault() - 1) * o.PerPageOrDefault()
}
func (o ListOptions) PerPageOrDefault() int64 {
if o.PerPage <= 0 {
return DefaultPerPage
}
return o.PerPage
}
// DefaultPerPage is the default number of items to return in a paginated result set
const DefaultPerPage = 10
func valsIn(attribute string, values []int64, vals *[]interface{}, counter *int64) string {
if len(values) == 1 {
return fmt.Sprintf("%v=%v", attribute, values[0])
}
m := fmt.Sprintf("%v IN (", attribute)
for _, id := range values {
m = m + fmt.Sprintf("$%v,", *counter)
*vals = append(*vals, id)
*counter++
}
m = m[:len(m)-1] + ")"
return m
}