sqlparser

package module
v0.0.0-...-f49e1a6 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT, Apache-2.0 Imports: 12 Imported by: 3

README

Tableland SQL Parser

Review Test Release standard-readme compliant

Go library for parsing Tableland-compliant SQL

Table of Contents

Background

This is a Go library for parsing a Tableland SQL statement as defined by Tableland SQL Specification.

It uses goyacc to generate a parser based on a given grammar and a given lexer. With the parser, you can generate an AST from a SQL statement.

This is inspired on the xwb1989/sqlparser, with eyes on SQLite's grammar and spec.

Usage

ast, err := sqlparser.Parse("SELECT * FROM table WHERE c1 > c2")
if err != nil {
    panic(err)
}

ast.PrettyPrint()

Resulting AST:

(*sqlparser.AST)({
 Root: (*sqlparser.Select)({
  SelectColumnList: (sqlparser.SelectColumnList) (len=1 cap=1) {
   (*sqlparser.StarSelectColumn)({
    TableRef: (*sqlparser.Table)(<nil>)
   })
  },
  From: (*sqlparser.Table)({
   Name: (string) (len=5) "table"
  }),
  Where: (*sqlparser.Where)({
   Type: (string) (len=5) "where",
   Expr: (*sqlparser.CmpExpr)({
    Operator: (string) (len=1) ">",
    Left: (*sqlparser.Column)({
     Name: (string) (len=2) "c1",
     TableRef: (*sqlparser.Table)(<nil>)
    }),
    Right: (*sqlparser.Column)({
     Name: (string) (len=2) "c2",
     TableRef: (*sqlparser.Table)(<nil>)
    }),
    Escape: (sqlparser.Expr) <nil>
   })
  })
 })
})

Contributing

To get started clone this repo.

Generating the parser

go run golang.org/x/tools/cmd/goyacc@master -l -o yy_parser.go grammar.y

Generating syntax diagrams

make generate-diagrams 

Requires Java 8 (or higher).

Feedback

Reach out with feedback and ideas:

License

MIT AND Apache-2.0, © 2021-2022 Tableland Network Contributors

Documentation

Overview

Code generated by goyacc -l -o yy_parser.go grammar.y. DO NOT EDIT.

Index

Constants

View Source
const (
	CompoundUnionStr     = "union"
	CompoundUnionAllStr  = "union all"
	CompoundIntersectStr = "intersect"
	CompoundExceptStr    = "except"
)

Compound Select operation types.

View Source
const (
	DistinctStr = "distinct "
	AllStr      = "all "
)

Distinct/All.

View Source
const (
	JoinStr = "join"

	LeftJoinStr  = "left join"
	RightJoinStr = "right join"
	FullJoinStr  = "full join"
	InnerJoinStr = "inner join"
)

Kinds of JoinOperator.

View Source
const (
	WhereStr  = "where"
	HavingStr = "having"
)

Types for Where.

View Source
const (
	AscStr  = "asc"
	DescStr = "desc"
)

Possible directions for OrderingTerm.

View Source
const (
	StrValue = ValueType(iota)
	IntValue
	FloatValue
	HexNumValue
	BlobValue
)

All possible value types.

View Source
const (
	UPlusStr  = "+"
	UMinusStr = "-"
	TildaStr  = "~"
)

Operators for UnaryExpr.

View Source
const (
	BitAndStr            = "&"
	BitOrStr             = "|"
	PlusStr              = "+"
	MinusStr             = "-"
	MultStr              = "*"
	DivStr               = "/"
	ModStr               = "%"
	ShiftLeftStr         = "<<"
	ShiftRightStr        = ">>"
	ConcatStr            = "||"
	JSONExtractOp        = "->"
	JSONUnquoteExtractOp = "->>"
)

Operators for BinaryExpr.

View Source
const (
	EqualStr        = "="
	LessThanStr     = "<"
	GreaterThanStr  = ">"
	LessEqualStr    = "<="
	GreaterEqualStr = ">="
	NotEqualStr     = "!="
	InStr           = "in"
	NotInStr        = "not in"
	LikeStr         = "like"
	NotLikeStr      = "not like"
	RegexpStr       = "regexp"
	NotRegexpStr    = "not regexp"
	MatchStr        = "match"
	NotMatchStr     = "not match"
	GlobStr         = "glob"
	NotGlobStr      = "not glob"
)

Operators for CmpExpr.

View Source
const (
	// NoneStr NONE convert type.
	NoneStr = ConvertType("none")

	// TextStr TEXT convert type.
	TextStr = ConvertType("text")

	// IntegerStr INTEGER convert type.
	IntegerStr = ConvertType("integer")
)
View Source
const (
	BetweenStr    = "between"
	NotBetweenStr = "not between"
)

Operators for BetweenExpr.

View Source
const (
	TypeIntStr     = "int"
	TypeIntegerStr = "integer"
	TypeTextStr    = "text"
	TypeBlobStr    = "blob"
)

Types for ColumnDef type.

