query

package module
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 6 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

limit and offset are used to limit the number of rows returned. 0 limit means no limit.
fields is used to select the fields to be returned, comma separated.
sort is used to sort the result set, can be prefixed with - to indicate descending order and comma separated to indicate multiple fields.
[] empty operator means in operator.

Validation

query.WithField is used to validate the field names.

  • WithNotIn is used to validate the field names that are not allowed.
  • WithIn is used to validate the field names that are allowed.
  • WithNotAllowed is used to validate the field names totally not allowed.

query.WithValues is used to validate the values of the fields.

  • WithNotIn is used to validate the values that are not allowed.
  • WithIn is used to validate the values that are allowed.
  • WithNotAllowed is used to validate the values totally not allowed.

query.WithValue is used to validate the values of the fields.

  • WithRequired is used to validate the values that are required.
  • WithNotEmpty is used to validate the values that are not empty.
  • WithNotIn is used to validate the values that are not allowed.
  • WithIn is used to validate the values that are allowed.
  • WithNotAllowed is used to validate the value that are not allowed.
  • WithOperator is used to validate the operator that is allowed.
  • WithNotOperator is used to validate the operator that is not allowed.
  • WithMax is used to validate the maximum of value, value must be a number.
  • WithMin is used to validate the minimum of value, value must be a number.

query.WithOffset is used to validate the offset value.

  • WithMax is used to validate the maximum of offset, value must be a number.
  • WithMin is used to validate the minimum of offset, value must be a number.
  • WithNotAllowed is used to validate the offset value that are not allowed.

query.WithLimit is used to validate the limit value.

  • WithMax is used to validate the maximum of limit, value must be a number.
  • WithMin is used to validate the minimum of limit, value must be a number.
  • WithNotAllowed is used to validate the limit value that are not allowed.

query.WithSort is used to validate the sort value.

  • WithNotIn is used to validate the sort value that are not allowed.
  • WithIn is used to validate the sort value that are allowed.
  • WithNotAllowed is used to validate the sort value that are not allowed.

Example of validation:

validator := query.NewValidator(
    WithValue("member", query.WithRequired(), query.WithNotIn("O", "P", "S")),
    WithValues(query.WithIn("age", "test", "member")),
    WithValue("age", query.WithOperator(OperatorEq), query.WithNotOperator(OperatorIn)),
    WithField(query.WithNotAllowed()),
)

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

// or pass with when parsing
query, err := query.Parse(rawQuery, query.WithValidator(validator))
// ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithIn

func WithIn(values ...string) optionValidateFunc

WithIn checks if the value is in the list of values.

  • Usable for 'WithValue', 'WithSort', 'WithValues', 'WithFields'

func WithMax

func WithMax(max string) optionValidateFunc

WithMax to validate the maximum of a value.

  • Usable for 'WithValue', 'WithLimit', 'WithOffset'

func WithMin

func WithMin(min string) optionValidateFunc

WithMin to validate the minimum of a value.

  • Usable for 'WithValue', WithSort', 'WithLimit'

func WithNotAllowed added in v0.1.2

func WithNotAllowed() optionValidateFunc

WithNotAllowed to validate the value is not allowed.

  • Usable for 'WithValue', 'WithOffset', 'WithLimit', 'WithSort', 'WithValues', 'WithFields'

func WithNotEmpty

func WithNotEmpty() optionValidateFunc

WithNotEmpty to validate the value is not empty.

  • Usable for 'WithValue'

func WithNotIn

func WithNotIn(values ...string) optionValidateFunc

WithNotIn checks if the value is not in the list of values.

  • Usable for 'WithValue', 'WithSort', 'WithValues', 'WithFields'

func WithNotOperator added in v0.1.4

func WithNotOperator(operators ...OperatorCmpType) optionValidateFunc

WithNotOperator to validate the operator is not allowed.

  • Usable for 'WithValue'

func WithOperator added in v0.1.4

func WithOperator(operators ...OperatorCmpType) optionValidateFunc

WithOperator to validate the operator is allowed.

  • Usable for 'WithValue'

func WithRequired

func WithRequired() optionValidateFunc

WithRequired to validate the value is required.

  • 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 ExpressionSort added in v0.1.10

type ExpressionSort 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 (
	// OperatorEmpty is the empty operator.
	OperatorEmpty OperatorCmpType = ""
	// 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 WithLimit added in v0.1.10

func WithLimit(opts ...optionValidateFunc) OptionValidateSet

func WithOffset added in v0.1.10

func WithOffset(opts ...optionValidateFunc) OptionValidateSet

func WithSort added in v0.1.10

func WithSort(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
	Sort   []ExpressionSort
	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) GetValue added in v0.1.6

func (q *Query) GetValue(v string) string

func (*Query) GetValues added in v0.1.6

func (q *Query) GetValues(v string) []string

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