query

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: MIT Imports: 4 Imported by: 0

README

Query

License Coverage GitHub Workflow Status Go Report Card Go PKG

Query is an adaptor of http query to expressions. Check adapters to convert it to sql or other expressions.

go get github.com/worldline-go/query

Usage

Parse url and extract query parameters with RAW, give to query.Parse to convert it to expression.
Use an adapter to convert it to sql or other expressions.

urlStr := "http://example.com?name=foo,bar|nick=bar&age[lt]=1&sort=-age&limit=10&offset=5&fields=id,name"
parsedURL, err := url.Parse(urlStr)
// ...
query, err := query.Parse(parsedURL.RawQuery)
// ...
sql, params, err := adaptergoqu.Select(query, goqu.From("test")).ToSQL()
// ...

// SQL: SELECT "id", "name" FROM "test" WHERE ((("name" IN ('foo', 'bar')) OR ("nick" = 'bar')) AND ("age" < '1')) ORDER BY "age" DESC LIMIT 10 OFFSET 5
// Params: []

If some value separated by , it will be converted to IN operator.
There are a list of [ ] operators that can be used in the query string:
eq, ne, gt, lt, gte, lte, like, ilike, nlike, nilike, in, nin, is, not

Validation
validator := query.NewValidator(
    WithValue("member", WithRequired()),
)

// after that use it to validate
err := validator.Validate(query)
if err != nil {
    // handle error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithIn

func WithIn(values ...string) optionValidateFunc

func WithMax

func WithMax(max json.Number) optionValidateFunc

WithMax to validate the maximum of a value.

  • only usable for 'WithValue'

func WithMin

func WithMin(min json.Number) optionValidateFunc

WithMin to validate the minimum of a value.

  • only usable for 'WithValue'

func WithNotAllowed added in v0.1.2

func WithNotAllowed() optionValidateFunc

func WithNotEmpty

func WithNotEmpty() optionValidateFunc

WithNotEmpty to validate the value is not empty.

  • only usable for 'WithValue'

func WithNotIn

func WithNotIn(values ...string) optionValidateFunc

func WithRequired

func WithRequired() optionValidateFunc

WithRequired to validate the value is required.

  • only usable for 'WithValue'

Types

type Expression

type Expression interface {
	Expression() Expression
}

type ExpressionCmp

type ExpressionCmp struct {
	Operator OperatorCmpType
	Field    string
	Value    any
}

func (ExpressionCmp) Expression

func (e ExpressionCmp) Expression() Expression

type ExpressionLogic

type ExpressionLogic struct {
	Operator OperatorLogicType
	List     []Expression
}

func (ExpressionLogic) Expression

func (e ExpressionLogic) Expression() Expression

type ExpressionOrder

type ExpressionOrder struct {
	// Field is the field name to order by.
	Field string
	// Desc indicates whether the order is descending.
	Desc bool
}

type OperatorCmpType

type OperatorCmpType string
const (
	// OperatorEq is the equality operator.
	OperatorEq OperatorCmpType = "eq"
	// OperatorNe is the not equal operator.
	OperatorNe OperatorCmpType = "ne"
	// OperatorGt is the greater than operator.
	OperatorGt OperatorCmpType = "gt"
	// OperatorLt is the less than operator.
	OperatorLt OperatorCmpType = "lt"
	// OperatorGte is the greater than or equal operator.
	OperatorGte OperatorCmpType = "gte"
	// OperatorLte is the less than or equal operator.
	OperatorLte OperatorCmpType = "lte"
	// OperatorLike is the like operator.
	OperatorLike OperatorCmpType = "like"
	// OperatorILike is the case insensitive like operator.
	OperatorILike OperatorCmpType = "ilike"
	// OperatorNLike is the not like operator.
	OperatorNLike OperatorCmpType = "nlike"
	// OperatorNILike is the case insensitive not like operator.
	OperatorNILike OperatorCmpType = "nilike"
	// OperatorIn is the in operator.
	OperatorIn OperatorCmpType = "in"
	// OperatorNIn is the not in operator.
	OperatorNIn OperatorCmpType = "nin"
	// OperatorIs is the is null operator.
	OperatorIs OperatorCmpType = "is"
	// OperatorIsNot is the is not null operator.
	OperatorIsNot OperatorCmpType = "not"
)

type OperatorLogicType

type OperatorLogicType string
const (
	// OperatorAnd is the AND operator.
	OperatorAnd OperatorLogicType = "and"
	// OperatorOr is the OR operator.
	OperatorOr OperatorLogicType = "or"
)

type OptionQuery added in v0.1.2

type OptionQuery func(*optionQuery)

func WithDefaultLimit added in v0.1.2

func WithDefaultLimit(limit uint64) OptionQuery

WithDefaultLimit sets the default limit value.

func WithDefaultOffset added in v0.1.2

func WithDefaultOffset(offset uint64) OptionQuery

WithDefaultOffset sets the default offset value.

func WithExpressionCmp added in v0.1.2

func WithExpressionCmp(key string, value ExpressionCmp) OptionQuery

WithExpressionCmp sets the expression comparison for a given key.

func WithSkipExpressionCmp added in v0.1.3

func WithSkipExpressionCmp(key ...string) OptionQuery

WithSkipExpressionCmp sets the keys to be skipped in the query.

type OptionValidate

type OptionValidate func(string, ...optionValidateFunc)

type OptionValidateSet

type OptionValidateSet func(v *Validator) error

func WithField

func WithField(opts ...optionValidateFunc) OptionValidateSet

func WithValue

func WithValue(key string, opts ...optionValidateFunc) OptionValidateSet

func WithValues added in v0.1.2

func WithValues(opts ...optionValidateFunc) OptionValidateSet

type Query

type Query struct {
	Values map[string][]ExpressionCmp

	Select []string
	Where  []Expression
	Order  []ExpressionOrder
	Offset *uint64
	Limit  *uint64
}

func Parse

func Parse(query string, opts ...OptionQuery) (*Query, error)

Parse parses a query string into a Query struct.

func ParseWithValidator added in v0.1.2

func ParseWithValidator(query string, validator *Validator, opts ...OptionQuery) (*Query, error)

func (*Query) CloneLimit added in v0.1.3

func (q *Query) CloneLimit() *uint64

func (*Query) CloneOffset added in v0.1.3

func (q *Query) CloneOffset() *uint64

func (*Query) GetLimit added in v0.1.2

func (q *Query) GetLimit() uint64

func (*Query) GetOffset added in v0.1.2

func (q *Query) GetOffset() uint64

func (*Query) Has added in v0.1.2

func (q *Query) Has(v string) bool

func (*Query) HasAny added in v0.1.2

func (q *Query) HasAny(vList ...string) bool

func (*Query) Validate

func (q *Query) Validate(v *Validator) error

func (*Query) Walk

func (q *Query) Walk(fn func(Token) error) error

Walk traverses the query tree and calls the provided function for each expression.

type Token

type Token struct {
	Expression Expression
	Type       WalkType
}

type Validator added in v0.1.1

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

func NewValidator

func NewValidator(opts ...OptionValidateSet) (*Validator, error)

type WalkType

type WalkType int
const (
	WalkCurrent WalkType = iota
	WalkStart
	WalkEnd
)

Directories

Path Synopsis
adapter

Jump to

Keyboard shortcuts

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