Documentation
¶
Overview ¶
Package csvquery parse and analyzes sql like query string.
Index ¶
- Constants
- Variables
- func Calc(left, right bool, op LogicalOperator) bool
- func GetPriority(operator LogicalOperator) int
- func IsOperator(operator string) bool
- func IsSameOperator(strOp string, op LogicalOperator) bool
- type Column
- type Columns
- type ComparisonOperator
- type Condition
- type ConditionMap
- type InfixNotation
- type LogicalOperator
- type Query
- type QueryColumns
- type Table
- type Tables
- type ValueType
- type WhereParser
- type WhereToken
- type WhereTokenStack
Constants ¶
const ( // SelectKeyword returns SELECT keyword. SelectKeyword keyword = "SELECT" // FromKeyword returns FROM keyword. FromKeyword keyword = "FROM" // WhereKeyword returns WHERE keyword. WhereKeyword keyword = "WHERE" // AndKeyword returns AND keyword. AndKeyword keyword = "AND" // OrKeyword returns OR keyword. OrKeyword keyword = "OR" )
const ConditionPrefix = "COND"
ConditionPrefix contains string condition prefix which is using in ConditionMap.
Variables ¶
var ( // ErrUnknownValueType describes unknown value type of condition value error. ErrUnknownValueType = errors.New("unknown value type of condition value") // ErrUnknownComparisonOperator describes unknown comparison operator error. ErrUnknownComparisonOperator = errors.New("unknown comparison operator") // ErrCastInterfaceToString error if script can't cast interface to string. ErrCastInterfaceToString = errors.New("can't cast interface to string") // ErrCastInterfaceToFloat64 error if script can't cast interface to float64. ErrCastInterfaceToFloat64 = errors.New("can't cast interface to float64") // ErrConvertToFloat64 error if script can't convert float64. ErrConvertToFloat64 = errors.New("can't convert float64") )
var ( // ErrIncorrectQuery return error if query string is incorrect. ErrIncorrectQuery = errors.New("incorrect query") // ErrIncorrectBracketPosition return error if query string has incorrect bracket positions in where statement. ErrIncorrectBracketPosition = fmt.Errorf("%w: incorrect bracket positions in where statement", ErrIncorrectQuery) // ErrTooManyStarColumns returns error if query string has more than one star in select statement. ErrTooManyStarColumns = fmt.Errorf("%w: too many star columns", ErrIncorrectQuery) )
var ComparisonOperators = []ComparisonOperator{ EqualOperator, NotEqualOperator, LessOperator, LessOrEqualOperator, GreaterOperator, GreaterOrEqualOperator, }
ComparisonOperators contains list of possible comparison operators.
var ErrPopEmptyStack = errors.New("stack is empty")
ErrPopEmptyStack returns error if a token stack is empty.
Functions ¶
func GetPriority ¶
func GetPriority(operator LogicalOperator) int
GetPriority returns priority of the binary operator.
func IsOperator ¶
IsOperator returns true if operator is one of the logical operators.
func IsSameOperator ¶
func IsSameOperator(strOp string, op LogicalOperator) bool
IsSameOperator returns true if strOp and op describes the same operator and false otherwise.
Types ¶
type ComparisonOperator ¶
type ComparisonOperator string
ComparisonOperator describes comparison operator type.
const ( // EqualOperator describes equal operator. EqualOperator ComparisonOperator = "=" // NotEqualOperator describes "not equal" operator. NotEqualOperator ComparisonOperator = "!=" // LessOperator describes less operator LessOperator ComparisonOperator = "<" // LessOrEqualOperator describes less or equal operator. LessOrEqualOperator ComparisonOperator = "<=" // GreaterOperator describes greater operator. GreaterOperator ComparisonOperator = ">" // GreaterOrEqualOperator describes greater or equal operator. GreaterOrEqualOperator ComparisonOperator = ">=" )
type Condition ¶
type Condition struct { Column Column Op ComparisonOperator ValueType ValueType Value interface{} }
Condition describes one condition in where statement.
type ConditionMap ¶
ConditionMap contains mapping strings conds to Condition structs.
func (*ConditionMap) Add ¶
func (cm *ConditionMap) Add(cond *Condition) string
Add adds unique Condition to the map.
type InfixNotation ¶
type InfixNotation struct {
// contains filtered or unexported fields
}
InfixNotation describes list of tokens in infix notation.
func NewInfixNotation ¶
func NewInfixNotation() *InfixNotation
NewInfixNotation return new infix notation instance.
func (*InfixNotation) AddToken ¶
func (n *InfixNotation) AddToken(token string)
AddToken adds new token.
func (*InfixNotation) ToPostfix ¶
func (n *InfixNotation) ToPostfix() []string
ToPostfix returns list of tokens in postfix notation.
type LogicalOperator ¶
type LogicalOperator string
LogicalOperator describes logical operator type.
const ( // AndOperator describes AND logical operator. AndOperator LogicalOperator = "AND" // OrOperator describes OR logical operator. OrOperator LogicalOperator = "OR" )
type Query ¶
type Query struct { Select Columns StarColumn bool From Tables Where *structs.Tree UsedColumns QueryColumns // contains filtered or unexported fields }
A Query describes a query string.
func (*Query) ParseFromStatement ¶
ParseFromStatement parses from statement.
func (*Query) ParseSelectStatement ¶
ParseSelectStatement parses select statement.
func (*Query) ParseWhereStatement ¶
ParseWhereStatement parses where statement.
type WhereParser ¶
type WhereParser struct {
// contains filtered or unexported fields
}
WhereParser contains where statement parser data.
func NewWhereParser ¶
func NewWhereParser(where string, logger *zap.Logger) *WhereParser
NewWhereParser returns new where statement parser.
type WhereToken ¶
type WhereToken string
WhereToken describes token type.
const ( // OpenBracketToken returns the opening parenthesis token. OpenBracketToken WhereToken = "(" // CloseBracketToken returns the closing parenthesis token. CloseBracketToken WhereToken = ")" // CondToken returns the condition token. CondToken WhereToken = "COND" // BinaryOpToken returns the operation token. BinaryOpToken WhereToken = "OP" )
type WhereTokenStack ¶
type WhereTokenStack struct {
// contains filtered or unexported fields
}
WhereTokenStack contains a stack of tokens.
func (*WhereTokenStack) IsEmpty ¶
func (s *WhereTokenStack) IsEmpty() bool
IsEmpty return true if the stack is empty and false otherwise.
func (*WhereTokenStack) IsNotEmpty ¶
func (s *WhereTokenStack) IsNotEmpty() bool
IsNotEmpty return true if the stack is not empty and false otherwise.
func (*WhereTokenStack) IsTopEqual ¶
func (s *WhereTokenStack) IsTopEqual(val WhereToken) bool
IsTopEqual return true if the last element of the stack is equal to val and false otherwise.
func (*WhereTokenStack) Pop ¶
func (s *WhereTokenStack) Pop() (WhereToken, error)
Pop pops the string element off the end of the stack and ErrPopEmptyStack error if the stack is empty.
func (*WhereTokenStack) Push ¶
func (s *WhereTokenStack) Push(value WhereToken)
Push adds a token to the stack.