View Source
const (
	// PrimaryKeyOrderEmpty no primary key order.
	PrimaryKeyOrderEmpty = ""

	// PrimaryKeyOrderAsc primary key asc order.
	PrimaryKeyOrderAsc = "asc"

	// PrimaryKeyOrderDesc primary key desc order.
	PrimaryKeyOrderDesc = "desc"
)
View Source
const (
	// MaxTextLength is the limit for the length of a TEXT literal value.
	MaxTextLength = 1024
	// MaxBlobLength is the limit for the length of a BLOB literal value.
	MaxBlobLength = 1024
	// MaxAllowedColumns is the limit for the number of columns in a CREATE TABLE statement.
	MaxAllowedColumns = 24
)
View Source
const ADD = 57417
View Source
const ALL = 57382
View Source
const ALTER = 57414
View Source
const ALWAYS = 57398
View Source
const AND = 57356
View Source
const ANDOP = 57430
View Source
const AS = 57361
View Source
const ASC = 57376
View Source
const BETWEEN = 57438
View Source
const BLOB = 57391
View Source
const BLOBVAL = 57351
View Source
const BY = 57371
View Source
const CASE = 57362
View Source
const CAST = 57360
View Source
const CHECK = 57395
View Source
const COLLATE = 57452
View Source
const COLUMN = 57416
View Source
const CONCAT = 57449
View Source
const CONFLICT = 57408
View Source
const CONSTRAINT = 57401
View Source
const CREATE = 57388
View Source
const CROSS = 57425
View Source
const DEFAULT = 57396
View Source
const DELETE = 57405
View Source
const DESC = 57377
View Source
const DISTINCT = 57381
View Source
const DO = 57409
View Source
const DROP = 57418
View Source
const ELSE = 57365
View Source
const END = 57366
View Source
const EOF = 0

EOF is the end of input.

View Source
const ERROR = 57352
View Source
const ESCAPE = 57446
View Source
const EXCEPT = 57386
View Source
const EXISTS = 57383
View Source
const FALSE = 57354
View Source
const FILTER = 57384
View Source
const FIRST = 57379
View Source
const FLOAT = 57350
View Source
const FROM = 57368
View Source
const FULL = 57420
View Source
const GE = 57444
View Source
const GENERATED = 57397
View Source
const GLOB = 57435
View Source
const GRANT = 57411
View Source
const GROUP = 57370
View Source
const HAVING = 57372
View Source
const HEXNUM = 57349
View Source
const IDENTIFIER = 57346
View Source
const IN = 57439
View Source
const INEQUALITY = 57445
View Source
const INNER = 57421
View Source
const INSERT = 57402
View Source
const INT = 57390
View Source
const INTEGER = 57358
View Source
const INTEGRAL = 57348
View Source
const INTERSECT = 57387
View Source
const INTO = 57403
View Source
const IS = 57432
View Source
const ISNOT = 57433
View Source
const ISNULL = 57440
View Source
const JOIN = 57426
View Source
const JSON_EXTRACT_OP = 57450
View Source
const JSON_UNQUOTE_EXTRACT_OP = 57451
View Source
const KEY = 57393
View Source
const LAST = 57380
View Source
const LE = 57443
View Source
const LEFT = 57422
View Source
const LIKE = 57437
View Source
const LIMIT = 57373
View Source
const LSHIFT = 57447
View Source
const MATCH = 57434
View Source
const NATURAL = 57423
View Source
const NE = 57442
View Source
const NONE = 57357
View Source
const NOT = 57431
View Source
const NOTHING = 57410
View Source
const NOTNULL = 57441
View Source
const NULL = 57355
View Source
const NULLS = 57378
View Source
const OFFSET = 57374
View Source
const ON = 57427
View Source
const OR = 57429
View Source
const ORDER = 57375
View Source
const OUTER = 57424
View Source
const PRIMARY = 57392
View Source
const REGEXP = 57436
View Source
const RENAME = 57415
View Source
const REVOKE = 57413
View Source
const RIGHT = 57419
View Source
const RSHIFT = 57448
View Source
const SELECT = 57367
View Source
const SET = 57407
View Source
const STORED = 57399
View Source
const STRING = 57347
View Source
const TABLE = 57389
View Source
const TEXT = 57359
View Source
const THEN = 57364
View Source
const TO = 57412
View Source
const TRUE = 57353
View Source
const UNARY = 57453
View Source
const UNION = 57385
View Source
const UNIQUE = 57394
View Source
const UPDATE = 57406
View Source
const USING = 57428
View Source
const VALUES = 57404
View Source
const VIRTUAL = 57400
View Source
const WHEN = 57363
View Source
const WHERE = 57369

Variables

View Source
var AllowedFunctions = map[string]bool{

	"abs": false,

	"char":     false,
	"coalesce": false,
	"format":   false,
	"glob":     false,
	"hex":      false,
	"ifnull":   false,
	"iif":      false,
	"instr":    false,

	"length": false,
	"like":   false,

	"lower":  false,
	"ltrim":  false,
	"max":    false,
	"min":    false,
	"nullif": false,
	"printf": false,
	"quote":  false,

	"replace": false,
	"round":   false,
	"rtrim":   false,
	"sign":    false,

	"substr":    false,
	"substring": false,

	"trim":    false,
	"typeof":  false,
	"unicode": false,

	"upper": false,

	"acos":    false,
	"acosh":   false,
	"asin":    false,
	"asinh":   false,
	"atan":    false,
	"atan2":   false,
	"atanh":   false,
	"ceil":    false,
	"ceiling": false,
	"cos":     false,
	"cosh":    false,
	"degrees": false,
	"exp":     false,
	"floor":   false,
	"ln":      false,
	"log":     false,
	"log10":   false,
	"log2":    false,
	"mod":     false,
	"pi":      false,
	"pow":     false,
	"power":   false,
	"radians": false,
	"sin":     false,
	"sinh":    false,
	"sqrt":    false,
	"tan":     false,
	"tanh":    false,
	"trunc":   false,

	"json":              false,
	"json_array":        false,
	"json_array_length": false,
	"json_extract":      false,
	"json_insert":       false,
	"json_object":       false,
	"json_patch":        false,
	"json_remove":       false,
	"json_replace":      false,
	"json_set":          false,
	"json_type":         false,
	"json_valid":        false,
	"json_quote":        false,
	"json_group_array":  false,
	"json_group_object": false,

	"avg":          false,
	"count":        false,
	"group_concat": false,

	"sum":   false,
	"total": false,

	"txn_hash":  true,
	"block_num": true,
}

