Categorygithub.com/tablelandnetwork/sqlparser
modulepackage
0.0.0-20240913183758-18c2b8ea43ad
Repository: https://github.com/tablelandnetwork/sqlparser.git
Documentation: pkg.go.dev

# 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

# Packages

No description provided by the author
No description provided by the author

# Functions

GetUniqueTableReferences returns a slice of tables' names referenced by the node.
NewWhere creates a WHERE or HAVING clause out of a Expr.
Parse parses an statement into an AST.
ValidateCreateTargetTable validates the table name for CREATE statements.
ValidateTargetTable validates the tables' names of statements.
ValidateTargetTables recursively validates all tables found in the node and return them.
Walk calls visit on every node.

# Constants

No description provided by the author
No description provided by the author
Distinct/All.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Possible directions for OrderingTerm.
No description provided by the author
Operators for BetweenExpr.
Operators for BinaryExpr.
Operators for BinaryExpr.
No description provided by the author
No description provided by the author
All possible value types.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Compound Select operation types.
Compound Select operation types.
Compound Select operation types.
Compound Select operation types.
No description provided by the author
Operators for BinaryExpr.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Possible directions for OrderingTerm.
No description provided by the author
Distinct/All.
Operators for BinaryExpr.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
EOF is the end of input.
Operators for CmpExpr.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
All possible value types.
No description provided by the author
No description provided by the author
Kinds of JoinOperator.
No description provided by the author
No description provided by the author
No description provided by the author
Operators for CmpExpr.
No description provided by the author
Operators for CmpExpr.
Operators for CmpExpr.
No description provided by the author
No description provided by the author
Types for Where.
No description provided by the author
All possible value types.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Kinds of JoinOperator.
No description provided by the author
Operators for CmpExpr.
No description provided by the author
No description provided by the author
IntegerStr INTEGER convert type.
No description provided by the author
No description provided by the author
No description provided by the author
All possible value types.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Kinds of JoinOperator.
No description provided by the author
No description provided by the author
Operators for BinaryExpr.
Operators for BinaryExpr.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Kinds of JoinOperator.
Operators for CmpExpr.
Operators for CmpExpr.
No description provided by the author
Operators for CmpExpr.
No description provided by the author
No description provided by the author
No description provided by the author
Operators for CmpExpr.
MaxAllowedColumns is the limit for the number of columns in a CREATE TABLE statement.
MaxBlobLength is the limit for the length of a BLOB literal value.
MaxTextLength is the limit for the length of a TEXT literal value.
Operators for BinaryExpr.
Operators for BinaryExpr.
Operators for BinaryExpr.
No description provided by the author
No description provided by the author
No description provided by the author
NoneStr NONE convert type.
No description provided by the author
Operators for BetweenExpr.
Operators for CmpExpr.
Operators for CmpExpr.
No description provided by the author
Operators for CmpExpr.
Operators for CmpExpr.
Operators for CmpExpr.
No description provided by the author
Operators for CmpExpr.
No description provided by the author
No description provided by the author
All values of NullsType type.
All values of NullsType type.
All values of NullsType type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Operators for BinaryExpr.
No description provided by the author
PrimaryKeyOrderAsc primary key asc order.
PrimaryKeyOrderDesc primary key desc order.
PrimaryKeyOrderEmpty no primary key order.
No description provided by the author
Operators for CmpExpr.
No description provided by the author
No description provided by the author
No description provided by the author
Kinds of JoinOperator.
No description provided by the author
No description provided by the author
No description provided by the author
Operators for BinaryExpr.
Operators for BinaryExpr.
No description provided by the author
No description provided by the author
All possible value types.
No description provided by the author
No description provided by the author
TextStr TEXT convert type.
No description provided by the author
Operators for UnaryExpr.
No description provided by the author
No description provided by the author
Types for ColumnDef type.
Types for ColumnDef type.
Types for ColumnDef type.
Types for ColumnDef type.
Operators for UnaryExpr.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Operators for UnaryExpr.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Types for Where.

# Variables

AllowedFunctions is a map of allowed functions in Tableland.

# Structs

