sqlparser

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolExpr

func BoolExpr(l *Lexer, be *BooleanExpression) bool

BoolExpr -> CompExpr { BoolOp CompExpr } BoolOp -> <AND> | <OR>

func ColumnOrGroup

func ColumnOrGroup(l *Lexer, stmt *Statement) bool

ColumnOrGroup -> <ID> OptGroup OptGroup -> <(> <ID> <)> | eps

func CompExpr

func CompExpr(l *Lexer, be *BooleanExpression) bool

CompExpr -> <(> BoolExpr <)> | InCompExpr | SimpleCompExpr

func FromStmt

func FromStmt(l *Lexer, stmt *Statement) bool

FromStmt -> <FROM> <ID>

func GetValue

func GetValue(s string) any

GetValue obtains an sql value from a string as an int, nil or string.

func InCompExpr

func InCompExpr(l *Lexer, be *BooleanExpression, id string) bool

InCompExpr -> <ID> (<NOT> | eps) <IN> <(> ValueList <)> ValueList -> <ID> { <,> <ID> }

func OptJoinStmt

func OptJoinStmt(l *Lexer, stmt *Statement) bool

OptJoinStmt -> <JOIN> <ID> <ON> <ID> <=> <ID> | eps

func OptWhereStmt

func OptWhereStmt(l *Lexer, stmt *Statement) bool

OptWhereStmt -> <WHERE> BoolExpr | eps

func SelectStmt

func SelectStmt(l *Lexer, stmt *Statement) bool

SelectStmt -> <SELECT> Columns Columns -> ColumnOrGroup { <,> ColumnOrGroup } | <*>

func SimpleCompExpr

func SimpleCompExpr(l *Lexer, be *BooleanExpression, id string) bool

SimpleCompExpr -> <ID> CompOp <ID> | <ID> <IS> (<NOT> | eps) <NULL>

Types

type BooleanComposite

type BooleanComposite struct {
	BoolOp  string
	SubExpr []BooleanExpression
}

struct BooleanComposite represents a boolean expression with many sub boolean expressions and an operator joining them (such as AND or OR).

func (*BooleanComposite) GetBson

func (bc *BooleanComposite) GetBson(
	tableFrom, tableJoin string,
) (bson.D, error)

GetBson implements the BooleanExpression interface.

type BooleanExpression

type BooleanExpression interface {
	GetBson(tableFrom, tableJoin string) (bson.D, error)
}

A BooleanExpression represents a parsed boolean comparision that can be converted to a mongoDB bson document given the tables related in the query (for _id management and joined table reference).

func ParseBoolExpr

func ParseBoolExpr(sql string) (BooleanExpression, error)

ParseBoolExpr parses a boolean expression only, returning the describing BooleanExpression.

ParseBoolExpr -> BoolExpr

type Column

type Column struct {
	Name          string
	GroupFunction string
}

struct Column represents a parsed SQL column (select entry), which is either an identifier or an identifier with a group function associated. Note that naming columns is not supported.

type Comparision

type Comparision struct {
	Id    string
	Value any
	Op    string
}

struct Comparision represents a simple comparision such as "A > 10", having an identifier on the left, a value on the right, and a boolean operator.

func (*Comparision) GetBson

func (c *Comparision) GetBson(tableFrom, tableJoin string) (bson.D, error)

GetBson implements the BooleanExpression interface.

type EmptyComparision

type EmptyComparision struct{}

struct EmptyComparision represents a comparision that is always true

func (EmptyComparision) GetBson

func (e EmptyComparision) GetBson(_, _ string) (bson.D, error)

GetBson implements the BooleanExpression interface.

type InComparision

type InComparision struct {
	Id     string
	Not    bool
	Values []any
}

struct InComparision represents a comparision using IN or NOT IN, such as "A IN (1, 2, 3, 4)".

func (*InComparision) GetBson

func (ic *InComparision) GetBson(tableFrom, tableJoin string) (bson.D, error)

GetBson implements the BooleanExpression interface.

type Lexer

type Lexer struct {
	Value string // the current value read as a string
	Token rune   // the current token mostly as returned from the scanner
	// contains filtered or unexported fields
}

struct Lexer implements a simple lexer for the sqlparser using a text scanner.

func NewLexer

func NewLexer(rd io.Reader) *Lexer

NewLexer creares a new Lexer from a reader.

func (*Lexer) Lex

func (l *Lexer) Lex() bool

Lex advances the scanner forward by one token, updating Token and Value data, and returning if the lexer reached EOF.

type Statement

type Statement struct {
	SelectColumn []Column

	FromTable string

	JoinTable    string
	JoinFromAttr string
	JoinToAttr   string

	Where BooleanExpression
}

struct Statement represents a parsed SQL statement, with selection columns, a single origin table, a single (optional) joined table with a single join condition, and a filtering expression.

func Parse

func Parse(sql string) (*Statement, error)

Parse parses an SQL string and returns the statement that describes it.

Parse -> SelectStmt FromStmt OptJoinStmt OptWhereStmt

func (*Statement) GetGroup

func (stmt *Statement) GetGroup() (bson.D, error)

GetGroup gets the document paired with the $group operator for a mongoDB aggregation for a Statement.

func (*Statement) GetLookup

func (stmt *Statement) GetLookup() (bson.D, error)

GetLookup gets the document paired with the $lookup operator for a mongoDB aggregation for a Statement.

func (*Statement) GetSelect

func (stmt *Statement) GetSelect() (bson.D, error)

GetSelect gets the bson representing the find key selection document, or the $project value in an aggregation pipeline.

func (*Statement) IsAggregate

func (stmt *Statement) IsAggregate() bool

IsAggregate returns if the Statement is an aggregation or a find. A Statement is an aggregation only if it has either a join or a group function in it.

func (*Statement) ToMongoAggregate

func (stmt *Statement) ToMongoAggregate() (mongo.Pipeline, error)

ToMongoAggregate gets the Pipeline representing an aggregation for a Statement.

func (*Statement) ToMongoFind

func (stmt *Statement) ToMongoFind() (bson.D, bson.D, error)

ToMongoFind gets the bsons representing a find for a statement. The first document is the filter and the second is the key selection.

Jump to

Keyboard shortcuts

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