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-24 17:29:00 -08:00

56 lines
1.3 KiB
Go

package main
import (
"errors"
"fmt"
"net/http"
)
var (
ErrMustProvideOptions = errors.New("Must provide necessary options")
ErrMustProvideOptionsJSON = newJSONError(ErrMustProvideOptions, http.StatusBadRequest)
)
// 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
}