ast

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ast holds types and functionality for the SLQ AST.

Note: the SLQ language implementation is fairly rudimentary and has some incomplete functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AST

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

AST is the Abstract Syntax Tree. It is the root node of a SQL query/stmt.

func Parse

func Parse(log lg.Log, input string) (*AST, error)

Parse parses the SLQ input string and builds the AST.

func (*AST) AddChild

func (a *AST) AddChild(node Node) error

func (*AST) AddSegment

func (a *AST) AddSegment(seg *Segment)

AddSegment appends seg to the AST.

func (*AST) Children

func (a *AST) Children() []Node

func (*AST) Context

func (a *AST) Context() antlr.ParseTree

func (*AST) Parent

func (a *AST) Parent() Node

func (*AST) Segments

func (a *AST) Segments() []*Segment

func (*AST) SetChildren

func (a *AST) SetChildren(children []Node) error

func (*AST) SetContext

func (a *AST) SetContext(ctx antlr.ParseTree) error

func (*AST) SetParent

func (a *AST) SetParent(parent Node) error

func (*AST) String

func (a *AST) String() string

func (*AST) Text

func (a *AST) Text() string

type Cmpr

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

Cmpr models a comparison.

func (*Cmpr) AddChild

func (bn *Cmpr) AddChild(child Node) error

func (*Cmpr) Children

func (bn *Cmpr) Children() []Node

func (*Cmpr) Context

func (bn *Cmpr) Context() antlr.ParseTree

func (*Cmpr) Parent

func (bn *Cmpr) Parent() Node

func (*Cmpr) SetChildren

func (bn *Cmpr) SetChildren(children []Node) error

func (*Cmpr) SetContext

func (bn *Cmpr) SetContext(ctx antlr.ParseTree) error

func (*Cmpr) SetParent

func (bn *Cmpr) SetParent(parent Node) error

func (*Cmpr) String

func (c *Cmpr) String() string

func (*Cmpr) Text

func (bn *Cmpr) Text() string

type ColExpr

type ColExpr interface {
	// IsColName returns true if the expr is a column name, e.g. "uid" or "users.uid".
	IsColName() bool
	ColExpr() (string, error)
	String() string
}

ColExpr indicates a column selection expression such as a column name, or context-appropriate function, e.g. "COUNT(*)".

type ColSelector

type ColSelector struct {
	Selector
}

ColSelector models a column selector such as ".user_id".

func (*ColSelector) AddChild

func (bn *ColSelector) AddChild(child Node) error

func (*ColSelector) Children

func (bn *ColSelector) Children() []Node

func (*ColSelector) ColExpr

func (s *ColSelector) ColExpr() (string, error)

ColExpr returns the column name.

func (*ColSelector) Context

func (bn *ColSelector) Context() antlr.ParseTree

func (*ColSelector) IsColName

func (s *ColSelector) IsColName() bool

func (*ColSelector) Parent

func (bn *ColSelector) Parent() Node

func (*ColSelector) SetChildren

func (bn *ColSelector) SetChildren(children []Node) error

func (*ColSelector) SetContext

func (bn *ColSelector) SetContext(ctx antlr.ParseTree) error

func (*ColSelector) SetParent

func (bn *ColSelector) SetParent(parent Node) error

func (*ColSelector) String

func (s *ColSelector) String() string

func (*ColSelector) Text

func (bn *ColSelector) Text() string

type Datasource

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

Datasource models a source such as "@sakila_sl3".

func (*Datasource) AddChild

func (bn *Datasource) AddChild(child Node) error

func (*Datasource) Children

func (bn *Datasource) Children() []Node

func (*Datasource) Context

func (bn *Datasource) Context() antlr.ParseTree

func (*Datasource) Parent

func (bn *Datasource) Parent() Node

func (*Datasource) SetChildren

func (bn *Datasource) SetChildren(children []Node) error

func (*Datasource) SetContext

func (bn *Datasource) SetContext(ctx antlr.ParseTree) error

