ast

package
v0.48.4 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package ast holds types and functionality for the SLQ AST.

The entrypoint is ast.Parse, which accepts a SLQ query, and returns an *ast.AST. The ast is a tree of ast.Node instances.

Note that much of the testing of package ast is performed in package libsq.

Index

Constants

View Source
const (
	FuncNameAvg         = "avg"
	FuncNameCount       = "count"
	FuncNameCountUnique = "count_unique"
	FuncNameMax         = "max"
	FuncNameMin         = "min"
	FuncNameSchema      = "schema"
	FuncNameCatalog     = "catalog"
	FuncNameSum         = "sum"
	FuncNameRowNum      = "rownum"
)

Variables

This section is empty.

Functions

func ExtractHandles added in v0.47.0

func ExtractHandles(ast *AST) []string

ExtractHandles returns a sorted slice of all handles mentioned in the AST. Duplicate mentions are removed.

func FindFirstNode added in v0.44.0

func FindFirstNode[T Node](ast *AST) T

FindFirstNode returns the first node of type T in ast, or nil if no such node exists.

func FindNodes added in v0.44.0

func FindNodes[T Node](ast *AST) []T

FindNodes returns the nodes of type T in ast.

func NodePrevSegmentChild added in v0.46.0

func NodePrevSegmentChild[T Node](node Node) (T, error)

NodePrevSegmentChild returns the first child of the previous segment, where the child must be of type T, or an error if the child is not of type T.

func NodeUnwrap added in v0.38.0

func NodeUnwrap[T Node](node Node) (T, bool)

NodeUnwrap "unwraps" node returning the lowest single contained node. False is returned if node (or any of its descendants), has more than one child. If node has no children, it is returned directly. This function is useful for "unwrapping" a node that is contained in an outer node. For example, an ExprNode may often contain just a single LiteralNode.

func ParseCatalogSchema added in v0.43.0

func ParseCatalogSchema(s string) (catalog, schema string, err error)

ParseCatalogSchema parses a string of the form 'catalog.schema' and returns the catalog and schema. It is permissible for one of the components to be empty (but not both). Whitespace and quotes are handled correctly.

Examples:

`catalog.schema` 						-> "catalog", "schema", nil
`catalog.` 									-> "catalog", "", nil
`schema`										-> "", "schema", nil
`"my catalog"."my schema"` 	-> "my catalog", "my schema", nil

An error is returned if s 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 *slog.Logger, input string) (*AST, error)

Parse parses the SLQ input string and builds the AST.

func (*AST) AddChild

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

AddChild implements ast.Node.

func (*AST) AddSegment

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

AddSegment appends seg to the AST.

func (*AST) Children

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

Children implements ast.Node.

func (*AST) Parent

func (a *AST) Parent() Node

Parent implements ast.Node.

func (*AST) Segments

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

Segments returns the AST's segments (its direct children).

func (*AST) SetChildren

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

SetChildren implements ast.Node.

func (*AST) SetParent

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

SetParent implements ast.Node.

func (*AST) String

func (a *AST) String() string

String implements ast.Node.

func (*AST) Text

func (a *AST) Text() string

Text implements ast.Node.

type ArgNode added in v0.31.0

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

ArgNode implements the SQL "DISTINCT" clause.

func (*ArgNode) AddChild added in v0.31.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*ArgNode) Children added in v0.31.0

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

Children implements ast.Node.

func (*ArgNode) Key added in v0.31.0

func (n *ArgNode) Key() string

Key returns the arg key. If the arg is "$name", the key is "name".

func (*ArgNode) Parent added in v0.31.0

func (bn *ArgNode) Parent() Node

Parent implements ast.Node.

func (*ArgNode) SetChildren added in v0.31.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*ArgNode) SetParent added in v0.31.0

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

SetParent implements ast.Node.

func (*ArgNode) String added in v0.31.0

func (n *ArgNode) String() string

String returns a log/debug-friendly representation.

func (*ArgNode) Text added in v0.31.0

func (bn *ArgNode) Text() string

Text implements ast.Node.

type ColSelectorNode added in v0.26.0

type ColSelectorNode struct {
	*SelectorNode
	// contains filtered or unexported fields
}

ColSelectorNode models a column selector such as ".first_name".

func (ColSelectorNode) AddChild added in v0.26.0

func (bn ColSelectorNode) AddChild(child Node) error

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*ColSelectorNode) Alias added in v0.26.0

func (n *ColSelectorNode) Alias() string

Alias returns the column alias, which may be empty. For example, given the selector ".first_name:given_name", the alias is "given_name".

func (ColSelectorNode) Children added in v0.26.0

func (bn ColSelectorNode) Children() []Node

Children implements ast.Node.

func (*ColSelectorNode) ColName added in v0.26.0

func (n *ColSelectorNode) ColName() string

ColName returns the column name. Note the name is not escaped/quoted, thus it could contain whitespace, etc.

func (*ColSelectorNode) IsColumn added in v0.26.0

func (n *ColSelectorNode) IsColumn() bool

IsColumn always returns true.

func (ColSelectorNode) Parent added in v0.26.0

func (bn ColSelectorNode) Parent() Node

Parent implements ast.Node.

func (ColSelectorNode) SetChildren added in v0.26.0

func (bn ColSelectorNode) SetChildren(children []Node) error

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (ColSelectorNode) SetParent added in v0.26.0

func (bn ColSelectorNode) SetParent(parent Node) error

SetParent implements ast.Node.

func (*ColSelectorNode) String added in v0.26.0

func (n *ColSelectorNode) String() string

String returns a log/debug-friendly representation.

func (*ColSelectorNode) Text added in v0.26.0

func (n *ColSelectorNode) Text() string

Text implements ResultColumn.

type ExprElementNode added in v0.38.0

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

ExprElementNode is an expression that acts as a ResultColumn.

.actor | (1+2):alias

In the example above, the expression "(1+2)" is rendered with an alias, e.g.

SELECT 1+2 AS "alias" FROM "actor"

func (*ExprElementNode) AddChild added in v0.38.0

func (ex *ExprElementNode) AddChild(child Node) error

AddChild implements Node.

func (*ExprElementNode) Alias added in v0.38.0

func (ex *ExprElementNode) Alias() string

Alias implements ResultColumn.

func (*ExprElementNode) Children added in v0.38.0

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

Children implements ast.Node.

func (*ExprElementNode) ExprNode added in v0.38.0

func (ex *ExprElementNode) ExprNode() *ExprNode

ExprNode returns the child expression.

func (*ExprElementNode) Parent added in v0.38.0

func (bn *ExprElementNode) Parent() Node

Parent implements ast.Node.

func (*ExprElementNode) SetChildren added in v0.38.0

func (ex *ExprElementNode) SetChildren(children []Node) error

SetChildren implements Node.

func (*ExprElementNode) SetParent added in v0.38.0

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

SetParent implements ast.Node.

func (*ExprElementNode) String added in v0.38.0

func (ex *ExprElementNode) String() string

String returns a log/debug-friendly representation.

func (*ExprElementNode) Text added in v0.38.0

func (ex *ExprElementNode) Text() string

Text implements ResultColumn.

type ExprNode added in v0.27.0

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

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

func (*ExprNode) AddChild added in v0.27.0

func (n *ExprNode) AddChild(child Node) error

AddChild implements Node.

func (*ExprNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*ExprNode) HasParens added in v0.38.0

func (n *ExprNode) HasParens() bool

HasParens returns true if the expression is enclosed in parentheses.

func (*ExprNode) Parent added in v0.27.0

func (bn *ExprNode) Parent() Node

Parent implements ast.Node.

func (*ExprNode) SetChildren added in v0.27.0

func (n *ExprNode) SetChildren(children []Node) error

SetChildren implements Node.

func (*ExprNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*ExprNode) String added in v0.27.0

func (n *ExprNode) String() string

