expr

package
v1.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AndExpr

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

AndExpr is an expression that joins child expressions by logical conjunctions.

func And

func And(exprs ...LogicalExpr) *AndExpr

And joins given expressions by logical conjunctions (LogicalAnd). Expressions can be represented by mixes of `expr.Cond{}`, `expr.Or()` and `expr.And()`.

Examples:

  => name = 'Peter' AND last_name = 'Parker'
	 expr.And(
	     expr.Cond{"name": "Peter"},
	     expr.Cond{"last_name": "Parker "},
	 )

  => (name = 'Peter' OR name = 'Mickey') AND last_name = 'Mouse'
	 expr.And(
      expr.Or(
          expr.Cond{"name": "Peter"},
	         expr.Cond{"name": "Mickey"},
      ),
      expr.Cond{"last_name": "Mouse"},
  )

func (*AndExpr) And

func (e *AndExpr) And(ands ...LogicalExpr) *AndExpr

And adds more AND conditions to the expression.

func (AndExpr) Base

func (e AndExpr) Base() interface{}

func (AndExpr) Empty

func (e AndExpr) Empty() bool

func (AndExpr) Expressions

func (e AndExpr) Expressions() []LogicalExpr

func (AndExpr) Fn

func (e AndExpr) Fn(in interface{}) error

func (AndExpr) Operator

func (e AndExpr) Operator() LogicalOperator

func (AndExpr) Prev

func (e AndExpr) Prev() immutable.Immutable

func (AndExpr) String

func (e AndExpr) String() string

type Comparison

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

Comparison represents the relationship between values.

func After

func After(value time.Time) *Comparison

After is a comparison that means: is after the time.

func AnyOf

func AnyOf(value interface{}) *Comparison

AnyOf is a comparison that means: is any of the values of the slice.

func Before

func Before(value time.Time) *Comparison

Before is a comparison that means: is before the time.

func Between

func Between(lower, upper interface{}) *Comparison

Between is a comparison that means: is between lower and upper bound.

func Eq

func Eq(value interface{}) *Comparison

Eq is a comparison that means: is equal to the value.

func Gt

func Gt(value interface{}) *Comparison

Gt is a comparison that means: is greater than the value.

func Gte

func Gte(value interface{}) *Comparison

Gte is a comparison that means: is greater than or equal to the value.

func In

func In(value ...interface{}) *Comparison

In is a comparison that means: is any of the values.

func Is

func Is(value interface{}) *Comparison

Is is a comparison that means: is equivalent to nil, true or false.

func IsNot

func IsNot(value interface{}) *Comparison

IsNot is a comparison that means: is not equivalent to nil, true nor false.

func IsNotNull

func IsNotNull() *Comparison

IsNotNull is a comparison that means: is not equivalent to nil.

func IsNull

func IsNull() *Comparison

IsNull is a comparison that means: is equivalent to nil.

func Like

func Like(value string) *Comparison

Like is a comparison that checks whether the reference matches the wildcard of the value.

func Lt

func Lt(value interface{}) *Comparison

Lt is a comparison that means: is less than the value.

func Lte

func Lte(value interface{}) *Comparison

Lte is a comparison that means: is less than or equal to the value.

func NotAnyOf

func NotAnyOf(value interface{}) *Comparison

NotAnyOf is a comparison that means: is none of the values of the slice.

func NotBetween

func NotBetween(lower, upper interface{}) *Comparison

NotBetween is a comparison that means: is not between lower and upper bound.

func NotEq

func NotEq(value interface{}) *Comparison

NotEq is a comparison that means: is not equal to the value.

func NotIn

func NotIn(value ...interface{}) *Comparison

NotIn is a comparison that means: is none of the values.

func NotLike

func NotLike(value string) *Comparison

NotLike is a comparison that checks whether the reference does not match the wildcard of the value.

func NotRegexp

func NotRegexp(value string) *Comparison

NotRegexp is a comparison that checks whether the reference does not match the regular expression.

func OnOrAfter

func OnOrAfter(value time.Time) *Comparison

OnOrAfter is a comparison that means: is on or after the time.

func OnOrBefore

func OnOrBefore(value time.Time) *Comparison

OnOrBefore is a comparison that means: is on or before the time.

func Op

func Op(op string, value interface{}) *Comparison

Op returns a comparison with the custom operator.

func Regexp

func Regexp(value string) *Comparison

Regexp is a comparison that checks whether the reference matches the regular expression.

func (*Comparison) CustomOperator

func (c *Comparison) CustomOperator() string

CustomOperator returns the custom operator of the comparison.

func (*Comparison) Operator

func (c *Comparison) Operator() ComparisonOperator

Operator returns the ComparisonOperator.

func (*Comparison) Value

func (c *Comparison) Value() interface{}

Value returns the value of the comparison.

type ComparisonOperator

type ComparisonOperator uint8

ComparisonOperator is a comparison operator.

const (
	ComparisonNone ComparisonOperator = iota
	ComparisonCustom

	ComparisonEqual
	ComparisonNotEqual

	ComparisonLessThan
	ComparisonGreaterThan

	ComparisonLessThanOrEqualTo
	ComparisonGreaterThanOrEqualTo

	ComparisonBetween
	ComparisonNotBetween

	ComparisonIn
	ComparisonNotIn

	ComparisonIs
	ComparisonIsNot

	ComparisonLike
	ComparisonNotLike

	ComparisonRegexp
	ComparisonNotRegexp
)

type Cond

type Cond map[interface{}]interface{}