AliasedSelectColumn defines an aliased SELECT column.
AliasedTableExpr represents a table expression coupled with an optional alias.
AlterTable represents an ALTER TABLE statement.
AlterTableAdd represents the alter table clause that adds a column.
AlterTableDrop represents the alter table clause that drops a column.
AlterTableRename represents the alter table clause that renames a column.
AndExpr represents an AND expression.
AST represents the root Node of the AST.
BetweenExpr represents a BETWEEN or a NOT BETWEEN expression.
BinaryExpr represents a binary value expression.
CaseExpr represents a CASE expression.
CmpExpr represents the comparison of two expressions.
CollateExpr the COLLATE operator.
Column represents a column.
ColumnConstraintCheck represents a CHECK column constraint for CREATE TABLE.
ColumnConstraintDefault represents a DEFAULT column constraint for CREATE TABLE.
ColumnConstraintGenerated represents a GENERATED ALWAYS column constraint for CREATE TABLE.
ColumnConstraintNotNull represents a NOT NULL column constraint for CREATE TABLE.
ColumnConstraintPrimaryKey represents a PRIMARY KEY column constraint for CREATE TABLE.
ColumnConstraintUnique represents a UNIQUE column constraint for CREATE TABLE.
ColumnDef represents the column definition of a CREATE TABLE statement.
CompoundSelect represents a compound operation of selects.
ConvertExpr represents a CAST expression.
CreateTable represents a CREATE TABLE statement.
CustomFuncExpr represents a function call.
Delete represents an DELETE statement.
ErrAlterTablePrimaryKeyNotAllowed indicates that primary key is not allowed in ALTER TABLE.
ErrAlterTableUniqueNotAllowed indicates that unique is not allowed in ALTER TABLE.
ErrBlobTooBig is an error returned when a query contains a BLOB constant that is too long.
ErrCompoudSelectNotAllowed indicates that a compound SELECT is not allowed.
ErrContainsJoinTableExpr indicates that a node contains a JOIN.
ErrGrantRepeatedPrivilege indicates a repeated privilege.
ErrKeywordIsNotAllowed indicates an error for keyword that is not allowed (eg CURRENT_TIME).
ErrMultiplePrimaryKey indicates a that a CREATE statement has more than one primary key.
ErrNaturalJoinWithOnOrUsingClause indicates that a ON or USING clause is used together with a NATURAL JOIN.
ErrNoSuchFunction indicates that the function called does not exist.
ErrNotNullConstraintDefaultNotNull indicates that you cannot add a not null constraint together with a not null default.
ErrNumericLiteralFloat indicates a literal numeric float is being used.
ErrRowIDNotAllowed indicates a reference to the columns rowid, _rowid_, or oid in an INSERT, UPDATE or CREATE statement.
ErrStatementContainsSubquery indicates a statement contains a subquery.
ErrSyntaxError indicates a syntax error.
ErrTableNameWrongFormat indicates that a table's name has the wrong format.
ErrTextTooLong is an error returned when a query contains a text constant that is too long.
ErrTooManyColumns is an error returned when a create statement has more columns that allowed.
ErrUpdateColumnsAndValuesDiffer indicates that there's a mismatch between the number of columns and number of values.
ErrUpsertMissingTarget indicates a missing conflict target.
ExistsExpr represents a EXISTS expression.
FuncExpr represents a function call.
Grant represents a GRANT statement.
IndexedColumn represents a indexed column.
Insert represents an INSERT statement.
IsExpr represents a IS expression.
IsNullExpr represents a IS expression.
JoinOperator represents a join operator.
JoinTableExpr represents a TableExpr that's a JOIN operation.
Lexer is responsible for token generation.
Limit represents the LIMIT clause.
NotExpr represents an NOT expression.
NotNullExpr represents a IS expression.
NullValue represents null values.
OnConflictClause represents an ON CONFLICT clause for upserts.
OnConflictTarget represents an ON CONFLICT target for upserts.
OnConflictUpdate represents an ON CONFLICT.
OrderingTerm represents an ordering term expression.
OrExpr represents an OR expression.
Param represents a question mark (?) parameter.
ParenExpr represents a (expr) expression.
ParenTableExpr represents a parenthesized TableExpr.
Revoke represents a REVOKE statement.
Select represents a SELECT statement.
StarSelectColumn defines a '*' or 'table.*' column.
Subquery represents a subquery.
Table represents a table.
TableConstraintCheck is a CHECK constraint for table definition.
TableConstraintPrimaryKey is a PRIMARY KEY constraint for table definition.
TableConstraintUnique is a UNIQUE constraint for table definition.
UnaryExpr represents a unary value expression.
Update represents an UPDATE statement.
UpdateExpr represents an UPDATE SET expression (Column = Expr).
ValidatedCreateTable is a Table that was validated by ValidateCreateTargetTable.
ValidatedTable is a Table that was validated by ValidateTargetTable.
Value represents a single value.
When represents a WHEN sub-expression.
Where represents a WHERE or HAVING clause.

# Interfaces

AlterTableClause represents an ALTER TABLE operation such as RENAME, ADD, or DROP.
ColTuple represents a list of column values for IN operator.
ColumnConstraint is used for parsing column constraint info from SQL.
CreateTableStatement is any CREATE TABLE statement.
Expr represents an expr node in the AST.
GrantOrRevokeStatement is any GRANT/REVOKE statement.
Node represents a node in the AST.
ReadStatement is any SELECT statement or UNION statement.
ReadStatementResolver resolves Tableland Custom Functions for a read statement.
SelectColumn represents a SELECT column.
SimpleTableExpr represents a direct table reference or a subquery.
Statement represents a SQL statement.
TableConstraint is a contrainst applied to the whole table in a CREATE TABLE statement.
TableExpr represents an expression referenced by FROM.
WriteStatement is any INSERT, UPDATE or DELETE statement.
WriteStatementResolver resolves Tableland Custom Functions for a write statement.

# Type aliases

BoolValue represents booleans.
ColumnList is a list of columns.
ConvertType specifies the type for ConvertExpr.
Exprs represents a list of expressions.
GroupBy represents a GROUP BY clause.
Identifier represents a Column, Table and Function name identifier.
IndexedColumnList is a list of indexed columns.
NullsType represents nulls type.
OrderBy represents an ORDER BY clause.
Privileges represents the GRANT privilges (INSERT, UPDATE, DELETE).
SelectColumnList represents a list of columns of a SELECT.
UpdateExprs represents a slice of UpdateExpr.
Upsert represents an upsert clause, which is a list of on conflict clause.
ValueType specifies the type for ValueExpr.
Visit defines the signature of a function that can be used to visit all nodes of a parse tree.