query

package
v1.0.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2019 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertQueryEqual

func AssertQueryEqual(t *testing.T, got *DBQuery, want *DBQuery)

AssertQueryEqual tests if two query input are equal

Types

type Builder

type Builder struct {
	*Config
	// contains filtered or unexported fields
}

Builder is a query builder. You should initialize it only once, and then use it in your http.Handler.

func MustNewBuilder

func MustNewBuilder(c *Config) *Builder

MustNewBuilder creates a new builder and panic on failure

func NewBuilder

func NewBuilder(c *Config) (*Builder, error)

NewBuilder initialize a Builder and parse the passing Model that will be used in the Parse calls.

func (*Builder) Parse

func (b *Builder) Parse(params url.Values) (*DBQuery, error)

Parse validates and parses the input params and return back a *DBQuery. It's safe to call it from multiple goroutines concurrently.

func (*Builder) ParseRequest

func (b *Builder) ParseRequest(r *http.Request) (*DBQuery, error)

ParseRequest is a helper function for parsing query from a request object

type Config

type Config struct {
	// Model is an instance of the struct definition. the Builder will parse
	// the url.Values according to this.
	Model interface{}
	// TagName is the name of the tag in the struct. defaults to "query".
	TagName string
	// Separator between field and command. defaults to "_".
	Separator string
	// IgnoreSort indicates if the builder should skip the sort process.
	IgnoreSort bool
	// SortParam is the name of the sort parameter.
	// defaults to "sort"
	SortParam string
	// DefaultSort is the default sort string for the query builder.
	// if the builder gets and empty sort parameter it'll add this default.
	DefaultSort string
	// LimitParam is the name of the limit parameter in the query string.
	// defaults to "limit".
	LimitParam string
	// DefaultLimit is the default value for limit option. default to 25.
	DefaultLimit int
	// LimitMaxValue is the maximum value that accept valid parameter.
	LimitMaxValue int
	// OffsetParam is the name of the offset parameter in the query string.
	// defaults to "offset"
	OffsetParam string
	// SearchOperator used to combine search condition together. defaults to "AND".
	SearchOperator string
	// ExplicitSelect - if true, the query will select the relevant specific columns.
	//    else will select '*'
	ExplicitSelect bool
	// OnlySelectNonDetailedFields - if true will select only the non 'detailed' fields
	//    true implies ExplicitSelect = true
	OnlySelectNonDetailedFields bool
}

Config for the Builder constructor.

type DBQuery

type DBQuery struct {
	// the number of rows returned by the SELECT statement.
	Limit int
	// start querying from offset x. used for pagination.
	Offset int
	// used as a parameter for the gorm.Order method. example: "age desc, name"
	Sort string
	// CondExp and CondVal come together and used as a parameters for the gorm.Where
	// method.
	//
	// examples:
	// 	1. Exp: "name = ?"
	//	   Val: "a8m"
	//
	//	2. Exp: "id IN (?)"
	//	   Val: []int{1,2,3}
	//
	//	3. Exp: "name = ? AND age >= ?"
	// 	   Val: "a8m", 22
	CondExp string
	CondVal []interface{}
	// Select specify fields that you want to retrieve from database when querying.
	// the default is to select all fields.
	//
	//	Select: "DISTINCT id"
	Select string
}

DBQuery are options for query a database

func (*DBQuery) And added in v1.0.7

func (q *DBQuery) And(exp string, vals ...interface{})

And adds expression to the current where statement with AND condition

func (*DBQuery) Apply added in v1.0.2

func (q *DBQuery) Apply(db *gorm.DB) *gorm.DB

Apply applies the query input on a database instance

type ParseError

type ParseError struct {
	// contains filtered or unexported fields
}

ParseError is a typed error created dynamically based on the parsing failure.

func (ParseError) Error

func (e ParseError) Error() string

Error implements the error interface.

type Searcher

type Searcher interface {
	Search(term string) (exp string, vals []interface{})
}

Searcher is the interface that wraps the Search method. Models that want to support search, need to implement this interface. if a "search" term is provided to the Parse method, the Builder will call the Search method with this value. the return value should be a search query for this term + its arguments. (we let gorm handle the escaping). If multiple search terms are provided, the Builder will use the SearchOperator to combine these search queries together.

type WrapFn

type WrapFn func(string) string

WrapFn is a function type that implements the Wrapper interface.

func (WrapFn) Wrap

func (f WrapFn) Wrap(s string) string

Wrap is the function that implements the Wrapper interface. it called itself with the argument. nopWrapper is an example for it.

type Wrapper

type Wrapper interface {
	Wrap(string) string
}

Wrapper is the interface that wraps the wrap method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL