query

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrResultClosed = errors.New("result already closed")

ErrResultClosed is returned when trying to close an already closed result.

Functions

This section is empty.

Types

type AndOp

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

AndOp is the And operator.

func And

func And(a, b Expr) *AndOp

And creates an expression that evaluates a And b And returns true if both are truthy.

func (*AndOp) Eval

func (op *AndOp) Eval(ctx EvalStack) (document.Value, error)

Eval implements the Expr interface. It evaluates a and b and returns true if both evalutate to true.

func (AndOp) LeftHand

func (op AndOp) LeftHand() Expr

func (AndOp) Precedence

func (op AndOp) Precedence() int

func (AndOp) RightHand

func (op AndOp) RightHand() Expr

func (AndOp) SetLeftHandExpr

func (op AndOp) SetLeftHandExpr(a Expr)

func (AndOp) SetRightHandExpr

func (op AndOp) SetRightHandExpr(b Expr)

type Cast added in v0.5.0

type Cast struct {
	Expr      Expr
	ConvertTo document.ValueType
}

Cast represents the CAST expression. It returns the primary key of the current document.

func (Cast) Eval added in v0.5.0

func (c Cast) Eval(ctx EvalStack) (document.Value, error)

Eval returns the primary key of the current document.

type CmpOp

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

A CmpOp is a comparison operator.

func Eq

func Eq(a, b Expr) CmpOp

Eq creates an expression that returns true if a equals b.

func Gt

func Gt(a, b Expr) CmpOp

Gt creates an expression that returns true if a is greater than b.

func Gte

func Gte(a, b Expr) CmpOp

Gte creates an expression that returns true if a is greater than or equal to b.

func Lt

func Lt(a, b Expr) CmpOp

Lt creates an expression that returns true if a is lesser than b.

func Lte

func Lte(a, b Expr) CmpOp

Lte creates an expression that returns true if a is lesser than or equal to b.

func Neq

func Neq(a, b Expr) CmpOp

Neq creates an expression that returns true if a equals b.

func NewCmpOp added in v0.5.0

func NewCmpOp(a, b Expr, t scanner.Token) CmpOp

NewCmpOp creates a comparison operator.

func (CmpOp) Eval

func (op CmpOp) Eval(ctx EvalStack) (document.Value, error)

Eval compares a and b together using the operator specified when constructing the CmpOp and returns the result of the comparison.

func (CmpOp) LeftHand

func (op CmpOp) LeftHand() Expr

func (CmpOp) Precedence

func (op CmpOp) Precedence() int

func (CmpOp) RightHand

func (op CmpOp) RightHand() Expr

func (CmpOp) SetLeftHandExpr

func (op CmpOp) SetLeftHandExpr(a Expr)

func (CmpOp) SetRightHandExpr

func (op CmpOp) SetRightHandExpr(b Expr)

type CreateIndexStmt

type CreateIndexStmt struct {
	IndexName   string
	TableName   string
	Path        document.ValuePath
	IfNotExists bool
	Unique      bool
}

CreateIndexStmt is a DSL that allows creating a full CREATE INDEX statement. It is typically created using the CreateIndex function.

func (CreateIndexStmt) IsReadOnly

func (stmt CreateIndexStmt) IsReadOnly() bool

IsReadOnly always returns false. It implements the Statement interface.

func (CreateIndexStmt) Run

Run runs the Create index statement in the given transaction. It implements the Statement interface.

type CreateTableStmt

type CreateTableStmt struct {
	TableName   string
	IfNotExists bool
	Config      database.TableConfig
}

CreateTableStmt is a DSL that allows creating a full CREATE TABLE statement.

func (CreateTableStmt) IsReadOnly

func (stmt CreateTableStmt) IsReadOnly() bool

IsReadOnly always returns false. It implements the Statement interface.

func (CreateTableStmt) Run

Run runs the Create table statement in the given transaction. It implements the Statement interface.

type DeleteStmt

