queryparser

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package queryparser is a parser to parse a simplified select statement (a la SQL) for the Vanadium key value store (a.k.a., syncbase).

The select is of the form:

<query_specification> ::=

<select_statement>
| <delete_statement>

<select_statement> ::=

<select_clause> <from_clause> [<where_clause>] [<escape_limit_offset_clause>...]

<delete_statement> ::=

delete <from_clause> [<where_clause>] [<escape_limit_clause>...]

<select_clause> ::= SELECT <selector> [{<comma><selector>}...]

<from_clause> ::= FROM <table>

<where_clause> ::= WHERE <expression>

<escape_limit_offset_clause> ::=

ESCAPE <char_literal>
| LIMIT <int_literal>
| OFFSET <int_literal>

<escape_limit_clause> ::=

ESCAPE <char_literal>
| LIMIT <int_literal>

<selector> ::= <column> [AS <string_literal>]

<column> ::=

k
| v[<period><field>]
| <function>

<field> ::= <segment>[{<period><segment>}...]

<segment> ::= <identifier>[<keys>]

<keys> ::= <key>...

<key> ::= <left_bracket> <operand> <right_bracket>

<function> ::= <identifier><left_paren>[<operand>[{<comma><operand>}...]<right_paren>

<table> ::= <identifier>

<expression> ::=

<left_paren> <expression> <right_paren>
| <logical_expression>
| <binary_expression>

<logical_expression> ::=

<expression> <logical_op> <expression>

<logical_op> ::=

AND
| OR

<binary_expression> ::=

<operand> <binary_op> <operand>
| v[<period><field>] IS [NOT] NIL

<operand> ::=

k
| v[<period><field>]
| <literal>
| <function>

<binary_op> ::=

=
| EQUAL
| <>
| NOT EQUAL
| LIKE
| NOT LIKE
| <
| <=
| >=
| >

<literal> ::=

<string_literal>
| <bool_literal>
| <int_literal>
| <float_literal>

Example: select v.Foo.Far, v.Baz[2] from Foobarbaz where Type(v) like "%.Customer" and (v.Foo = 42 and v.Bar not like "abc%) or (k >= "100" and k < "200")

Index

Constants

View Source
const (
	// MaxStatementLen defines the maximum length of a statement.
	MaxStatementLen = 10000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AsClause

type AsClause struct {
	AltName Name
	Node
}

func (AsClause) String

func (a AsClause) String() string

type BinaryOperator

type BinaryOperator struct {
	Type BinaryOperatorType
	Node
}

func (BinaryOperator) String

func (o BinaryOperator) String() string

type BinaryOperatorType

type BinaryOperatorType int
const (
	And BinaryOperatorType = 1 + iota
	Equal
	GreaterThan
	GreaterThanOrEqual
	Is
	IsNot
	LessThan
	LessThanOrEqual
	Like
	NotEqual
	NotLike
	Or
)

Values for BinaryOperatorType.

type CharValue

type CharValue struct {
	Value rune
	Node
}

func (CharValue) String

func (c CharValue) String() string

type DeleteStatement

type DeleteStatement struct {
	From   *FromClause
	Where  *WhereClause
	Escape *EscapeClause
	Limit  *LimitClause
	Node
}

func (DeleteStatement) CopyAndSubstitute

func (st DeleteStatement) CopyAndSubstitute(db ds.Database, paramValues []*vdl.Value) (Statement, error)

func (DeleteStatement) Offset

func (st DeleteStatement) Offset() int64

func (DeleteStatement) String

func (st DeleteStatement) String() string

Pretty string of delete statement.

type EscapeClause

type EscapeClause struct {
	EscapeChar *CharValue
	Node
}

func (EscapeClause) String

func (e EscapeClause) String() string

type Expression

type Expression struct {
	Operand1 *Operand
	Operator *BinaryOperator
	Operand2 *Operand
	Node
}

func (Expression) CopyAndSubstitute

func (e Expression) CopyAndSubstitute(db ds.Database, pi *paramInfo) (*Expression, error)

func (Expression) String

func (e Expression) String() string

type Field

type Field struct {
	Segments []Segment
	Node
}

func ParseIndexField

func ParseIndexField(db ds.Database, fieldName, tableName string) (*Field, error)

ParseIndexField is used to parse datasource supplied index fields. It creates a new scanner with the contents of the field name and only succeeds if the result of parsing the contents of the scan is a field and there is nothing left over. Note: This function is NOT involved in the parsing of the AST. Offsets of 0 are

returned on error as these errors are unrelated to the input query.  They
are configuration errors in the datasource.

func (Field) String

func (f Field) String() string

type FromClause

type FromClause struct {
	Table TableEntry
	Node
}

func (FromClause) String

func (f FromClause) String() string

type Function

type Function struct {
	Name     string
	Args     []*Operand
	ArgTypes []OperandType // Filled in by checker.
	RetType  OperandType   // Filled in by checker.
	Computed bool          // Checker sets to true and sets RetValue if function takes no args
	RetValue *Operand
	Node
}

func (Function) CopyAndSubstitute

func (f Function) CopyAndSubstitute(db ds.Database, pi *paramInfo) (*Function, error)

func (Function) String

func (f Function) String() string

type Int64Value

type Int64Value struct {
	Value int64
	Node
}

func (Int64Value) String

func (i Int64Value) String() string

type LimitClause

type LimitClause struct {
	Limit *Int64Value
	Node
}

func (LimitClause) String

func (l LimitClause) String() string

type Name

type Name struct {
	Value string
	Node
}

func (Name) String

func (n Name) String() string

type Node

type Node struct {
	Off int64
}

type Operand

type Operand struct {
	Type     OperandType
	BigInt   *big.Int
	BigRat   *big.Rat
	Bool     bool
	Column   *Field
	Float    float64
	Function *Function
	Int      int64
	Str      string
	Time     time.Time
	Prefix   string           // Computed by checker for Like expressions
	Pattern  *pattern.Pattern // Computed by checker for Like expressions
	Uint     uint64
	Expr     *Expression
	Object   *vdl.Value
	Node
}

func ConvertValueToAnOperand

func ConvertValueToAnOperand(value *vdl.Value, off int64) (*Operand, error)

func (Operand) CopyAndSubstitute

func (o Operand) CopyAndSubstitute(db ds.Database, pi *paramInfo) (*Operand, error)

func (Operand) String

func (o Operand) String() string

type OperandType

type OperandType int
const (
	TypBigInt OperandType = 1 + iota // Only as a result of Resolve/Coerce Operand
	TypBigRat                        // Only as a result of Resolve/Coerce Operand
	TypBool
	TypExpr
	TypField
	TypFloat
	TypFunction
	TypInt
	TypNil
	TypParameter
	TypStr
	TypTime
	TypObject // Only as the result of a ResolveOperand
	TypUint   // Only as the result of a ResolveOperand
)

Values for OperandType.

type ResultsOffsetClause

type ResultsOffsetClause struct {
	ResultsOffset *Int64Value
	Node
}

func (ResultsOffsetClause) String

func (l ResultsOffsetClause) String() string

type Segment

type Segment struct {
	Value string
	Keys  []*Operand // Used as key(s) or index(es) to dereference map/set/array/list.
	Node
}

func (Segment) String

func (s Segment) String() string

type SelectClause

type SelectClause struct {
	Selectors []Selector
	Node
}

func (SelectClause) String

func (sel SelectClause) String() string

type SelectStatement

type SelectStatement struct {
	Select        *SelectClause
	From          *FromClause
	Where         *WhereClause
	Escape        *EscapeClause
	Limit         *LimitClause
	ResultsOffset *ResultsOffsetClause
	Node
}

func (SelectStatement) CopyAndSubstitute

func (st SelectStatement) CopyAndSubstitute(db ds.Database, paramValues []*vdl.Value) (Statement, error)

func (SelectStatement) Offset

func (st SelectStatement) Offset() int64

func (SelectStatement) String

func (st SelectStatement) String() string

Pretty string of select statement.

type Selector

type Selector struct {
	Type     SelectorType
	Field    *Field
	Function *Function
	As       *AsClause // If not nil, used in returned column header.
	Node
}

Selector: entries in the select clause. Entries can be functions for fields. The AS name, if present, will ONLY be used in the returned column header.

func (Selector) String

func (s Selector) String() string

type SelectorType

type SelectorType int
const (
	TypSelField SelectorType = 1 + iota
	TypSelFunc
)

Values for SelectorType.

type Statement

type Statement interface {
	Offset() int64
	String() string
	CopyAndSubstitute(db ds.Database, paramValues []*vdl.Value) (Statement, error)
}

func Parse

func Parse(db ds.Database, src string) (*Statement, error)

Parse a statement. Return it or an error.

type TableEntry

type TableEntry struct {
	Name    string
	DBTable ds.Table // Checker gets table from db and sets this.
	Node
}

func (TableEntry) String

func (t TableEntry) String() string

type Token

type Token struct {
	Tok   TokenType
	Value string
	Off   int64
}

type TokenType

type TokenType int
const (
	TokCHAR TokenType = 1 + iota
	TokCOMMA
	TokEOF
	TokEQUAL
	TokFLOAT
	TokIDENT
	TokINT
	TokLEFTANGLEBRACKET
	TokLEFTBRACKET
	TokLEFTPAREN
	TokMINUS
	TokParameter // allowed only in prepared statements
	TokPERIOD
	TokRIGHTANGLEBRACKET
	TokRIGHTBRACKET
	TokRIGHTPAREN
	TokSTRING
	TokERROR
)

Definitions of token type values.

type WhereClause

type WhereClause struct {
	Expr *Expression
	Node
}

func (WhereClause) CopyAndSubstitute

func (w WhereClause) CopyAndSubstitute(db ds.Database, paramValues []*vdl.Value) (*WhereClause, error)

func (WhereClause) String

func (w WhereClause) String() string

Jump to

Keyboard shortcuts

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