AllowedFunctions is a map of allowed functions in Tableland. The value indicates if the function is custom.

Functions

func GetUniqueTableReferences

func GetUniqueTableReferences(node Node) []string

GetUniqueTableReferences returns a slice of tables' names referenced by the node.

func Walk

func Walk(visit Visit, nodes ...Node) error

Walk calls visit on every node. If visit returns false, the underlying nodes are also visited. If it returns an error, walking is interrupted, and the error is returned.

Types

type AST

type AST struct {
	Statements []Statement
	Errors     map[int]error
}

AST represents the root Node of the AST.

func Parse

func Parse(statement string) (*AST, error)

Parse parses an statement into an AST.

func (*AST) PrettyPrint

func (node *AST) PrettyPrint()

PrettyPrint prints the AST.

func (*AST) String

func (node *AST) String() string

type AliasedSelectColumn

type AliasedSelectColumn struct {
	Expr Expr
	As   Identifier
}

AliasedSelectColumn defines an aliased SELECT column.

func (*AliasedSelectColumn) String

func (node *AliasedSelectColumn) String() string

String returns the string representation of the node.

type AliasedTableExpr

type AliasedTableExpr struct {
	Expr SimpleTableExpr
	As   Identifier
}

AliasedTableExpr represents a table expression coupled with an optional alias. If As is empty, no alias was used.

func (*AliasedTableExpr) String

func (node *AliasedTableExpr) String() string

String returns the string representation of the node.

type AlterTable

type AlterTable struct {
	Table            *Table
	AlterTableClause AlterTableClause
}

AlterTable represents an ALTER TABLE statement.

func (*AlterTable) GetTable

func (node *AlterTable) GetTable() *Table

GetTable returns the table that ALTER refers to.

func (*AlterTable) Resolve

func (node *AlterTable) Resolve(resolver WriteStatementResolver) (string, error)

Resolve returns a string representation with custom function nodes resolved to the values passed by resolver.

func (*AlterTable) String

func (node *AlterTable) String() string

String returns the string representation of the node.

type AlterTableAdd

type AlterTableAdd struct {
	ColumnDef *ColumnDef
}

AlterTableAdd represents the alter table clause that adds a column.

func (*AlterTableAdd) String

func (node *AlterTableAdd) String() string

String returns the string representation of the node.

type AlterTableClause

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

AlterTableClause represents an ALTER TABLE operation such as RENAME, ADD, or DROP.

type AlterTableDrop

type AlterTableDrop struct {
	Column *Column
}

AlterTableDrop represents the alter table clause that drops a column.

func (*AlterTableDrop) String

func (node *AlterTableDrop) String() string

String returns the string representation of the node.

type AlterTableRename

type AlterTableRename struct {
	OldColumn *Column
	NewColumn *Column
}

AlterTableRename represents the alter table clause that renames a column.

func (*AlterTableRename) String

func (node *AlterTableRename) String() string

String returns the string representation of the node.

type AndExpr

type AndExpr struct {
	Left, Right Expr
}

AndExpr represents an AND expression.

func (*AndExpr) String

func (node *AndExpr) String() string

String returns the string representation of the node.

type BetweenExpr

type BetweenExpr struct {
	Operator string
	Left     Expr
	From, To Expr
}

BetweenExpr represents a BETWEEN or a NOT BETWEEN expression.

func (*BetweenExpr) String

func (node *BetweenExpr) String() string

String returns the string representation of the node.

type BinaryExpr

type BinaryExpr struct {
	Operator    string
	Left, Right Expr
}

BinaryExpr represents a binary value expression.

func (*BinaryExpr) String

func (node *BinaryExpr) String() string

String returns the string representation of the node.

type BoolValue

type BoolValue bool

BoolValue represents booleans.

func (BoolValue) String

func (node BoolValue) String() string

String returns the string representation of the node.

type CaseExpr

type CaseExpr struct {
	Expr  Expr
	Whens []*When
	Else  Expr
}

CaseExpr represents a CASE expression.

func (*CaseExpr) String

func (node *CaseExpr) String() string

String returns the string representation of the node.

type CmpExpr

type CmpExpr struct {
	Operator    string
	Left, Right Expr
	Escape      Expr
}

CmpExpr represents the comparison of two expressions.

func (*CmpExpr) String

func (node *CmpExpr) String() string

String returns the string representation of the node.

type ColTuple

type ColTuple interface {
	Expr
	// contains filtered or unexported methods
}

ColTuple represents a list of column values for IN operator. It can be ValTuple or Subquery.

type CollateExpr

type CollateExpr struct {
	Expr          Expr
	CollationName Identifier
}

CollateExpr the COLLATE operator.

func (*CollateExpr) String

func (node *CollateExpr) String() string

String returns the string representation of the node.

type Column

type Column struct {
	Name     Identifier
	TableRef *Table
}

Column represents a column.

func (*Column) String

func (node *Column) String() string

String returns the string representation of the node.

type ColumnConstraint

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

ColumnConstraint is used for parsing column constraint info from SQL.