String returns a log/debug-friendly representation.

func (*ExprNode) Text added in v0.27.0

func (bn *ExprNode) Text() string

Text implements ast.Node.

type FuncNode added in v0.27.0

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

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

func (*FuncNode) AddChild added in v0.27.0

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

AddChild implements Node.

func (*FuncNode) Alias added in v0.27.0

func (fn *FuncNode) Alias() string

Alias implements ResultColumn.

func (*FuncNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*FuncNode) FuncName added in v0.27.0

func (fn *FuncNode) FuncName() string

FuncName returns the function name.

func (*FuncNode) IsProprietary added in v0.36.2

func (fn *FuncNode) IsProprietary() bool

IsProprietary returns true if this is a DB-proprietary function, as opposed to a portable function. For example, SQLite has a "strftime" function. In the SLQ, this is referenced as "_strftime": SLQ uses the underscore to indicate a proprietary function.

func (*FuncNode) Parent added in v0.27.0

func (bn *FuncNode) Parent() Node

Parent implements ast.Node.

func (*FuncNode) SetChildren added in v0.27.0

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

SetChildren implements Node.

func (*FuncNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*FuncNode) String added in v0.27.0

func (fn *FuncNode) String() string

String returns a log/debug-friendly representation.

func (*FuncNode) Text added in v0.27.0

func (fn *FuncNode) Text() string

Text implements ResultColumn.

type GroupByNode added in v0.27.0

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

GroupByNode models GROUP BY. The children of GroupBy node can be of type selector or FuncNode.

func (*GroupByNode) AddChild added in v0.27.0

func (n *GroupByNode) AddChild(child Node) error

AddChild implements Node.

func (*GroupByNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*GroupByNode) Parent added in v0.27.0

func (bn *GroupByNode) Parent() Node

Parent implements ast.Node.

func (*GroupByNode) SetChildren added in v0.27.0

func (n *GroupByNode) SetChildren(children []Node) error

SetChildren implements ast.Node.

func (*GroupByNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*GroupByNode) String added in v0.27.0

func (n *GroupByNode) String() string

String returns a log/debug-friendly representation.

func (*GroupByNode) Text added in v0.27.0

func (bn *GroupByNode) Text() string

Text implements ast.Node.

type HandleNode added in v0.26.0

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

HandleNode models a source handle such as "@sakila".

func (*HandleNode) AddChild added in v0.26.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*HandleNode) Children added in v0.26.0

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

Children implements ast.Node.

func (*HandleNode) Handle added in v0.38.0

func (d *HandleNode) Handle() string

Handle returns the handle value, e.g. "@sakila".

func (*HandleNode) Parent added in v0.26.0

func (bn *HandleNode) Parent() Node

Parent implements ast.Node.

func (*HandleNode) SetChildren added in v0.26.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*HandleNode) SetParent added in v0.26.0

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

SetParent implements ast.Node.

func (*HandleNode) String added in v0.26.0

func (d *HandleNode) String() string

String returns a log/debug-friendly representation.

func (*HandleNode) Text added in v0.26.0

func (bn *HandleNode) Text() string

Text implements ast.Node.

type HavingNode added in v0.46.0

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

HavingNode models the HAVING clause. It must always be preceded by a GROUP BY clause.

func (*HavingNode) AddChild added in v0.46.0

func (n *HavingNode) AddChild(child Node) error

AddChild implements Node.

func (*HavingNode) Children added in v0.46.0

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

Children implements ast.Node.

func (*HavingNode) Parent added in v0.46.0

func (bn *HavingNode) Parent() Node

Parent implements ast.Node.

func (*HavingNode) SetChildren added in v0.46.0

func (n *HavingNode) SetChildren(children []Node) error

SetChildren implements ast.Node.

func (*HavingNode) SetParent added in v0.46.0

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

SetParent implements ast.Node.

func (*HavingNode) String added in v0.46.0

func (n *HavingNode) String() string

String returns a log/debug-friendly representation.

func (*HavingNode) Text added in v0.46.0

func (bn *HavingNode) Text() string

Text implements ast.Node.

type Inspector

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

Inspector provides functionality for AST interrogation.

TODO: Inspector can probably be rewritten to be a set of functions, especially now with generics available.

func NewInspector

func NewInspector(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() (*SegmentNode, 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) FindFinalTableSegment added in v0.40.0

func (in *Inspector) FindFinalTableSegment() (*SegmentNode, error)

FindFinalTableSegment returns the final segment that has at least one child that is an ast.TblSelectorNode.

func (*Inspector) FindFirstHandle added in v0.38.0

func (in *Inspector) FindFirstHandle() (handle string)

FindFirstHandle returns the first handle mentioned in the query, or returns empty string.

func (*Inspector) FindFirstTableSelector added in v0.40.0

func (in *Inspector) FindFirstTableSelector() *TblSelectorNode

FindFirstTableSelector returns the first top-level (child of a segment) table selector node.

func (*Inspector) FindGroupByNode added in v0.28.0

func (in *Inspector) FindGroupByNode() (*GroupByNode, error)

FindGroupByNode returns the GroupByNode, or nil if not found.

func (*Inspector) FindHandles added in v0.29.0

func (in *Inspector) FindHandles() []string

FindHandles returns all handles mentioned in the AST.

func (*Inspector) FindHavingNode added in v0.46.0

func (in *Inspector) FindHavingNode() (*HavingNode, error)

FindHavingNode returns the HavingNode, or nil if not found.

func (*Inspector) FindJoins added in v0.40.0

func (in *Inspector) FindJoins() ([]*JoinNode, error)

FindJoins returns all ast.JoinNode instances.

func (*Inspector) FindNodes

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

FindNodes returns the nodes having typ.

func (*Inspector) FindOrderByNode added in v0.27.0

func (in *Inspector) FindOrderByNode() (*OrderByNode, error)

FindOrderByNode returns the OrderByNode, or nil if not found.

func (*Inspector) FindRowRangeNode added in v0.38.0

func (in *Inspector) FindRowRangeNode() (*RowRangeNode, error)

FindRowRangeNode returns the single RowRangeNode, or nil. An error can be returned if the AST is in an illegal state.

func (*Inspector) FindTableSegments added in v0.40.0

func (in *Inspector) FindTableSegments() []*SegmentNode

FindTableSegments returns the segments that have at least one child that is a ast.TblSelectorNode.

func (*Inspector) FindUniqueNode added in v0.30.0

func (in *Inspector) FindUniqueNode() (*UniqueNode, error)

FindUniqueNode returns any UniqueNode, or nil.

func (*Inspector) FindWhereClauses

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

FindWhereClauses returns all the WHERE clauses in the AST.

type JoinNode added in v0.26.0

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

JoinNode models a SQL JOIN node.

func (*JoinNode) AddChild added in v0.26.0

func (n *JoinNode) AddChild(node Node) error

AddChild implements ast.Node.

func (*JoinNode) Children added in v0.26.0

func (n *JoinNode) Children() []Node

Children implements ast.Node.

func (*JoinNode) JoinType added in v0.40.0

func (n *JoinNode) JoinType() jointype.Type

JoinType returns the join type.

func (*JoinNode) Parent added in v0.26.0

func (n *JoinNode) Parent() Node

Parent implements ast.Node.

func (*JoinNode) Predicate added in v0.40.0

func (n *JoinNode) Predicate() *ExprNode

Predicate returns the join predicate, which may be nil.

func (*JoinNode) Segment added in v0.26.0

func (n *JoinNode) Segment() *SegmentNode

func (*JoinNode) SetChildren added in v0.26.0

func (n *JoinNode) SetChildren(children []Node) error

SetChildren implements ast.Node.

func (*JoinNode) SetParent added in v0.26.0

func (n *JoinNode) SetParent(parent Node) error

SetParent implements ast.Node.

func (*JoinNode) String added in v0.26.0

func (n *JoinNode) String() string

String implements ast.Node.

func (*JoinNode) Table added in v0.40.0

func (n *JoinNode) Table() *TblSelectorNode

Table is the selector for join's target table.

func (*JoinNode) Text added in v0.26.0

func (n *JoinNode) Text() string

Text implements ast.Node.

type LiteralNode added in v0.27.0

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

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

func (*LiteralNode) AddChild added in v0.27.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*LiteralNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*LiteralNode) LiteralType added in v0.31.0

func (n *LiteralNode) LiteralType() LiteralType

LiteralType returns the literal type (number, string, etc).

func (*LiteralNode) Parent added in v0.27.0

func (bn *LiteralNode) Parent() Node

Parent implements ast.Node.

func (*LiteralNode) SetChildren added in v0.27.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*LiteralNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*LiteralNode) String added in v0.27.0

func (n *LiteralNode) String() string

String returns a log/debug-friendly representation.

func (*LiteralNode) Text added in v0.27.0

func (bn *LiteralNode) Text() string

Text implements ast.Node.

type LiteralType added in v0.31.0

type LiteralType string

LiteralType is an enum of literal types.

const (
	LiteralNull          LiteralType = "null"
	LiteralNaturalNumber LiteralType = "int"
	LiteralAnyNumber     LiteralType = "float"
	LiteralString        LiteralType = "string"
	LiteralBool          LiteralType = "bool"
)

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 (which 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

	// Text returns the node's raw text value.
	Text() string

	// String returns a debug-friendly string representation.
	String() string
	// contains filtered or unexported methods
}

Node is an AST node.

func NodeNextSibling added in v0.31.0

func NodeNextSibling(node Node) Node

NodeNextSibling returns the node's next sibling, or nil.

func NodePrevSibling added in v0.31.0

func NodePrevSibling(node Node) Node

NodePrevSibling returns the node's previous sibling, or nil.

func NodeRoot added in v0.44.0

func NodeRoot(node Node) Node

NodeRoot returns the root node of the tree containing node. This returned node should be an *ast.AST.

func NodesHavingText added in v0.31.0

func NodesHavingText(tree Node, text string) []Node

NodesHavingText returns any node whose node.Text() method returns text.

type OperatorNode added in v0.27.0

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

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

func (*OperatorNode) AddChild added in v0.27.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*OperatorNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*OperatorNode) Parent added in v0.27.0

func (bn *OperatorNode) Parent() Node

Parent implements ast.Node.

func (*OperatorNode) SetChildren added in v0.27.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*OperatorNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*OperatorNode) String added in v0.27.0

func (n *OperatorNode) String() string

String returns a log/debug-friendly representation.

func (*OperatorNode) Text added in v0.27.0

func (bn *OperatorNode) Text() string

Text implements ast.Node.

type OrderByDirection added in v0.27.0

type OrderByDirection string

OrderByDirection specifies the "ORDER BY" direction.

const (
	// OrderByDirectionNone is the default order direction.
	OrderByDirectionNone OrderByDirection = ""

	// OrderByDirectionAsc is the ascending  (DESC) order direction.
	OrderByDirectionAsc OrderByDirection = "ASC"

	// OrderByDirectionDesc is the descending (DESC) order direction.
	OrderByDirectionDesc OrderByDirection = "DESC"
)

type OrderByNode added in v0.27.0

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

OrderByNode implements the SQL "ORDER BY" clause.

func (*OrderByNode) AddChild added in v0.27.0

func (n *OrderByNode) AddChild(child Node) error

AddChild implements Node.AddChild. It returns an error if child is nil or not type OrderByTermNode.

func (*OrderByNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*OrderByNode) Parent added in v0.27.0

func (bn *OrderByNode) Parent() Node

Parent implements ast.Node.

func (*OrderByNode) SetChildren added in v0.27.0

func (n *OrderByNode) SetChildren(children []Node) error

SetChildren implements ast.Node.

func (*OrderByNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*OrderByNode) String added in v0.27.0

func (n *OrderByNode) String() string

String returns a log/debug-friendly representation.

func (*OrderByNode) Terms added in v0.27.0

func (n *OrderByNode) Terms() []*OrderByTermNode

Terms returns a new slice containing the ordering terms of the OrderByNode. The returned slice has at least one term.

func (*OrderByNode) Text added in v0.27.0

func (bn *OrderByNode) Text() string

Text implements ast.Node.

type OrderByTermNode added in v0.27.0

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

OrderByTermNode is a child of OrderByNode.

func (*OrderByTermNode) AddChild added in v0.27.0

func (n *OrderByTermNode) AddChild(child Node) error

AddChild accepts a single child of type *SelectorNode.

func (*OrderByTermNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*OrderByTermNode) Direction added in v0.27.0

func (n *OrderByTermNode) Direction() OrderByDirection

Direction returns the ordering term's direction.

func (*OrderByTermNode) Parent added in v0.27.0

func (bn *OrderByTermNode) Parent() Node

Parent implements ast.Node.

func (*OrderByTermNode) Selector added in v0.27.0

func (n *OrderByTermNode) Selector() Node

Selector returns the ordering term's selector.

func (*OrderByTermNode) SetChildren added in v0.27.0

func (n *OrderByTermNode) SetChildren(children []Node) error

SetChildren implements ast.Node.

func (*OrderByTermNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*OrderByTermNode) String added in v0.27.0

func (n *OrderByTermNode) String() string

String returns a log/debug-friendly representation.

func (*OrderByTermNode) Text added in v0.27.0

func (bn *OrderByTermNode) Text() string

Text implements ast.Node.

type ResultColumn added in v0.26.0

type ResultColumn interface {
	Node

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

	// Alias returns the column alias, which may be empty.
	// For example, given the selector ".first_name:given_name", the
	// alias is "given_name".
	Alias() string

	// Text returns the raw text of the node, e.g. ".actor" or "1*2".
	Text() string
	// contains filtered or unexported methods
}

ResultColumn indicates a column selection expression Node such as a column name, or context-appropriate function, e.g. "COUNT(*)". See: https://www.sqlite.org/syntax/result-column.html

type RowRangeNode added in v0.27.0

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

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

func (*RowRangeNode) AddChild added in v0.27.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*RowRangeNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*RowRangeNode) Parent added in v0.27.0

func (bn *RowRangeNode) Parent() Node

Parent implements ast.Node.

func (*RowRangeNode) Range added in v0.27.0

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

Range returns the offset and limit.

func (*RowRangeNode) SetChildren added in v0.27.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*RowRangeNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*RowRangeNode) String added in v0.27.0

func (rr *RowRangeNode) String() string

String implements ast.Node.

func (*RowRangeNode) Text added in v0.27.0

func (bn *RowRangeNode) Text() string

Text implements ast.Node.

type SegmentNode added in v0.26.0

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

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

func (*SegmentNode) AddChild added in v0.26.0

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

AddChild implements ast.Node.

func (*SegmentNode) ChildType added in v0.26.0

func (s *SegmentNode) 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 SelectorNode|FuncNode.

func (*SegmentNode) Children added in v0.26.0

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

Children implements ast.Node.

func (*SegmentNode) Next added in v0.26.0

func (s *SegmentNode) Next() *SegmentNode

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

func (*SegmentNode) Parent added in v0.26.0

func (s *SegmentNode) Parent() Node

Parent implements ast.Node.

func (*SegmentNode) Prev added in v0.26.0

func (s *SegmentNode) Prev() *SegmentNode

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

func (*SegmentNode) SegIndex added in v0.26.0

func (s *SegmentNode) SegIndex() int

SegIndex returns the index of this segment.

func (*SegmentNode) SetChildren added in v0.26.0

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

SetChildren implements ast.Node.

func (*SegmentNode) SetParent added in v0.26.0

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

SetParent implements ast.Node.

func (*SegmentNode) String added in v0.26.0

func (s *SegmentNode) String() string

String returns a log/debug-friendly representation.

func (*SegmentNode) Text added in v0.26.0

func (s *SegmentNode) Text() string

Text implements ast.Node.

type Selector

type Selector interface {
	Node
	// contains filtered or unexported methods
}

Selector is a Node marker interface for selector node types. A selector node models a selector such as ".first_name" or ".actor.last_name".

type SelectorNode added in v0.26.0

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

SelectorNode 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 TblSelectorNode or ColSelectorNode.

func (*SelectorNode) AddChild added in v0.26.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*SelectorNode) Children added in v0.26.0

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

Children implements ast.Node.

func (*SelectorNode) Parent added in v0.26.0

func (bn *SelectorNode) Parent() Node

Parent implements ast.Node.

func (*SelectorNode) SelValue added in v0.26.0

func (s *SelectorNode) SelValue() (string, error)

SelValue returns the selector value. See extractSelValue.

func (*SelectorNode) SetChildren added in v0.26.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*SelectorNode) SetParent added in v0.26.0

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

SetParent implements ast.Node.

func (*SelectorNode) String added in v0.26.0

func (s *SelectorNode) String() string

Strings returns a log/debug-friendly representation.

func (*SelectorNode) Text added in v0.26.0

func (bn *SelectorNode) Text() string

Text implements ast.Node.

type TblColSelectorNode added in v0.26.0

type TblColSelectorNode struct {
	*SelectorNode
	// contains filtered or unexported fields
}

TblColSelectorNode models the TABLE.COLUMN selector, e.g. actor.first_name.

func (TblColSelectorNode) AddChild added in v0.26.0

func (bn TblColSelectorNode) AddChild(child Node) error

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*TblColSelectorNode) Alias added in v0.26.0

func (n *TblColSelectorNode) Alias() string

Alias returns the column alias, which may be empty. For example, given the selector ".first_name:given_name", the alias is "given_name".

func (TblColSelectorNode) Children added in v0.26.0

func (bn TblColSelectorNode) Children() []Node

Children implements ast.Node.

func (*TblColSelectorNode) ColName added in v0.26.0

func (n *TblColSelectorNode) ColName() string

ColName returns the column name, e.g. first_name.

func (*TblColSelectorNode) IsColumn added in v0.26.0

func (n *TblColSelectorNode) IsColumn() bool

IsColumn implements ResultColumn.

func (TblColSelectorNode) Parent added in v0.26.0

func (bn TblColSelectorNode) Parent() Node

Parent implements ast.Node.

func (TblColSelectorNode) SetChildren added in v0.26.0

func (bn TblColSelectorNode) SetChildren(children []Node) error

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (TblColSelectorNode) SetParent added in v0.26.0

func (bn TblColSelectorNode) SetParent(parent Node) error

SetParent implements ast.Node.

func (*TblColSelectorNode) String added in v0.26.0

func (n *TblColSelectorNode) String() string

String returns a log/debug-friendly representation.

func (*TblColSelectorNode) TblName added in v0.26.0

func (n *TblColSelectorNode) TblName() string

TblName returns the table name, e.g. actor.

func (*TblColSelectorNode) Text added in v0.26.0

func (n *TblColSelectorNode) Text() string

Text implements ResultColumn.

type TblSelectorNode added in v0.26.0

type TblSelectorNode struct {
	SelectorNode
	// contains filtered or unexported fields
}

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

func (*TblSelectorNode) AddChild added in v0.26.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*TblSelectorNode) Alias added in v0.40.0

func (n *TblSelectorNode) Alias() string

Alias returns the node's alias, or empty string.

func (*TblSelectorNode) Children added in v0.26.0

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

Children implements ast.Node.

func (*TblSelectorNode) Handle added in v0.26.0

func (n *TblSelectorNode) Handle() string

Handle returns the handle, which may be empty.

func (*TblSelectorNode) Parent added in v0.26.0

func (bn *TblSelectorNode) Parent() Node