type DeleteStmt struct {
	TableName string
	WhereExpr Expr
}

DeleteStmt is a DSL that allows creating a full Delete query.

func (DeleteStmt) IsReadOnly

func (stmt DeleteStmt) IsReadOnly() bool

IsReadOnly always returns false. It implements the Statement interface.

func (DeleteStmt) Run

func (stmt DeleteStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error)

Run deletes matching documents by batches of deleteBufferSize documents. Some engines can't iterate while deleting keys (https://github.com/etcd-io/bbolt/issues/146) and some can't create more than one iterator per read-write transaction (https://github.com/dgraph-io/badger/issues/1093). To deal with these limitations, Run will iterate on a limited number of documents, copy the keys to a buffer and delete them after the iteration is complete, and it will do that until there is no document left to delete. Increasing deleteBufferSize will occasionate less key searches (O(log n) for most engines) but will take more memory.

type DropIndexStmt

type DropIndexStmt struct {
	IndexName string
	IfExists  bool
}

DropIndexStmt is a DSL that allows creating a DROP INDEX query.

func (DropIndexStmt) IsReadOnly

func (stmt DropIndexStmt) IsReadOnly() bool

IsReadOnly always returns false. It implements the Statement interface.

func (DropIndexStmt) Run

func (stmt DropIndexStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error)

Run runs the DropIndex statement in the given transaction. It implements the Statement interface.

type DropTableStmt

type DropTableStmt struct {
	TableName string
	IfExists  bool
}

DropTableStmt is a DSL that allows creating a DROP TABLE query.

func (DropTableStmt) IsReadOnly

func (stmt DropTableStmt) IsReadOnly() bool

IsReadOnly always returns false. It implements the Statement interface.

func (DropTableStmt) Run

func (stmt DropTableStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error)

Run runs the DropTable statement in the given transaction. It implements the Statement interface.

type EvalStack

type EvalStack struct {
	Tx       *database.Transaction
	Document document.Document
	Params   []driver.NamedValue
	Cfg      *database.TableConfig
}

EvalStack contains information about the context in which the expression is evaluated. Any of the members can be nil except the transaction.

type Expr

type Expr interface {
	Eval(EvalStack) (document.Value, error)
}

An Expr evaluates to a value.

func Add added in v0.5.0

func Add(a, b Expr) Expr

Add creates an expression thats evaluates to the result of a + b.

func BitwiseAnd added in v0.5.0

func BitwiseAnd(a, b Expr) Expr

BitwiseAnd creates an expression thats evaluates to the result of a & b.

func BitwiseOr added in v0.5.0

func BitwiseOr(a, b Expr) Expr

BitwiseOr creates an expression thats evaluates to the result of a & b.

func BitwiseXor added in v0.5.0

func BitwiseXor(a, b Expr) Expr

BitwiseXor creates an expression thats evaluates to the result of a & b.

func Div added in v0.5.0

func Div(a, b Expr) Expr

Div creates an expression thats evaluates to the result of a / b.

func GetFunc added in v0.5.0

func GetFunc(name string, args ...Expr) (Expr, error)

GetFunc return a function expression by name.

func Mod added in v0.5.0

func Mod(a, b Expr) Expr

Mod creates an expression thats evaluates to the result of a % b.

func Mul added in v0.5.0

func Mul(a, b Expr) Expr

Mul creates an expression thats evaluates to the result of a * b.

func Or

func Or(a, b Expr) Expr

Or creates an expression that first evaluates a, returns true if truthy, then evaluates b, returns true if truthy Or false if falsy.

func Sub added in v0.5.0

func Sub(a, b Expr) Expr

Sub creates an expression thats evaluates to the result of a - b.

type FieldSelector

type FieldSelector []string

A FieldSelector is a ResultField that extracts a field from a document at a given path.

func (FieldSelector) Eval

func (f FieldSelector) Eval(stack EvalStack) (document.Value, error)

Eval extracts the document from the context and selects the right field. It implements the Expr interface.

func (FieldSelector) Name

func (f FieldSelector) Name() string

Name joins the chunks of the fields selector with the . separator.

type InsertStmt

type InsertStmt struct {
	TableName  string
	FieldNames []string
	Values     LiteralExprList
}

InsertStmt is a DSL that allows creating a full Insert query.

func (InsertStmt) IsReadOnly

func (stmt InsertStmt) IsReadOnly() bool

IsReadOnly always returns false. It implements the Statement interface.

func (InsertStmt) Run

func (stmt InsertStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error)

Run the Insert statement in the given transaction. It implements the Statement interface.

type KVPair

type KVPair struct {
	K string
	V Expr
}

KVPair associates an identifier with an expression.

type KVPairs

type KVPairs []KVPair

KVPairs is a list of KVPair.

func (KVPairs) Eval

func (kvp KVPairs) Eval(ctx EvalStack) (document.Value, error)

Eval turns a list of KVPairs into a document.

type LiteralExprList

type LiteralExprList []Expr

LiteralExprList is a list of expressions.

func (LiteralExprList) Eval

func (l LiteralExprList) Eval(stack EvalStack) (document.Value, error)

Eval evaluates all the expressions and returns a litteralValueList. It implements the Expr interface.

type LiteralValue

type LiteralValue document.Value

A LiteralValue represents a litteral value of any type defined by the value package.

func BlobValue added in v0.5.0

func BlobValue(v []byte) LiteralValue

BlobValue creates a litteral value of type Blob.

func BoolValue

func BoolValue(v bool) LiteralValue

BoolValue creates a litteral value of type Bool.

func DocumentValue

func DocumentValue(d document.Document) LiteralValue

DocumentValue creates a litteral value of type Document.

func DurationValue added in v0.5.0

func DurationValue(v time.Duration) LiteralValue

DurationValue creates a litteral value of type Duration.

func Float64Value

func Float64Value(v float64) LiteralValue

Float64Value creates a litteral value of type Float64.

func IntValue

func IntValue(v int) LiteralValue

IntValue creates a litteral value of type Int.

func NullValue

func NullValue() LiteralValue

NullValue creates a litteral value of type Null.

func TextValue added in v0.5.0

func TextValue(v string) LiteralValue

TextValue creates a litteral value of type Text.

func (LiteralValue) Eval

Eval returns l. It implements the Expr interface.

type NamedParam

type NamedParam string

NamedParam is an expression which represents the name of a parameter.

func (NamedParam) Eval

func (p NamedParam) Eval(stack EvalStack) (document.Value, error)

Eval looks up for the parameters in the stack for the one that has the same name as p and returns the value.

type OrOp

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

OrOp is the And operator.

func (*OrOp) Eval

func (op *OrOp) Eval(ctx EvalStack) (document.Value, error)

Eval implements the Expr interface. It evaluates a and b and returns true if a or b evalutate to true.

func (OrOp) LeftHand

func (op OrOp) LeftHand() Expr

func (OrOp) Precedence

func (op OrOp) Precedence() int

func (OrOp) RightHand

func (op OrOp) RightHand() Expr

func (OrOp) SetLeftHandExpr

func (op OrOp) SetLeftHandExpr(a Expr)

func (OrOp) SetRightHandExpr

func (op OrOp) SetRightHandExpr(b Expr)

type PKFunc added in v0.5.0

type PKFunc struct{}

PKFunc represents the pk() function. It returns the primary key of the current document.

func (PKFunc) Eval added in v0.5.0

func (k PKFunc) Eval(ctx EvalStack) (document.Value, error)

Eval returns the primary key of the current document.

type PositionalParam

type PositionalParam int

PositionalParam is an expression which represents the position of a parameter.

func (PositionalParam) Eval

func (p PositionalParam) Eval(stack EvalStack) (document.Value, error)

Eval looks up for the parameters in the stack for the one that is has the same position as p and returns the value.

type Query

type Query struct {
	Statements []Statement
}

A Query can execute statements against the database. It can read or write data from any table, or even alter the structure of the database. Results are returned as streams.

func New

func New(statements ...Statement) Query

New creates a new query with the given statements.

func (Query) Exec

func (q Query) Exec(tx *database.Transaction, args []driver.NamedValue, forceReadOnly bool) (*Result, error)

Exec the query within the given transaction. If the one of the statements requires a read-write transaction and tx is not, tx will get promoted.

func (Query) Run

func (q Query) Run(db *database.Database, args []driver.NamedValue) (*Result, error)

Run executes all the statements in their own transaction and returns the last result.

type Result

type Result struct {
	document.Stream
	// contains filtered or unexported fields
}

Result of a query.

func (*Result) Close

func (r *Result) Close() (err error)

Close the result stream. After closing the result, Stream is not supposed to be used. If the result stream was already closed, it returns ErrResultClosed.

func (Result) LastInsertId

func (r Result) LastInsertId() (int64, error)

LastInsertId is not supported and returns an error. Use LastInsertKey instead.

func (Result) LastInsertKey

func (r Result) LastInsertKey() ([]byte, error)

LastInsertKey returns the database's auto-generated key after, for example, an INSERT into a table with primary key.

func (Result) RowsAffected

func (r Result) RowsAffected() (int64, error)

RowsAffected returns the number of rows affected by the query.

type ResultField

type ResultField interface {
	Iterate(stack EvalStack, fn func(field string, value document.Value) error) error
	Name() string
}

A ResultField is a field that will be part of the result document that will be returned at the end of a Select statement.

type ResultFieldExpr added in v0.5.0

type ResultFieldExpr struct {
	Expr

	ExprName string
}

ResultFieldExpr turns any expression into a ResultField.

func (ResultFieldExpr) Iterate added in v0.5.0

func (r ResultFieldExpr) Iterate(stack EvalStack, fn func(field string, value document.Value) error) error

Iterate evaluates Expr and calls fn once with the result.

func (ResultFieldExpr) Name added in v0.5.0

func (r ResultFieldExpr) Name() string

Name returns the raw expression.

type SelectStmt

type SelectStmt struct {
	TableName        string
	WhereExpr        Expr
	OrderBy          FieldSelector
	OrderByDirection scanner.Token
	OffsetExpr       Expr
	LimitExpr        Expr
	Selectors        []ResultField
}

SelectStmt is a DSL that allows creating a full Select query.

func (SelectStmt) IsReadOnly

func (stmt SelectStmt) IsReadOnly() bool

IsReadOnly always returns true. It implements the Statement interface.

func (SelectStmt) Run

func (stmt SelectStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error)

Run the Select statement in the given transaction. It implements the Statement interface.

type Statement

type Statement interface {
	Run(*database.Transaction, []driver.NamedValue) (Result, error)
	IsReadOnly() bool
}

A Statement represents a unique action that can be executed against the database.

type UpdateStmt

type UpdateStmt struct {
	TableName string
	Pairs     map[string]Expr
	WhereExpr Expr
}

UpdateStmt is a DSL that allows creating a full Update query.

func (UpdateStmt) IsReadOnly

func (stmt UpdateStmt) IsReadOnly() bool

IsReadOnly always returns false. It implements the Statement interface.

func (UpdateStmt) Run

func (stmt UpdateStmt) Run(tx *database.Transaction, args []driver.NamedValue) (Result, error)

Run runs the Update table statement in the given transaction. It implements the Statement interface.

type Wildcard

type Wildcard struct{}

A Wildcard is a ResultField that iterates over all the fields of a document.

func (Wildcard) Iterate

func (w Wildcard) Iterate(stack EvalStack, fn func(fd string, v document.Value) error) error

Iterate call the document iterate method.

func (Wildcard) Name

func (w Wildcard) Name() string

Name returns the "*" character.

Jump to

Keyboard shortcuts

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