type ColumnConstraintCheck

type ColumnConstraintCheck struct {
	Name Identifier
	Expr Expr
}

ColumnConstraintCheck represents a CHECK column constraint for CREATE TABLE.

func (*ColumnConstraintCheck) String

func (node *ColumnConstraintCheck) String() string

String returns the string representation of the node.

type ColumnConstraintDefault

type ColumnConstraintDefault struct {
	Name        Identifier
	Expr        Expr
	Parenthesis bool
}

ColumnConstraintDefault represents a DEFAULT column constraint for CREATE TABLE.

func (*ColumnConstraintDefault) String

func (node *ColumnConstraintDefault) String() string

String returns the string representation of the node.

type ColumnConstraintGenerated

type ColumnConstraintGenerated struct {
	Name Identifier
	Expr Expr

	// the GENERATED ALWAYS keywords are optional in the grammar.
	GeneratedAlways bool

	// this is a flag for VIRTUAL or STORED keywords.
	IsStored bool
}

ColumnConstraintGenerated represents a GENERATED ALWAYS column constraint for CREATE TABLE.

func (*ColumnConstraintGenerated) String

func (node *ColumnConstraintGenerated) String() string

String returns the string representation of the node.

type ColumnConstraintNotNull

type ColumnConstraintNotNull struct {
	Name Identifier
}

ColumnConstraintNotNull represents a NOT NULL column constraint for CREATE TABLE.

func (*ColumnConstraintNotNull) String

func (node *ColumnConstraintNotNull) String() string

String returns the string representation of the node.

type ColumnConstraintPrimaryKey

type ColumnConstraintPrimaryKey struct {
	Name          Identifier
	Order         string
	AutoIncrement bool
}

ColumnConstraintPrimaryKey represents a PRIMARY KEY column constraint for CREATE TABLE.

func (*ColumnConstraintPrimaryKey) String

func (node *ColumnConstraintPrimaryKey) String() string

String returns the string representation of the node.

type ColumnConstraintUnique

type ColumnConstraintUnique struct {
	Name Identifier
}

ColumnConstraintUnique represents a UNIQUE column constraint for CREATE TABLE.

func (*ColumnConstraintUnique) String

func (node *ColumnConstraintUnique) String() string

String returns the string representation of the node.

type ColumnDef

type ColumnDef struct {
	Column      *Column
	Type        string
	Constraints []ColumnConstraint
}

ColumnDef represents the column definition of a CREATE TABLE statement.

func (*ColumnDef) HasPrimaryKey

func (node *ColumnDef) HasPrimaryKey() bool

HasPrimaryKey checks if column definition has a primary key constraint.

func (*ColumnDef) String

func (node *ColumnDef) String() string

String returns the string representation of the node.

type ColumnList

type ColumnList []*Column

ColumnList is a list of columns.

func (ColumnList) String

func (node ColumnList) String() string

String returns the string representation of the node.

type CompoundSelect

type CompoundSelect struct {
	Left  *Select
	Type  string
	Right ReadStatement
}

CompoundSelect represents a compound operation of selects.

func (*CompoundSelect) Resolve

func (node *CompoundSelect) Resolve(resolver ReadStatementResolver) (string, error)

Resolve returns a string representation with custom function nodes resolved to the values passed by resolver.

func (*CompoundSelect) String

func (node *CompoundSelect) String() string

type ConvertExpr

type ConvertExpr struct {
	Expr Expr
	Type ConvertType
}

ConvertExpr represents a CAST expression.

func (*ConvertExpr) String

func (node *ConvertExpr) String() string

String returns the string representation of the node.

type ConvertType

type ConvertType string

ConvertType specifies the type for ConvertExpr.

type CreateTable

type CreateTable struct {
	Table       *Table
	ColumnsDef  []*ColumnDef
	Constraints []TableConstraint

	// This is the only TableOption supported in the AST.
	// The grammar cannot parse this option.
	// It is used to toggle the strict mode directiy in the AST.
	StrictMode bool
}

CreateTable represents a CREATE TABLE statement.

func (*CreateTable) String

func (node *CreateTable) String() string

String returns the string representation of the node.

func (*CreateTable) StructureHash

func (node *CreateTable) StructureHash() string

StructureHash returns the hash of the structure of the statement.

type CreateTableStatement

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

CreateTableStatement is any CREATE TABLE statement.

type CustomFuncExpr

type CustomFuncExpr struct {
	Name           Identifier
	Args           Exprs
	ResolvedString string
}

CustomFuncExpr represents a function call.

func (*CustomFuncExpr) String

func (node *CustomFuncExpr) String() string

String returns the string representation of the node.

type Delete

type Delete struct {
	Table *Table
	Where *Where
}

Delete represents an DELETE statement.

func (*Delete) AddWhereClause

func (node *Delete) AddWhereClause(where *Where)

AddWhereClause add a WHERE clause to DELETE.

func (*Delete) GetTable

func (node *Delete) GetTable() *Table

GetTable returns the table.

func (*Delete) Resolve

func (node *Delete) Resolve(resolver WriteStatementResolver) (string, error)

Resolve returns a string representation with custom function nodes resolved to the values passed by resolver.

func (*Delete) String

func (node *Delete) String() string

String returns the string representation of the node.

type ErrAlterTablePrimaryKeyNotAllowed

type ErrAlterTablePrimaryKeyNotAllowed struct{}

ErrAlterTablePrimaryKeyNotAllowed indicates that primary key is not allowed in ALTER TABLE.

func (*ErrAlterTablePrimaryKeyNotAllowed) Error