func (*Datasource) SetParent

func (bn *Datasource) SetParent(parent Node) error

func (*Datasource) String

func (d *Datasource) String() string

func (*Datasource) Text

func (bn *Datasource) Text() string

type Expr

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

Expr models a SLQ expression such as ".uid > 4".

func (*Expr) AddChild

func (e *Expr) AddChild(child Node) error

func (*Expr) Children

func (bn *Expr) Children() []Node

func (*Expr) Context

func (bn *Expr) Context() antlr.ParseTree

func (*Expr) Parent

func (bn *Expr) Parent() Node

func (*Expr) SetChildren

func (bn *Expr) SetChildren(children []Node) error

func (*Expr) SetContext

func (bn *Expr) SetContext(ctx antlr.ParseTree) error

func (*Expr) SetParent

func (bn *Expr) SetParent(parent Node) error

func (*Expr) String

func (e *Expr) String() string

func (*Expr) Text

func (bn *Expr) Text() string

type Func

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

Func models a function. For example, "COUNT()".

func (*Func) AddChild

func (fn *Func) AddChild(child Node) error

func (*Func) Children

func (bn *Func) Children() []Node

func (*Func) ColExpr

func (fn *Func) ColExpr() (string, error)

ColExpr implements ColExpr.

func (*Func) Context

func (bn *Func) Context() antlr.ParseTree

func (*Func) FuncName

func (fn *Func) FuncName() string

FuncName returns the function name.

func (*Func) IsColName

func (fn *Func) IsColName() bool

IsColName implements ColExpr.

func (*Func) Parent

func (bn *Func) Parent() Node

func (*Func) SetChildren

func (fn *Func) SetChildren(children []Node) error

SetChildren implements Node.

func (*Func) SetContext

func (bn *Func) SetContext(ctx antlr.ParseTree) error

func (*Func) SetParent

func (bn *Func) SetParent(parent Node) error

func (*Func) String

func (fn *Func) String() string

func (*Func) Text

func (bn *Func) Text() string

type Group

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

Group models GROUP BY.

func (*Group) AddChild

func (g *Group) AddChild(child Node) error

func (*Group) Children

func (bn *Group) Children() []Node

func (*Group) Context

func (bn *Group) Context() antlr.ParseTree

func (*Group) Parent

func (bn *Group) Parent() Node

func (*Group) SetChildren

func (bn *Group) SetChildren(children []Node) error

func (*Group) SetContext

func (bn *Group) SetContext(ctx antlr.ParseTree) error

func (*Group) SetParent

func (bn *Group) SetParent(parent Node) error

func (*Group) String

func (g *Group) String() string

func (*Group) Text

func (bn *Group) Text() string

type Inspector

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

Inspector provides functionality for AST interrogation.

func NewInspector

func NewInspector(log lg.Log, ast *AST) *Inspector

NewInspector returns an Inspector instance for ast.

func (*Inspector) CountNodes

func (in *Inspector) CountNodes(typ reflect.Type) int

CountNodes counts the number of nodes having typ.

func (*Inspector) FindColExprSegment

func (in *Inspector) FindColExprSegment() (*Segment, error)

FindColExprSegment returns the segment containing col expressions (such as ".uid, .email"). This is typically the last segment. It's also possible that there is no such segment (which usually results in a SELECT * FROM).

func (*Inspector) FindFinalSelectableSegment

func (in *Inspector) FindFinalSelectableSegment() (*Segment, error)

FindFinalSelectableSegment returns the final segment that has at lest one child that implements Selectable.

func (*Inspector) FindNodes

func (in *Inspector) FindNodes(typ reflect.Type) []Node

FindNodes returns all of the nodes having typ.

func (*Inspector) FindSelectableSegments

func (in *Inspector) FindSelectableSegments() []*Segment

FindSelectableSegments returns the segments that have at least one child that implements Selectable.

func (*Inspector) FindWhereClauses

