parse

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package parse defines a parser for parsing SQL-formatted queries.

The SQL should be formatted as follows:

[[WHERE] <where condition>] [ORDER BY <sort order>] [LIMIT <limit>]

Note that prefixing the WHERE condition with "WHERE" is currently optional, although this might change in the future.

* <where condition>

The following types are supported:

string -	surrounded by matching double- (") or single-quotes (')\
integer -	must fit in an int64 or a uint64
float -		must fit in a float64
boolean -	TRUE or FALSE

The following standard SQL operators are supported:

=, !=
<, >, <=, >=
IS, IS NOT
IN, NOT IN
BETWEEN, NOT BETWEEN
AND
OR

* <sort order>

This should be formatted

key1 [ASC | DESC], key2 [ASC | DESC], ..., keyn [ASC | DESC]

where ASC and DESC denote increasing and decreasing order, respectively. Precisely what this means is determined by the underlying storage engine and data type. If ASC or DESC is omitted, then ASC is assumed by default.

* <limit>

A non-negative integer (that must fit in an int64) must be provided.

Index

Constants

View Source
const (
	String = Type(iota)
	Int64
	Uint64
	Float64
	True
	False
	OpenBracket
	CloseBracket
	Comma
	Where
	Is
	Eq
	Lt
	Gt
	Le
	Ge
	Ne
	Not
	And
	Or
	In
	Between
	Order
	By
	Asc
	Desc
	Limit
	EOF
)

The valid token types.

Variables

This section is empty.

Functions

func ParseTokensOrderBy

func ParseTokensOrderBy(t *Tokeniser) (sort.OrderBy, error)

ParseTokensOrderBy parses a stream of tokens into a sort order.

func ParseTokensWhere

func ParseTokensWhere(t *Tokeniser) (condition.Condition, error)

ParseTokensWhere parses a stream of tokens into a condition.

func SQLOrderBy

func SQLOrderBy(s string) (sort.OrderBy, error)

SQLOrderBy parses an SQL-formatted string into a sort order.

func SQLWhere

func SQLWhere(s string) (condition.Condition, error)

SQLWhere parses an SQL-formatted string into a condition.

Types

type BoolToken

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

BoolToken represents a boolean.

func (*BoolToken) FinishPosition

func (t *BoolToken) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*BoolToken) IsValue

func (*BoolToken) IsValue() bool

IsValue returns true if the token could represent a value.

func (*BoolToken) StartPosition

func (t *BoolToken) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*BoolToken) String

func (t *BoolToken) String() string

String returns the token as a string.

func (*BoolToken) Type

func (t *BoolToken) Type() Type

Type returns the token type.

func (*BoolToken) Value

func (t *BoolToken) Value() interface{}

Value returns the value associated with this token.

type CommaToken

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

CommaToken represents a comma.

func (*CommaToken) FinishPosition

func (t *CommaToken) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*CommaToken) IsValue

func (*CommaToken) IsValue() bool

IsValue returns true if the token could represent a value.

func (*CommaToken) StartPosition

func (t *CommaToken) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*CommaToken) String

func (*CommaToken) String() string

String returns the token as a string.

func (*CommaToken) Type

func (*CommaToken) Type() Type

Type returns the token type.

func (*CommaToken) Value

func (t *CommaToken) Value() interface{}

Value returns nil.

type EOFToken

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

EOFToken represents the end of the stream.

func (*EOFToken) FinishPosition

func (t *EOFToken) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*EOFToken) IsValue

func (*EOFToken) IsValue() bool

IsValue returns true if the token could represent a value.

func (*EOFToken) StartPosition

func (t *EOFToken) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*EOFToken) String

func (*EOFToken) String() string

String returns the token as a string.

func (*EOFToken) Type

func (*EOFToken) Type() Type

Type returns the token type.

func (*EOFToken) Value

func (*EOFToken) Value() interface{}

Value returns nil.

type Float64Token

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

Float64Token represents a float64.

func (*Float64Token) FinishPosition

func (t *Float64Token) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*Float64Token) IsValue

func (*Float64Token) IsValue() bool

IsValue returns true if the token could represent a value.

func (*Float64Token) StartPosition

func (t *Float64Token) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*Float64Token) String

func (t *Float64Token) String() string

String returns the token as a string.

func (*Float64Token) Type

func (*Float64Token) Type() Type

Type returns the token type.

func (*Float64Token) Value

func (t *Float64Token) Value() interface{}

Value returns the value associated with this token.

type Int64Token

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

Int64Token represents an int64.

func (*Int64Token) FinishPosition

func (t *Int64Token) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*Int64Token) IsValue

func (*Int64Token) IsValue() bool

IsValue returns true if the token could represent a value.

func (*Int64Token) StartPosition

func (t *Int64Token) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*Int64Token) String

func (t *Int64Token) String() string

String returns the token as a string.

