ensql

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UnknownQueryType = iota
	SelectQuery
)
View Source
const (
	SELECT   = "SELECT"
	FROM     = "FROM"
	WHERE    = "WHERE"
	AS       = "AS"
	OFFSET   = "OFFSET"
	LIMIT    = "LIMIT"
	EQ       = "="
	NE       = "!="
	GT       = ">"
	LT       = "<"
	GTE      = ">="
	LTE      = "<="
	AND      = "AND"
	OR       = "OR"
	LIKE     = "LIKE"
	ILIKE    = "ILIKE"
	ASTERISK = "*"
	COMMA    = ","
	DOT      = "."
	LP       = "("
	RP       = ")"
	SC       = ";"
	SQUOTE   = '\''
	MINUS    = '-'
	ESCAPE   = '\\'
)

Reserved Words constants

Variables

View Source
var (
	ErrEmptyQuery             = errors.New("empty query is invalid")
	ErrMissingTopic           = errors.New("topic name cannot be empty")
	ErrUnhandledStep          = errors.New("parser has reached an unhandled state")
	ErrNoFieldsSelected       = errors.New("SELECT requires field projection or *")
	ErrInvalidSelectAllFields = errors.New("cannot select * and specify fields")
	ErrNonNumeric             = errors.New("cannot parse non-numeric token as a number")
)
View Source
var (
	Empty = Token{"", EmptyToken, 0}
)
View Source
var ReservedWords = []string{
	SELECT, FROM, WHERE, AS, OFFSET, LIMIT,
	EQ, NE, GTE, LTE, GT, LT, AND, OR, LIKE, ILIKE,
	ASTERISK, COMMA, DOT, LP, RP, SC,
}

NOTE: GT and LT must follow GTE and LTE in this list (or in general, any word that is a prefix of another word must follow that word to ensure parsing is correct).

Functions

func InvalidState

func InvalidState(expected, actual string) error

InvalidState is a developer error; it means that the parser proceeded to a step but modified the underlying state of the parsing incorrectly. This is not a user error, e.g. due to syntax, so invalid state usually panics.

Types

type Condition

type Condition struct {
	Left     Token
	Operator Operator
	Right    Token
}

type Operator

type Operator uint8

Operator fields for where clauses and conditional queries

const (
	UnknownOperator Operator = iota
	Eq                       // =
	Ne                       // !=
	Gt                       // >
	Lt                       // <
	Gte                      // >=
	Lte                      // <=
	Like                     // like
	ILike                    // ilike
	And                      // AND
	Or                       // OR
)

func (Operator) String

func (o Operator) String() string

type Query

type Query struct {
	Type       QueryType
	Topic      Topic
	Conditions []Condition
	Fields     []Token
	Aliases    map[string]string
	Offset     uint64
	HasOffset  bool
	Limit      uint64
	HasLimit   bool
	Raw        string
}

Query is a parsed representation of an EnSQL statement that can be used to process EnSQL requests. All elements from the SQL statement must be represented in the query (including things like comments if supported by EnSQL). The raw sql is also available on the query for debugging purposes.

func Parse

func Parse(sql string) (Query, error)

Parse an EnSQL statement to create a Query object for an Ensign SQL execution. An error is returned on syntax or validation errors that occur during parsing.

func (Query) String

func (q Query) String() string

The raw query is returned as the string representation of the query.

type QueryType

type QueryType uint8

The type of the EnSQL query (e.g. SELECT)

func (QueryType) String

func (q QueryType) String() string

type SyntaxError

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

func Error

func Error(pos int, near, msg string) *SyntaxError

func Errorf

func Errorf(pos int, near, format string, args ...interface{}) *SyntaxError

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type Token

type Token struct {
	Token  string
	Type   TokenType
	Length int
}

A token represents a parsed element from the SQL and is returned from peek. The token string may not match the original string in the query (for example it might be uppercased or have quotations or whitespace stripped). When evaluating tokens, both the token type and the token itself should be used in concert to ensure correct normalization has occurred. The length is used to advance the parser index and may not match the length of the parsed token string.

func Tokenize

func Tokenize(sql string) []Token

Tokenize returns the tokens parsed from the input string with no validation or FSM. This function is primarily used by tests but can also be used by debugging tools to determine how a SQL query is being parsed.

func (Token) ParseFloat

func (t Token) ParseFloat(bitSize int) (float64, error)

Parse a numeric token as a float using strconv.ParseFloat. Generally, the bitSize should be 64 (e.g. double) unless otherwise defined by the schema.

func (Token) ParseInt

func (t Token) ParseInt(base, bitSize int) (int64, error)

Parse a numeric token as a signed integer using strconv.ParseInt. Generally, the base should be 10 and the bitSize should be 64 unless otherwise defined by the schema.

func (Token) ParseUint

func (t Token) ParseUint(base, bitSize int) (uint64, error)

Parse a numeric token as an unsigned integer using strconv.ParseUint. Generally, the base should be 10 and the bitSize should be 64 unless otherwise defined by the schema.

type TokenType

type TokenType uint8
const (
	UnknownTokenType TokenType = iota
	EmptyToken
	ReservedWord
	OperatorToken
	Asterisk
	Punctuation
	Identifier
	QuotedString
	Numeric
)

type Topic

type Topic struct {
	Topic   string
	Schema  string
	Version uint32
}

Represents the topic and event types that are processed by the query.

Jump to

Keyboard shortcuts

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