func (in *Inspector) FindWhereClauses() ([]*Where, error)

FindWhereClauses returns all the WHERE clauses in the AST.

type Join

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

Join models a SQL JOIN node. It has a child of type JoinConstraint.

func (*Join) AddChild

func (jn *Join) AddChild(node Node) error

func (*Join) Children

func (jn *Join) Children() []Node

func (*Join) Context

func (jn *Join) Context() antlr.ParseTree

func (*Join) LeftTbl

func (jn *Join) LeftTbl() *TblSelector

LeftTbl is the selector for the left table of the join.

func (*Join) Parent

func (jn *Join) Parent() Node

func (*Join) RightTbl

func (jn *Join) RightTbl() *TblSelector

RightTbl is the selector for the right table of the join.

func (*Join) Segment

func (jn *Join) Segment() *Segment

func (*Join) Selectable

func (jn *Join) Selectable()

Selectable implements the Selectable marker interface.

func (*Join) SetChildren

func (jn *Join) SetChildren(children []Node) error

func (*Join) SetContext

func (jn *Join) SetContext(ctx antlr.ParseTree) error

func (*Join) SetParent

func (jn *Join) SetParent(parent Node) error

func (*Join) String

func (jn *Join) String() string

func (*Join) Text

func (jn *Join) Text() string

type JoinConstraint

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

JoinConstraint models a join's constraint. For example the elements inside the parentheses in "join(.uid == .user_id)".

func (*JoinConstraint) AddChild

func (jc *JoinConstraint) AddChild(child Node) error

func (*JoinConstraint) Children

func (jc *JoinConstraint) Children() []Node

func (*JoinConstraint) Context

func (jc *JoinConstraint) Context() antlr.ParseTree

func (*JoinConstraint) Parent

func (jc *JoinConstraint) Parent() Node

func (*JoinConstraint) SetChildren

func (jc *JoinConstraint) SetChildren(children []Node) error

func (*JoinConstraint) SetContext

func (jc *JoinConstraint) SetContext(ctx antlr.ParseTree) error

func (*JoinConstraint) SetParent

func (jc *JoinConstraint) SetParent(parent Node) error

func (*JoinConstraint) String

func (jc *JoinConstraint) String() string

func (*JoinConstraint) Text

func (jc *JoinConstraint) Text() string

type Literal

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

Literal is a leaf node representing a literal such as a number or a string.

func (*Literal) AddChild

func (bn *Literal) AddChild(child Node) error

func (*Literal) Children

func (bn *Literal) Children() []Node

func (*Literal) Context

func (bn *Literal) Context() antlr.ParseTree

func (*Literal) Parent

func (bn *Literal) Parent() Node

func (*Literal) SetChildren

func (bn *Literal) SetChildren(children []Node) error

func (*Literal) SetContext

func (bn *Literal) SetContext(ctx antlr.ParseTree) error

func (*Literal) SetParent

func (bn *Literal) SetParent(parent Node) error

func (*Literal) String

func (li *Literal) String() string

func (*Literal) Text

func (bn *Literal) Text() string

type Node

type Node interface {
	// Parent returns the node's parent, which may be nil..
	Parent() Node

	// SetParent sets the node's parent, returning an error if illegal.
	SetParent(n Node) error

	// Children returns the node's children (may be empty).
	Children() []Node

	// SetChildren sets the node's children, returning an error if illegal.
	SetChildren(children []Node) error

	// AddChild adds a child node, returning an error if illegal.
	AddChild(child Node) error

	// Context returns the parse tree context.
	Context() antlr.ParseTree

	// SetContext sets the parse tree context, returning an error if illegal.
	SetContext(ctx antlr.ParseTree) error

	// String returns a debug-friendly string representation.
	String() string

	// Text returns the node's text representation.
	Text() string
}

Node is an AST node.

type Operator

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

Operator is a leaf node in an expression representing an operator such as ">" or "==".

func (*Operator) AddChild

func (bn *Operator) AddChild(child Node) error

