filter

package
v0.0.0-...-0b9d80d Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2016 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package filter parses and evaluates Chain filter expressions. A predicate is a boolean expression with zero or more placeholder values ($1, $2, etc) that are initially unconstrained. The predicate is evaluated in an environment (such as a transaction object or a UTXO) that determines the value of all non-placeholder terms. The predicate and its fixed values together constrain the placeholders. Function Eval finds the set of all placeholder values that satisfy the predicate.

Expressions in a filter expression have the following forms:

Form                     Type     Subexpression types
expr1 "OR" expr2         bool     bool, bool
expr1 "AND" expr2        bool     bool, bool
ident "(" expr ")"       bool     list, bool
expr1 "=" expr2          bool     any (must match)
expr "." ident           any      object
"(" expr ")"             any      any
ident                    any      n/a
placeholder              scalar   n/a
string                   string   n/a
int                      int      n/a

ident is an alphanumeric identifier
placeholder is a decimal int with prefix "$"
scalar means int or string
string is single-quoted, and cannot contain backslash
int is decimal or hexadecimal (with prefix "0x")
list is a slice of environments

The environment is a map from names to values. Identifier expressions get their values from the environment map.

The form 'ident(expr)' is an existential quantifier. The environment value for 'ident' must be a list of subenvironments. The subexpression 'expr' is evaluated in each subenvironment, and if there exists one subenvironment for which 'expr' is true, the expression as a whole is true.

Filters are statically type-checked: if a subexpression doesn't have the appropriate type, Parse will return an error.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadFilter = errors.New("invalid query filter")

ErrBadFilter is returned from Parse when it encounters an invalid filter expression.

Functions

func FieldAsSQL

func FieldAsSQL(col string, f Field) string

FieldAsSQL returns a jsonb indexing SQL representation of the field.

Types

type Field

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

Field is a type for simple expressions that simply access an attribute of the queried object. They're used for GROUP BYs.

func ParseField

func ParseField(s string) (f Field, err error)

ParseField parses a field expression (either an attrExpr or a selectorExpr).

func (Field) String

func (f Field) String() string

type Predicate

type Predicate struct {
	Parameters int
	// contains filtered or unexported fields
}

Predicate represents a parsed filter predicate.

func Parse

func Parse(predicate string) (p Predicate, err error)

Parse parses a predicate and returns an internal representation of the predicate or an error if it fails to parse.

func (Predicate) MarshalText

func (p Predicate) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface and returns a cleaned, canonical representation of the predicate.

func (Predicate) String

func (p Predicate) String() string

String returns a cleaned, canonical representation of the predicate.

type SQLExpr

type SQLExpr struct {
	SQL    string
	Values []interface{}
}

func AsSQL

func AsSQL(p Predicate, dataColumn string, values []interface{}) (sqlExpr SQLExpr, err error)

AsSQL translates p to SQL.

type Type

type Type int

Type defines the value types in filter expressions.

const (
	Any Type = iota
	Bool
	String
	Integer
	Object
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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