Cond is a map that defines conditions for a query.

Each entry of the map represents a constraint (a column-value relation bound by a ComparisonOperator). The comparison can be specified after the column name, if no ComparisonOperator is provided the equality operator (LogicalAnd) is used as the default.

Examples:

=> age = 18
expr.Cond{"age": 18}

=> age >= 18
expr.Cond{"age >=": 18}

=> id IN (1, 2, 3)
expr.Cond{"id IN": []{1, 2, 3}}

=> age > 32 AND age < 35
 expr.Cond{"age >": 32, "age <": 35}

func (Cond) Constraints

func (c Cond) Constraints() []Constraint

Constraints returns each one of the Cond entries as constraints.

func (Cond) Empty

func (c Cond) Empty() bool

func (Cond) Expressions

func (c Cond) Expressions() []LogicalExpr

func (Cond) Operator

func (c Cond) Operator() LogicalOperator

func (Cond) String

func (c Cond) String() string

type Constraint

type Constraint interface {
	// Key is the leftmost part of the constraint and usually contains a column
	// name.
	Key() interface{}
	// Value if the rightmost part of the constraint and usually contains a column
	// value.
	Value() interface{}
}

Constraint represents a single condition like "a = 1", where "a" is the key and "1" is the value.

func NewConstraint

func NewConstraint(key interface{}, value interface{}) Constraint

NewConstraint constructs a new constraint with the given key and value.

type Constraints

type Constraints interface {
	// Constraints returns the list of constraints.
	Constraints() []Constraint
}

Constraints represents a list of constraints, like "a = 1, b = 2, c = 3".

type FuncExpr

type FuncExpr struct {
	*RawExpr
}

FuncExpr is similar to RawExpr but is designed for database functions.

func Func

func Func(name string, args ...interface{}) *FuncExpr

Func returns a database function expression.

Examples:

	 => MOD(29, 9)
  expr.Func("MOD", 29, 9)

  => CONCAT("foo", "bar")
  expr.Func("CONCAT", "foo", "bar")

  => NOW()
  expr.Func("NOW")

  => RTRIM("Hello  ")
  expr.Func("RTRIM", "Hello  ")

func (*FuncExpr) Name

func (e *FuncExpr) Name() string

Name returns the name of the function.

type LogicalExpr

type LogicalExpr interface {
	// Expressions returns all child expressions in the logical expression.
	Expressions() []LogicalExpr
	// Operator returns the Operator that joins all the child expressions in the
	// logical expression.
	Operator() LogicalOperator
	// Empty returns true if the logical expression has no child expressions.
	Empty() bool

	fmt.Stringer
}

LogicalExpr represents an expression to be used in logical statements. It consists of a group of expressions joined by operators like LogicalAnd or LogicalOr.

func Logical

func Logical(op LogicalOperator, exprs ...LogicalExpr) LogicalExpr

Logical constructs a LogicalExpr with given operator and expressions.

type LogicalOperator

type LogicalOperator uint

LogicalOperator is a logical operation on a compound statement.

const (
	LogicalNone LogicalOperator = iota
	LogicalAnd
	LogicalOr
)

func (LogicalOperator) String

func (op LogicalOperator) String() string

type OrExpr

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

OrExpr is an expression that joins child expressions by logical disjunction.

func Or

func Or(exprs ...LogicalExpr) *OrExpr

Or joins given expressions by logical disjunction (LogicalOr). Expressions can be represented by mixes of `expr.Cond{}`, `expr.Or()`, `expr.And()` and `expr.Raw()`.

Example:

=> year = 2012 OR year = 1987
expr.Or(
    expr.Cond{"year": 2012},
    expr.Cond{"year": 1987},
)

func (OrExpr) Base

func (e OrExpr) Base() interface{}

func (OrExpr) Empty

func (e OrExpr) Empty() bool

func (OrExpr) Expressions

func (e OrExpr) Expressions() []LogicalExpr

func (OrExpr) Fn

func (e OrExpr) Fn(in interface{}) error

func (OrExpr) Operator

func (e OrExpr) Operator() LogicalOperator

func (*OrExpr) Or

func (e *OrExpr) Or(ors ...LogicalExpr) *OrExpr

Or adds more OR conditions to the expression.

func (OrExpr) Prev

func (e OrExpr) Prev() immutable.Immutable

func (OrExpr) String

func (e OrExpr) String() string

type RawExpr

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

RawExpr is a raw expression that can bypass SQL filters.

func Bool

func Bool(value bool) *RawExpr

Bool returns a RawExpr based on the given boolean value.

func Raw

func Raw(value string, args ...interface{}) *RawExpr

Raw keeps the given value and arguments unfiltered and passes them directly to the query .

CAUTION: It is possible to cause SQL injection if user inputs are not properly sanitized before giving to this function.

Example:

=> SOUNDEX('Hello')
expr.Raw("SOUNDEX('Hello')")

func (*RawExpr) Arguments

func (e *RawExpr) Arguments() []interface{}

Arguments returns arguments of the raw expression. It returns nil if there is no argument.

func (*RawExpr) Empty

func (e *RawExpr) Empty() bool

func (*RawExpr) Expressions

func (e *RawExpr) Expressions() []LogicalExpr

func (RawExpr) Operator

func (e RawExpr) Operator() LogicalOperator

func (RawExpr) Raw

func (e RawExpr) Raw() string

Raw returns the value of the raw expression.

func (RawExpr) String

func (e RawExpr) String() string

Jump to

Keyboard shortcuts

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