func (*Operator) Children

func (bn *Operator) Children() []Node

func (*Operator) Context

func (bn *Operator) Context() antlr.ParseTree

func (*Operator) Parent

func (bn *Operator) Parent() Node

func (*Operator) SetChildren

func (bn *Operator) SetChildren(children []Node) error

func (*Operator) SetContext

func (bn *Operator) SetContext(ctx antlr.ParseTree) error

func (*Operator) SetParent

func (bn *Operator) SetParent(parent Node) error

func (*Operator) String

func (o *Operator) String() string

func (*Operator) Text

func (bn *Operator) Text() string

type RowRange

type RowRange struct {
	Offset int
	Limit  int
	// contains filtered or unexported fields
}

RowRange models a range, effectively {OFFSET,LIMIT}.

func (*RowRange) AddChild

func (bn *RowRange) AddChild(child Node) error

func (*RowRange) Children

func (bn *RowRange) Children() []Node

func (*RowRange) Context

func (bn *RowRange) Context() antlr.ParseTree

func (*RowRange) Parent

func (bn *RowRange) Parent() Node

func (*RowRange) Range

func (rr *RowRange) Range() (offset, limit int)

func (*RowRange) SetChildren

func (bn *RowRange) SetChildren(children []Node) error

func (*RowRange) SetContext

func (bn *RowRange) SetContext(ctx antlr.ParseTree) error

func (*RowRange) SetParent

func (rr *RowRange) SetParent(parent Node) error

func (*RowRange) String

func (rr *RowRange) String() string

func (*RowRange) Text

func (bn *RowRange) Text() string

type Segment

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

Segment models a segment of a query (the elements separated by pipes). For example, ".user | .uid, .username" is two segments (".user" and ".uid, .username").

func (*Segment) AddChild

func (s *Segment) AddChild(child Node) error

AddChild implements ast.Node.

func (*Segment) ChildType

func (s *Segment) ChildType() (reflect.Type, error)

ChildType returns the expected Type of the segment's elements, based on the content of the segment's node's children. The type should be something like Selector|Func.

func (*Segment) Children

func (s *Segment) Children() []Node

Children implements ast.Node.

func (*Segment) Context

func (s *Segment) Context() antlr.ParseTree

Context implements ast.Node.

func (*Segment) Next

func (s *Segment) Next() *Segment

Next returns the next segment, or nil if this is the last segment.

func (*Segment) Parent

func (s *Segment) Parent() Node

Parent implements ast.Node.

func (*Segment) Prev

func (s *Segment) Prev() *Segment

Prev returns the previous segment, or nil if this is the first segment.

func (*Segment) SegIndex

func (s *Segment) SegIndex() int

SegIndex returns the index of this segment.

func (*Segment) SetChildren

func (s *Segment) SetChildren(children []Node) error

SetChildren implements ast.Node.

func (*Segment) SetContext

func (s *Segment) SetContext(ctx antlr.ParseTree) error

SetContext implements ast.Node.

func (*Segment) SetParent

func (s *Segment) SetParent(parent Node) error

SetParent implements ast.Node.

func (*Segment) String

func (s *Segment) String() string

func (*Segment) Text

func (s *Segment) Text() string

type Selectable

type Selectable interface {
	Selectable()
}

Selectable is a marker interface to indicate that the node can be selected from. That is, the node represents a SQL table, view, or join table, and can be used like "SELECT * FROM [selectable]".

type Selector

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

Selector is a selector such as ".my_table" or ".my_col". The generic selector will typically be replaced with a more specific selector node such as TblSelector or ColSelector.

func (*Selector) AddChild

func (bn *Selector) AddChild(child Node) error

func (*Selector) Children

func (bn *Selector) Children() []Node

func (*Selector) Context

func (bn *Selector) Context() antlr.ParseTree

func (*Selector) Parent

func (bn *Selector) Parent() Node

func (*Selector) SelValue

func (s *Selector) SelValue() string

func (*Selector) SetChildren