func (*Int64Token) Type

func (*Int64Token) Type() Type

Type returns the token type.

func (*Int64Token) Value

func (t *Int64Token) Value() interface{}

Value returns the value associated with this token.

type ParseError

type ParseError struct {
	Msg string // The error message
	T   Token  // The current token
}

ParseError represents an error whilst parsing input.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns the error message.

func (*ParseError) FinishPosition

func (e *ParseError) FinishPosition() int

FinishPosition returns the finish position (inclusive) of the error in the input.

func (*ParseError) StartPosition

func (e *ParseError) StartPosition() int

StartPosition returns the start position (inclusive) of the error in the input.

type Query

type Query struct {
	Cond     condition.Condition // The condition
	Order    sort.OrderBy        // The sort order
	HasLimit bool                // Was a limit specified?
	Limit    int64               // The limit
}

Query describes a query.

func ParseTokens

func ParseTokens(t *Tokeniser) (*Query, error)

ParseTokens parses a stream of tokens into a query.

func SQL

func SQL(s string) (*Query, error)

SQL parses an SQL-formatted string into a query.

type ScanError

type ScanError struct {
	Msg string // The error message
	Pos int    // The position in the input
}

ScanError represents an error whilst parsing input with a Scanner.

func (*ScanError) Error

func (e *ScanError) Error() string

Error returns the error message.

func (*ScanError) Position

func (e *ScanError) Position() int

Position returns the position of the error in the input.

type Scanner

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

Scanner allows for rune-by-rune reading of input.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new scanner wrapping the reader r.

func (*Scanner) Pop

func (s *Scanner) Pop() (rune, error)

Pop pops the next rune off the input.

func (*Scanner) Position

func (s *Scanner) Position() int

Position returns the current position of the scanner in the input.

func (*Scanner) Push

func (s *Scanner) Push(r rune)

Push pushes the given rune back onto the input.

type StringToken

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

StringToken represents a string.

func (*StringToken) FinishPosition

func (t *StringToken) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*StringToken) IsValue

func (*StringToken) IsValue() bool

IsValue returns true if the token could represent a value.

func (*StringToken) StartPosition

func (t *StringToken) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*StringToken) String

func (t *StringToken) String() string

String returns the token as a string.

func (*StringToken) Type

func (*StringToken) Type() Type

Type returns the token type.

func (*StringToken) Value

func (t *StringToken) Value() interface{}

Value returns the value associated with this token.

type Token

type Token interface {
	// Type returns the token type.
	Type() Type
	// StartPosition returns the initial position (inclusive) of this token in
	// the stream.
	StartPosition() int
	// FinishPosition returns the final position (inclusive) of this token in
	// the stream.
	FinishPosition() int
	// IsValue returns true if the token could represent a value.
	IsValue() bool
	// Value returns the value associated with this token (if any).
	Value() interface{}
}

Token is the interface describing a token.

type Tokeniser

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

Tokeniser tokenises a scanner.

func NewTokeniser

func NewTokeniser(s *Scanner) *Tokeniser

NewTokeniser returns a new tokeniser for the given scanner.

func (*Tokeniser) Pop

func (t *Tokeniser) Pop() (Token, error)

Pop pops the next token off the input.

func (*Tokeniser) Push

func (t *Tokeniser) Push(tok Token)

Push pushes the given token back onto the input.

type Type

type Type int

Type is the type of a token.

func (Type) String

func (t Type) String() string

String returns a string description of the type.

type Uint64Token

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

Uint64Token represents a uint64.

func (*Uint64Token) FinishPosition

func (t *Uint64Token) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*Uint64Token) IsValue

func (*Uint64Token) IsValue() bool

IsValue returns true if the token could represent a value.

func (*Uint64Token) StartPosition

func (t *Uint64Token) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*Uint64Token) String

func (t *Uint64Token) String() string

String returns the token as a string.

func (*Uint64Token) Type

func (*Uint64Token) Type() Type

Type returns the token type.

func (*Uint64Token) Value

func (t *Uint64Token) Value() interface{}

Value returns the value associated with this token.

type WordToken

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

WordToken represents a reserved word.

func (*WordToken) FinishPosition

func (t *WordToken) FinishPosition() int

FinishPosition returns the final position (inclusive) of this token in the stream.

func (*WordToken) IsValue

func (*WordToken) IsValue() bool

IsValue returns true if the token could represent a value.

func (*WordToken) StartPosition

func (t *WordToken) StartPosition() int

StartPosition returns the initial position (inclusive) of this token in the stream.

func (*WordToken) String

func (t *WordToken) String() string

String returns the token as a string.

func (*WordToken) Type

func (t *WordToken) Type() Type

Type returns the token type.

func (*WordToken) Value

func (t *WordToken) Value() interface{}

Value returns nil.

Jump to

Keyboard shortcuts

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