Documentation ¶
Index ¶
- Constants
- Variables
- type Error
- type Filter
- type Method
- type Query
- func (q *Query) AddField(field string) *Query
- func (q *Query) AddFilter(name string, m Method, value interface{}) *Query
- func (q *Query) AddFilterRaw(condition string) *Query
- func (q *Query) AddSortBy(by string, desc bool) *Query
- func (q *Query) AddValidation(NameAndTags string, v ValidationFunc) *Query
- func (q *Query) Args() []interface{}
- func (q *Query) FieldsString() string
- func (q *Query) GetFilter(name string) (*Filter, error)
- func (q *Query) HaveField(field string) bool
- func (q *Query) HaveFilter(name string) bool
- func (q *Query) HaveSortBy(by string) bool
- func (q *Query) IgnoreUnknownFilters(i bool) *Query
- func (q *Query) LIMIT() string
- func (q *Query) OFFSET() string
- func (q *Query) ORDER() string
- func (q *Query) Order() string
- func (q *Query) Parse() (err error)
- func (q *Query) RemoveFilter(name string) error
- func (q *Query) RemoveValidation(NameAndOrTags string) error
- func (q *Query) ReplaceNames(r Replacer)
- func (q *Query) SELECT() string
- func (q *Query) SQL(table string) string
- func (q *Query) Select() string
- func (q *Query) SetDelimiterIN(d string) *Query
- func (q *Query) SetDelimiterOR(d string) *Query
- func (q *Query) SetUrlQuery(query url.Values) *Query
- func (q *Query) SetUrlString(Url string) error
- func (q *Query) SetValidations(v Validations) *Query
- func (q *Query) WHERE() string
- func (q *Query) Where() string
- type Replacer
- type Sort
- type StateOR
- type ValidationFunc
- type Validations
Constants ¶
const NULL = "NULL"
NULL constant
Variables ¶
var ( ErrRequired = NewError("required") ErrBadFormat = NewError("bad format") ErrEmptyValue = NewError("empty value") ErrUnknownMethod = NewError("unknown method") ErrNotInScope = NewError("not in scope") ErrSimilarNames = NewError("similar names of keys are not allowed") ErrMethodNotAllowed = NewError("method are not allowed") ErrFilterNotAllowed = NewError("filter are not allowed") ErrFilterNotFound = NewError("filter not found") ErrValidationNotFound = NewError("validation not found") )
Errors list:
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error special rqp.Error type
type Filter ¶
type Filter struct { Key string // key from URL (eg. "id[eq]") Name string // name of filter, takes from Key (eg. "id") Method Method // compare method, takes from Key (eg. EQ) Value interface{} OR StateOR }
Filter represents a filter defined in the query part of URL
type Query ¶
type Query struct { Fields []string Offset int Limit int Sorts []Sort Filters []*Filter Error error // contains filtered or unexported fields }
Query the main struct of package
func NewParse ¶
func NewParse(q url.Values, v Validations) (*Query, error)
NewParse creates new Query instance and Parse it
func NewQV ¶
func NewQV(q url.Values, v Validations) *Query
NewQV creates new Query instance with parameters
func (*Query) AddFilterRaw ¶
AddFilterRaw adds a filter to Query as SQL condition. This function supports only single condition per one call. If you'd like add more then one conditions you should call this func several times.
func (*Query) AddValidation ¶
func (q *Query) AddValidation(NameAndTags string, v ValidationFunc) *Query
AddValidation adds a validation to Query
func (*Query) Args ¶
func (q *Query) Args() []interface{}
Args returns slice of arguments for WHERE statement
func (*Query) FieldsString ¶
FieldsString returns elements list separated by comma (",") for querying in SELECT statement or a star ("*") if nothing provided
Return example:
When "fields" empty or not provided: `*`.
When "fields=id,email": `id, email`.
func (*Query) HaveFilter ¶
HaveFilter returns true if request contains some filter
func (*Query) HaveSortBy ¶
HaveSortBy returns true if request contains sorting by specified in by field name
func (*Query) IgnoreUnknownFilters ¶
IgnoreUnknownFilters set behavior for Parser to raise ErrFilterNotAllowed to undefined filters or not
func (*Query) ORDER ¶
ORDER returns words ORDER BY with list of elements for sorting you can use +/- prefix to specify direction of sorting (+ is default, apsent is +)
Return example: ` ORDER BY id DESC, email`
func (*Query) Order ¶
Order returns list of elements for ORDER BY statement you can use +/- prefix to specify direction of sorting (+ is default) return example: `id DESC, email`
func (*Query) Parse ¶
Parse parses the query of URL as query you can use standart http.Request query by r.URL.Query()
func (*Query) RemoveFilter ¶
RemoveFilter removes the filter by name
func (*Query) RemoveValidation ¶
RemoveValidation remove a validation from Query You can provide full name of filter with tags or only name of filter: RemoveValidation("id:int") and RemoveValidation("id") are equal
func (*Query) ReplaceNames ¶
ReplaceNames replace all specified name to new names Sometimes we've to hijack properties, for example when we do JOINs, so you can ask for filter/field "user_id" but replace it with "users.user_id". Parameter is a map[string]string which means map[currentName]newName. The library provide beautiful way by using special type rqp.Replacer. Example:
rqp.ReplaceNames(rqp.Replacer{ "user_id": "users.user_id", })
func (*Query) SELECT ¶
SELECT returns word SELECT with fields from Filter "fields" separated by comma (",") from URL-Query or word SELECT with star ("*") if nothing provided
Return examples:
When "fields" empty or not provided: `SELECT *`.
When "fields=id,email": `SELECT id, email`.
func (*Query) Select ¶
Select returns elements list separated by comma (",") for querying in SELECT statement or a star ("*") if nothing provided
Return examples:
When "fields" empty or not provided: `*`
When "fields=id,email": `id, email`
func (*Query) SetDelimiterIN ¶
SetDelimiterIN sets delimiter for values of filters
func (*Query) SetDelimiterOR ¶
SetDelimiterOR sets delimiter for OR filters in query part of URL
func (*Query) SetUrlQuery ¶
SetUrlQuery change url in the Query for parsing uses when you need provide Query from http.HandlerFunc(w http.ResponseWriter, r *http.Request) you can do q.SetUrlValues(r.URL.Query())
func (*Query) SetUrlString ¶
SetUrlString change url in the Query for parsing uses when you would like to provide raw URL string to parsing
func (*Query) SetValidations ¶
func (q *Query) SetValidations(v Validations) *Query
SetValidations change validations rules for the instance
type ValidationFunc ¶
type ValidationFunc func(value interface{}) error
ValidationFunc represents validator for Filters
func MinMax ¶
func MinMax(min, max int) ValidationFunc
MinMax validation if value between or equal min and max
func Multi ¶
func Multi(values ...ValidationFunc) ValidationFunc
Multi multiple validation func usage: Multi(Min(10), Max(100))
func NotEmpty ¶
func NotEmpty() ValidationFunc
NotEmpty validation if string value length more then 0
type Validations ¶
type Validations map[string]ValidationFunc
Validations type replacement for map. Used in NewParse(), NewQV(), SetValidations()