type ErrAlterTableUniqueNotAllowed

type ErrAlterTableUniqueNotAllowed struct{}

ErrAlterTableUniqueNotAllowed indicates that unique is not allowed in ALTER TABLE.

func (*ErrAlterTableUniqueNotAllowed) Error

type ErrBlobTooBig

type ErrBlobTooBig struct {
	Length     int
	MaxAllowed int
}

ErrBlobTooBig is an error returned when a query contains a BLOB constant that is too long.

func (*ErrBlobTooBig) Error

func (e *ErrBlobTooBig) Error() string

type ErrCompoudSelectNotAllowed

type ErrCompoudSelectNotAllowed struct{}

ErrCompoudSelectNotAllowed indicates that a compound SELECT is not allowed.

func (*ErrCompoudSelectNotAllowed) Error

type ErrContainsJoinTableExpr

type ErrContainsJoinTableExpr struct{}

ErrContainsJoinTableExpr indicates that a node contains a JOIN.

func (*ErrContainsJoinTableExpr) Error

func (e *ErrContainsJoinTableExpr) Error() string

type ErrGrantRepeatedPrivilege

type ErrGrantRepeatedPrivilege struct {
	Privilege string
}

ErrGrantRepeatedPrivilege indicates a repeated privilege.

func (*ErrGrantRepeatedPrivilege) Error

func (e *ErrGrantRepeatedPrivilege) Error() string

type ErrKeywordIsNotAllowed

type ErrKeywordIsNotAllowed struct {
	Keyword string
}

ErrKeywordIsNotAllowed indicates an error for keyword that is not allowed (eg CURRENT_TIME).

func (*ErrKeywordIsNotAllowed) Error

func (e *ErrKeywordIsNotAllowed) Error() string

type ErrMultiplePrimaryKey

type ErrMultiplePrimaryKey struct{}

ErrMultiplePrimaryKey indicates a that a CREATE statement has more than one primary key.

func (*ErrMultiplePrimaryKey) Error

func (e *ErrMultiplePrimaryKey) Error() string

type ErrNaturalJoinWithOnOrUsingClause

type ErrNaturalJoinWithOnOrUsingClause struct{}

ErrNaturalJoinWithOnOrUsingClause indicates that a ON or USING clause is used together with a NATURAL JOIN.

func (*ErrNaturalJoinWithOnOrUsingClause) Error

type ErrNoSuchFunction

type ErrNoSuchFunction struct {
	FunctionName string
}

ErrNoSuchFunction indicates that the function called does not exist.

func (*ErrNoSuchFunction) Error

func (e *ErrNoSuchFunction) Error() string

type ErrNotNullConstraintDefaultNotNull

type ErrNotNullConstraintDefaultNotNull struct{}

ErrNotNullConstraintDefaultNotNull indicates that you cannot add a not null constraint together with a not null default.

func (*ErrNotNullConstraintDefaultNotNull) Error

type ErrNumericLiteralFloat

type ErrNumericLiteralFloat struct {
	Value []byte
}

ErrNumericLiteralFloat indicates a literal numeric float is being used.

func (*ErrNumericLiteralFloat) Error

func (e *ErrNumericLiteralFloat) Error() string

type ErrRowIDNotAllowed

type ErrRowIDNotAllowed struct{}

ErrRowIDNotAllowed indicates a reference to the columns rowid, _rowid_, or oid in an INSERT, UPDATE or CREATE statement.

func (*ErrRowIDNotAllowed) Error

func (e *ErrRowIDNotAllowed) Error() string

type ErrStatementContainsSubquery

type ErrStatementContainsSubquery struct {
	StatementKind string
}

ErrStatementContainsSubquery indicates a statement contains a subquery.

func (*ErrStatementContainsSubquery) Error

type ErrSyntaxError

type ErrSyntaxError struct {
	YaccError string
	Position  int
	Literal   string
}

ErrSyntaxError indicates a syntax error.

func (*ErrSyntaxError) Error

func (e *ErrSyntaxError) Error() string

type ErrTableNameWrongFormat

type ErrTableNameWrongFormat struct {
	Name string
}

ErrTableNameWrongFormat indicates that a table's name has the wrong format.

func (*ErrTableNameWrongFormat) Error

func (e *ErrTableNameWrongFormat) Error() string

type ErrTextTooLong

type ErrTextTooLong struct {
	Length     int
	MaxAllowed int
}

ErrTextTooLong is an error returned when a query contains a text constant that is too long.

func (*ErrTextTooLong) Error

func (e *ErrTextTooLong) Error() string

type ErrTooManyColumns

type ErrTooManyColumns struct {
	ColumnCount int
	MaxAllowed  int
}

ErrTooManyColumns is an error returned when a create statement has more columns that allowed.

func (*ErrTooManyColumns) Error

func (e *ErrTooManyColumns) Error() string

type ErrUpdateColumnsAndValuesDiffer

type ErrUpdateColumnsAndValuesDiffer struct {
	ColumnsCount int
	ValuesCount  int
}

ErrUpdateColumnsAndValuesDiffer indicates that there's a mismatch between the number of columns and number of values.

func (*ErrUpdateColumnsAndValuesDiffer) Error

type ErrUpsertMissingTarget

type ErrUpsertMissingTarget struct{}

ErrUpsertMissingTarget indicates a missing conflict target. The conflict target may be omitted on the last ON CONFLICT clause in the INSERT statement, but is required for all other ON CONFLICT clause.

func (*ErrUpsertMissingTarget) Error