func (bn *Selector) SetChildren(children []Node) error

func (*Selector) SetContext

func (bn *Selector) SetContext(ctx antlr.ParseTree) error

func (*Selector) SetParent

func (bn *Selector) SetParent(parent Node) error

func (*Selector) String

func (s *Selector) String() string

func (*Selector) Text

func (bn *Selector) Text() string

type TblSelector

type TblSelector struct {
	Selector
	DSName  string
	TblName string
}

TblSelector is a selector for a table, such as ".my_table" or "@my_src.my_table".

func (*TblSelector) AddChild

func (bn *TblSelector) AddChild(child Node) error

func (*TblSelector) Children

func (bn *TblSelector) Children() []Node

func (*TblSelector) Context

func (bn *TblSelector) Context() antlr.ParseTree

func (*TblSelector) Parent

func (bn *TblSelector) Parent() Node

func (*TblSelector) SelValue

func (s *TblSelector) SelValue() string

func (*TblSelector) Selectable

func (s *TblSelector) Selectable()

Selectable implements the Selectable marker interface.

func (*TblSelector) SetChildren

func (bn *TblSelector) SetChildren(children []Node) error

func (*TblSelector) SetContext

func (bn *TblSelector) SetContext(ctx antlr.ParseTree) error

func (*TblSelector) SetParent

func (bn *TblSelector) SetParent(parent Node) error

func (*TblSelector) String

func (s *TblSelector) String() string

func (*TblSelector) Text

func (bn *TblSelector) Text() string

type Terminal

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

Terminal is a terminal/leaf node that typically is interpreted simply as its text value.

func (*Terminal) AddChild

func (bn *Terminal) AddChild(child Node) error

func (*Terminal) Children

func (bn *Terminal) Children() []Node

func (*Terminal) Context

func (bn *Terminal) Context() antlr.ParseTree

func (*Terminal) Parent

func (bn *Terminal) Parent() Node

func (*Terminal) SetChildren

func (bn *Terminal) SetChildren(children []Node) error

func (*Terminal) SetContext

func (bn *Terminal) SetContext(ctx antlr.ParseTree) error

func (*Terminal) SetParent

func (bn *Terminal) SetParent(parent Node) error

func (*Terminal) String

func (t *Terminal) String() string

func (*Terminal) Text

func (bn *Terminal) Text() string

type Walker

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

Walker traverses a node tree (the AST, or a subset thereof).

func NewWalker

func NewWalker(log lg.Log, node Node) *Walker

NewWalker returns a new Walker instance.

func (*Walker) AddVisitor

func (w *Walker) AddVisitor(typ reflect.Type, visitor nodeVisitorFn) *Walker

AddVisitor adds a visitor function for the specified node type (and returns the receiver Walker, to enabled chaining).

func (*Walker) Walk

func (w *Walker) Walk() error

Walk starts the walking process.

type Where

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

Where represents a SQL WHERE clause, i.e. a filter on the SELECT.

func (*Where) AddChild

func (w *Where) AddChild(node Node) error

func (*Where) Children

func (bn *Where) Children() []Node

func (*Where) Context

func (bn *Where) Context() antlr.ParseTree

func (*Where) Expr

func (w *Where) Expr() *Expr

Expr returns the expression that constitutes the SetWhere clause, or nil if no expression.

func (*Where) Parent

func (bn *Where) Parent() Node

func (*Where) SetChildren

func (bn *Where) SetChildren(children []Node) error

func (*Where) SetContext

func (bn *Where) SetContext(ctx antlr.ParseTree) error

func (*Where) SetParent

func (bn *Where) SetParent(parent Node) error

func (*Where) String

func (w *Where) String() string

func (*Where) Text

func (bn *Where) Text() string

Directories

Path Synopsis
internal
slq
Package sqlbuilder contains functionality for building SQL from the AST.
Package sqlbuilder contains functionality for building SQL from the AST.

Jump to

Keyboard shortcuts

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