csvquery

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 9, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package csvquery parse and analyzes sql like query string.

Index

Constants

View Source
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"
)
View Source
const ConditionPrefix = "COND"

ConditionPrefix contains string condition prefix which is using in ConditionMap.

Variables

View Source
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")
)
View Source
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)
)

ComparisonOperators contains list of possible comparison operators.

View Source
var ErrPopEmptyStack = errors.New("stack is empty")

ErrPopEmptyStack returns error if a token stack is empty.

Functions

func Calc

func Calc(left, right bool, op LogicalOperator) bool

Calc calculates binary statement.

func GetPriority

func GetPriority(operator LogicalOperator) int

GetPriority returns priority of the binary operator.

func IsOperator

func IsOperator(operator string) bool

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 Column

type Column string

Column describes table column.

type Columns

type Columns []Column

Columns describes list of table columns.

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.

func (*Condition) CheckCondition

func (c *Condition) CheckCondition(value string) (bool, error)

CheckCondition checks condition.

type ConditionMap

type ConditionMap map[string]*Condition

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) Size

func (n *InfixNotation) Size() int

Size returns count of tokens.

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 NewQuery

func NewQuery(query string, logger *zap.Logger) *Query

NewQuery returns the query.

func (*Query) Parse

func (q *Query) Parse() error

Parse parses the sql like query string.

func (*Query) ParseFromStatement

func (q *Query) ParseFromStatement() error

ParseFromStatement parses from statement.

func (*Query) ParseSelectStatement

func (q *Query) ParseSelectStatement() error

ParseSelectStatement parses select statement.

func (*Query) ParseWhereStatement

func (q *Query) ParseWhereStatement() error

ParseWhereStatement parses where statement.

type QueryColumns

type QueryColumns []Column

QueryColumns describes list of unique table columns.

type Table

type Table string

Table describes table.

type Tables

type Tables []Table

Tables describes list of tables.

type ValueType

type ValueType uint

ValueType describes value type of condition value.

const (
	// TypeNumber return condition number value type.
	TypeNumber ValueType = iota
	// TypeString return condition string value type.
	TypeString
)

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.

func (*WhereParser) Parse

func (p *WhereParser) Parse() (map[Column]int, *structs.Tree, error)

Parse parses the where statement.

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.

Jump to

Keyboard shortcuts

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