func (e *ErrUpsertMissingTarget) Error() string

type ExistsExpr

type ExistsExpr struct {
	Subquery *Subquery
}

ExistsExpr represents a EXISTS expression.

func (*ExistsExpr) String

func (node *ExistsExpr) String() string

String returns the string representation of the node.

type Expr

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

Expr represents an expr node in the AST.

type Exprs

type Exprs []Expr

Exprs represents a list of expressions.

func (Exprs) String

func (node Exprs) String() string

String returns the string representation of the node.

type FuncExpr

type FuncExpr struct {
	Name     Identifier
	Distinct bool
	Args     Exprs
	Filter   *Where
}

FuncExpr represents a function call.

func (*FuncExpr) String

func (node *FuncExpr) String() string

String returns the string representation of the node.

type Grant

type Grant struct {
	Privileges Privileges
	Table      *Table
	Roles      []string
}

Grant represents a GRANT statement.

func (*Grant) GetPrivileges

func (node *Grant) GetPrivileges() Privileges

GetPrivileges returns the privileges.

func (*Grant) GetRoles

func (node *Grant) GetRoles() []string

GetRoles returns the roles.

func (*Grant) GetTable

func (node *Grant) GetTable() *Table

GetTable returns the table.

func (*Grant) String

func (node *Grant) String() string

String returns the string representation of the node.

type GrantOrRevokeStatement

type GrantOrRevokeStatement interface {
	Statement

	GetRoles() []string
	GetPrivileges() Privileges
	GetTable() *Table
	// contains filtered or unexported methods
}

GrantOrRevokeStatement is any GRANT/REVOKE statement.

type GroupBy

type GroupBy Exprs

GroupBy represents a GROUP BY clause.

func (GroupBy) String

func (node GroupBy) String() string

String returns the string representation of the node.

type Identifier

type Identifier string

Identifier represents a Column, Table and Function name identifier.

func (Identifier) IsEmpty

func (node Identifier) IsEmpty() bool

IsEmpty returns if the identifier is empty.

func (Identifier) String

func (node Identifier) String() string

String returns the string representation of the node.

type IndexedColumn

type IndexedColumn struct {
	Column        *Column
	CollationName Identifier
	Order         string
}

IndexedColumn represents a indexed column.

func (*IndexedColumn) String

func (node *IndexedColumn) String() string

String returns the string representation of the node.

type IndexedColumnList

type IndexedColumnList []*IndexedColumn

IndexedColumnList is a list of indexed columns.

func (IndexedColumnList) String

func (node IndexedColumnList) String() string

String returns the string representation of the node.

type Insert

type Insert struct {
	Table         *Table
	Columns       ColumnList
	Rows          []Exprs
	DefaultValues bool
	Upsert        Upsert
	Select        *Select

	// RETURNING clause is not accepted in the parser.
	ReturningClause Exprs
}

Insert represents an INSERT statement.

func (*Insert) GetTable

func (node *Insert) GetTable() *Table

GetTable returns the table.

func (*Insert) Resolve

func (node *Insert) Resolve(resolver WriteStatementResolver) (string, error)

Resolve returns a string representation with custom function nodes resolved to the values passed by resolver.

func (*Insert) String

func (node *Insert) String() string

String returns the string representation of the node.

type IsExpr

type IsExpr struct {
	Left, Right Expr
}

IsExpr represents a IS expression.

func (*IsExpr) String

func (node *IsExpr) String() string

String returns the string representation of the node.

type IsNullExpr

type IsNullExpr struct {
	Expr Expr
}

IsNullExpr represents a IS expression.

func (*IsNullExpr) String

func (node *IsNullExpr) String() string

String returns the string representation of the node.

type JoinOperator

type JoinOperator struct {
	Op      string
	Natural bool
	Outer   bool
}

JoinOperator represents a join operator.

func (*JoinOperator) String

func (node *JoinOperator) String() string

type JoinTableExpr

type JoinTableExpr struct {
	LeftExpr     TableExpr
	JoinOperator *JoinOperator
	RightExpr    TableExpr
	On           Expr
	Using        ColumnList
}

JoinTableExpr represents a TableExpr that's a JOIN operation.

func (*JoinTableExpr) String

func (node *JoinTableExpr) String() string

String returns the string representation of the node.

type Lexer

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

Lexer is responsible for token generation.

func (*Lexer) AddError

func (l *Lexer) AddError(err error)

AddError keeps track of errors per statement for syntatically valid statements.

func (*Lexer) Error

func (l *Lexer) Error(e string)

Error is used for syntatically not valid statements.

func (*Lexer) Lex

func (l *Lexer) Lex(lval *yySymType) (token int)

Lex returns a token to be used in the parser.

type Limit

type Limit struct {
	Limit  Expr
	Offset Expr
}

Limit represents the LIMIT clause.

func (*Limit) String

func (node *Limit) String() string

String returns the string representation of the node.

type Node

type Node interface {
	String() string
	// contains filtered or unexported methods
}

Node represents a node in the AST.

type NotExpr

type NotExpr struct {
	Expr Expr
}

NotExpr represents an NOT expression.

func (*NotExpr) String

func (node *NotExpr) String() string

String returns the string representation of the node.

type NotNullExpr

type NotNullExpr struct {
	Expr Expr
}

NotNullExpr represents a IS expression.

func (*NotNullExpr) String

func (node *NotNullExpr) String() string

String returns the string representation of the node.

type NullValue

type NullValue struct{}

NullValue represents null values.

func (*NullValue) String

func (node *NullValue) String() string