Parent implements ast.Node.

func (*TblSelectorNode) SelValue added in v0.26.0

func (n *TblSelectorNode) SelValue() (string, error)

SelValue returns the table name. TODO: Can we get rid of this method SelValue?

func (*TblSelectorNode) SetChildren added in v0.26.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*TblSelectorNode) SetHandle added in v0.40.0

func (n *TblSelectorNode) SetHandle(h string)

SetHandle sets the handle.

func (*TblSelectorNode) SetParent added in v0.26.0

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

SetParent implements ast.Node.

func (*TblSelectorNode) SetTable added in v0.43.0

func (n *TblSelectorNode) SetTable(tbl tablefq.T)

SetTable sets the table value.

func (*TblSelectorNode) String added in v0.26.0

func (n *TblSelectorNode) String() string

String returns a log/debug-friendly representation.

func (*TblSelectorNode) SyncTblNameAlias added in v0.40.0

func (n *TblSelectorNode) SyncTblNameAlias()

SyncTblNameAlias sets the table name to the alias value, if the alias is non-empty, and then sets the alias to empty.

func (*TblSelectorNode) Table added in v0.43.0

func (n *TblSelectorNode) Table() tablefq.T

Table returns the table name. This is the raw value without punctuation.

func (*TblSelectorNode) TblAliasOrName added in v0.40.0

func (n *TblSelectorNode) TblAliasOrName() tablefq.T

TblAliasOrName returns the table alias if set; if not, it returns the table name.

func (*TblSelectorNode) Text added in v0.26.0

func (bn *TblSelectorNode) Text() string

Text implements ast.Node.

type UniqueNode added in v0.30.0

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

UniqueNode implements the SQL "DISTINCT" clause.

func (*UniqueNode) AddChild added in v0.30.0

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

AddChild always returns an error. Node implementations should implement a type-specific method that only accepts a child of an appropriate type for that node.

func (*UniqueNode) Children added in v0.30.0

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

Children implements ast.Node.

func (*UniqueNode) Parent added in v0.30.0

func (bn *UniqueNode) Parent() Node

Parent implements ast.Node.

func (*UniqueNode) SetChildren added in v0.30.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*UniqueNode) SetParent added in v0.30.0

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

SetParent implements ast.Node.

func (*UniqueNode) String added in v0.30.0

func (n *UniqueNode) String() string

String returns a log/debug-friendly representation.

func (*UniqueNode) Text added in v0.30.0

func (bn *UniqueNode) Text() string

Text implements ast.Node.

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(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 any node that is assignable to typ.

func (*Walker) Walk

func (w *Walker) Walk() error

Walk starts the walking process.

type WhereNode added in v0.27.0

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

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

func (*WhereNode) AddChild added in v0.27.0

func (n *WhereNode) AddChild(node Node) error

AddChild implements Node.

func (*WhereNode) Children added in v0.27.0

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

Children implements ast.Node.

func (*WhereNode) Expr added in v0.27.0

func (n *WhereNode) Expr() *ExprNode

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

func (*WhereNode) Parent added in v0.27.0

func (bn *WhereNode) Parent() Node

Parent implements ast.Node.

func (*WhereNode) SetChildren added in v0.27.0

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

SetChildren implements ast.Node. It always returns an error. Node implementations must provide their own type-specific implementation if they accept children.

func (*WhereNode) SetParent added in v0.27.0

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

SetParent implements ast.Node.

func (*WhereNode) String added in v0.27.0

func (n *WhereNode) String() string

String returns a log/debug-friendly representation.

func (*WhereNode) Text added in v0.27.0

func (bn *WhereNode) Text() string

Text implements ast.Node.

Directories

Path Synopsis
Package antlrz contains utilities for working with ANTLR4.
Package antlrz contains utilities for working with ANTLR4.
internal
slq
Package render provides the mechanism for rendering ast into SQL.
Package render provides the mechanism for rendering ast into SQL.

Jump to

Keyboard shortcuts

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