String returns the string representation of the node.

type NullsType

type NullsType int

NullsType represents nulls type.

const (
	NullsNil NullsType = iota
	NullsFirst
	NullsLast
)

All values of NullsType type.

type OnConflictClause

type OnConflictClause struct {
	Target   *OnConflictTarget
	DoUpdate *OnConflictUpdate
}

OnConflictClause represents an ON CONFLICT clause for upserts.

func (*OnConflictClause) String

func (node *OnConflictClause) String() string

type OnConflictTarget

type OnConflictTarget struct {
	Columns ColumnList
	Where   *Where
}

OnConflictTarget represents an ON CONFLICT target for upserts.

type OnConflictUpdate

type OnConflictUpdate struct {
	Exprs UpdateExprs
	Where *Where
}

OnConflictUpdate represents an ON CONFLICT.

type OrExpr

type OrExpr struct {
	Left, Right Expr
}

OrExpr represents an OR expression.

func (*OrExpr) String

func (node *OrExpr) String() string

String returns the string representation of the node.

type OrderBy

type OrderBy []*OrderingTerm

OrderBy represents an ORDER BY clause.

func (OrderBy) String

func (node OrderBy) String() string

String returns the string representation of the node.

type OrderingTerm

type OrderingTerm struct {
	Expr      Expr
	Direction string
	Nulls     NullsType
}

OrderingTerm represents an ordering term expression.

func (*OrderingTerm) String

func (node *OrderingTerm) String() string

String returns the string representation of the node.

type ParenExpr

type ParenExpr struct {
	Expr Expr
}

ParenExpr represents a (expr) expression.

func (*ParenExpr) String

func (node *ParenExpr) String() string

String returns the string representation of the node.

type ParenTableExpr

type ParenTableExpr struct {
	TableExpr TableExpr
}

ParenTableExpr represents a parenthesized TableExpr.

func (*ParenTableExpr) String

func (node *ParenTableExpr) String() string

String returns the string representation of the node.

type Privileges

type Privileges map[string]struct{}

Privileges represents the GRANT privilges (INSERT, UPDATE, DELETE).

func (Privileges) Len

func (node Privileges) Len() int

Len returns the length of privileges slice.

func (Privileges) String

func (node Privileges) String() string

String returns the string representation of the node.

type ReadStatement

type ReadStatement interface {
	Statement

	// Resolve returns a string representation with custom function nodes resolved to the values
	// passed by resolver.
	Resolve(ReadStatementResolver) (string, error)
	// contains filtered or unexported methods
}

ReadStatement is any SELECT statement or UNION statement.

type ReadStatementResolver

type ReadStatementResolver interface {
	// GetBlockNumber returns the last known block number for the provided chainID. If the chainID isn't known,
	// it returns (0, false).
	GetBlockNumber(chainID int64) (int64, bool)
}

ReadStatementResolver resolves Tableland Custom Functions for a read statement.

type Revoke

type Revoke struct {
	Privileges Privileges
	Table      *Table
	Roles      []string
}

Revoke represents a REVOKE statement.

func (*Revoke) GetPrivileges

func (node *Revoke) GetPrivileges() Privileges

GetPrivileges returns the privileges.

func (*Revoke) GetRoles

func (node *Revoke) GetRoles() []string

GetRoles returns the roles.

func (*Revoke) GetTable

func (node *Revoke) GetTable() *Table

GetTable returns the table.

func (*Revoke) String

func (node *Revoke) String() string

String returns the string representation of the node.

type Select

type Select struct {
	Distinct         string
	SelectColumnList SelectColumnList
	From             TableExpr
	Where            *Where
	GroupBy          GroupBy
	Having           *Where
	Limit            *Limit
	OrderBy          OrderBy
}

Select represents a SELECT statement.

func (*Select) Resolve

func (node *Select) Resolve(resolver ReadStatementResolver) (string, error)

Resolve returns a string representation with custom function nodes resolved to the values passed by resolver.

func (*Select) String

func (node *Select) String() string

String returns the string representation of the node.

type SelectColumn

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

SelectColumn represents a SELECT column.

type SelectColumnList

type SelectColumnList []SelectColumn

SelectColumnList represents a list of columns of a SELECT.

func (SelectColumnList) String

func (node SelectColumnList) String() string

String returns the string representation of the node.

type SimpleTableExpr

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

SimpleTableExpr represents a direct table reference or a subquery.

type StarSelectColumn

type StarSelectColumn struct {
	TableRef *Table
}

StarSelectColumn defines a '*' or 'table.*' column.

func (*StarSelectColumn) String

func (node *StarSelectColumn) String() string

String returns the string representation of the node.

type Statement

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

Statement represents a SQL statement.

type Subquery

type Subquery struct {
	Select ReadStatement
}

Subquery represents a subquery.

func (*Subquery) String

func (node *Subquery) String() string

String returns the string representation of the node.

type Table

type Table struct {
	Name Identifier

	// IsTarget indicates if the table is a target of a statement or simply a reference.
	IsTarget bool
}

Table represents a table.

func (*Table) String

func (node *Table) String() string

String returns the string representation of the node.

type TableConstraint

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

TableConstraint is a contrainst applied to the whole table in a CREATE TABLE statement.

type TableConstraintCheck

type TableConstraintCheck struct {
	Name Identifier
	Expr Expr
}

TableConstraintCheck is a CHECK constraint for table definition.

func (*TableConstraintCheck) String

func (node *TableConstraintCheck) String() string

String returns the string representation of the node.

type TableConstraintPrimaryKey

type TableConstraintPrimaryKey struct {
	Name    Identifier
	Columns IndexedColumnList
}

TableConstraintPrimaryKey is a PRIMARY KEY constraint for table definition.

func (*TableConstraintPrimaryKey) String

func (node *TableConstraintPrimaryKey) String() string

String returns the string representation of the node.

type TableConstraintUnique

type TableConstraintUnique struct {
	Name    Identifier
	Columns ColumnList
}

TableConstraintUnique is a UNIQUE constraint for table definition.

func (*TableConstraintUnique) String

func (node *TableConstraintUnique) String() string

String returns the string representation of the node.

type TableExpr

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

TableExpr represents an expression referenced by FROM.

type UnaryExpr

type UnaryExpr struct {
	Operator string
	Expr     Expr
}

UnaryExpr represents a unary value expression.

func (*UnaryExpr) String

func (node *UnaryExpr) String() string

String returns the string representation of the node.

type Update

type Update struct {
	Table *Table
	Exprs UpdateExprs
	Where *Where

	// RETURNING clause is not accepted in the parser.
	ReturningClause Exprs
}

Update represents an UPDATE statement.

func (*Update) AddWhereClause

func (node *Update) AddWhereClause(where *Where)

AddWhereClause add a WHERE clause to UPDATE.

func (*Update) GetTable

func (node *Update) GetTable() *Table

GetTable returns the table.

func (*Update) Resolve

func (node *Update) Resolve(resolver WriteStatementResolver) (string, error)

Resolve returns a string representation with custom function nodes resolved to the values passed by resolver.

func (*Update) String

func (node *Update) String() string

String returns the string representation of the node.

type UpdateExpr

type UpdateExpr struct {
	Column *Column
	Expr   Expr
}

UpdateExpr represents an UPDATE SET expression (Column = Expr).

type UpdateExprs

type UpdateExprs []*UpdateExpr

UpdateExprs represents a slice of UpdateExpr.

func (UpdateExprs) String

func (node UpdateExprs) String() string

String returns the string representation of the node.

type Upsert

type Upsert []*OnConflictClause

Upsert represents an upsert clause, which is a list of on conflict clause.

func (Upsert) String

func (node Upsert) String() string

type ValidatedCreateTable

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

ValidatedCreateTable is a Table that was validated by ValidateCreateTargetTable.

func ValidateCreateTargetTable

func ValidateCreateTargetTable(table *Table) (*ValidatedCreateTable, error)

ValidateCreateTargetTable validates the table name for CREATE statements.

func (*ValidatedCreateTable) ChainID

func (node *ValidatedCreateTable) ChainID() int64

ChainID returns the table's chain id.

func (*ValidatedCreateTable) Name

func (node *ValidatedCreateTable) Name() string

Name returns the table's name.

func (*ValidatedCreateTable) Prefix

func (node *ValidatedCreateTable) Prefix() string

Prefix returns table's prefix.

type ValidatedTable

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

ValidatedTable is a Table that was validated by ValidateTargetTable.

func ValidateTargetTable

func ValidateTargetTable(table *Table) (*ValidatedTable, error)

ValidateTargetTable validates the tables' names of statements.

func ValidateTargetTables

func ValidateTargetTables(node Node) ([]*ValidatedTable, error)

ValidateTargetTables recursively validates all tables found in the node and return them.

func (*ValidatedTable) ChainID

func (node *ValidatedTable) ChainID() int64

ChainID returns the table's chain id.

func (*ValidatedTable) Name

func (node *ValidatedTable) Name() string

Name returns the table's name.

func (*ValidatedTable) Prefix

func (node *ValidatedTable) Prefix() string

Prefix returns table's prefix.

func (*ValidatedTable) TokenID

func (node *ValidatedTable) TokenID() int64

TokenID returns the table's token id.

type Value

type Value struct {
	Type  ValueType
	Value []byte
}

Value represents a single value.

func (*Value) String

func (node *Value) String() string

String returns the string representation of the node.

type ValueType

type ValueType int

ValueType specifies the type for ValueExpr.

type Visit

type Visit func(node Node) (stop bool, err error)

Visit defines the signature of a function that can be used to visit all nodes of a parse tree.

type When

type When struct {
	Condition Expr
	Value     Expr
}

When represents a WHEN sub-expression.

func (*When) String

func (node *When) String() string

String returns the string representation of the node.

type Where

type Where struct {
	Type string
	Expr Expr
}

Where represents a WHERE or HAVING clause.

func NewWhere

func NewWhere(typ string, expr Expr) *Where

NewWhere creates a WHERE or HAVING clause out of a Expr. If the expression is nil, it returns nil.

func (*Where) String

func (node *Where) String() string

String returns the string representation of the node.

type WriteStatement

type WriteStatement interface {
	Statement

	GetTable() *Table

	// Resolve returns a string representation with custom function nodes resolved to the values
	// passed by resolver.
	Resolve(WriteStatementResolver) (string, error)
	// contains filtered or unexported methods
}

WriteStatement is any INSERT, UPDATE or DELETE statement.

type WriteStatementResolver

type WriteStatementResolver interface {
	// GetTxnHash returns the transaction hash of the transaction containing the query being processed.
	GetTxnHash() string

	// GetBlockNumber returns the block number of the block containing query being processed.
	GetBlockNumber() int64
}

WriteStatementResolver resolves Tableland Custom Functions for a write statement.

Directories

Path Synopsis
cmd
wasm
nolint
nolint

Jump to

Keyboard shortcuts

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