Categorygithub.com/ikaiguang/go-sqlparser
modulepackage
1.0.0
Repository: https://github.com/ikaiguang/go-sqlparser.git
Documentation: pkg.go.dev

# README

go-sqlparser

sqlparser, SQL Parser, go sql parser, golang sql parser

摘自 https://github.com/vitessio/vitess

版本


go version go1.11.1 darwin/amd64

cd $GOPATH/src/github.com/vitessio/vitess

git log


commit 32dd398dd5459e5cf15904db35aba7460519b612 (HEAD -> master, origin/master, origin/HEAD)
Merge: 714e12689 066779f52
Author: Michael Demmer <[email protected]>
Date:   Wed Nov 21 21:19:16 2018 -0500

    Merge pull request #4336 from planetscale/ss-vtgatev3
    
    select *: expand column list for tables with authoritative column lists

commit 714e126899c76a821dac78148f2d51550621edaa
...

Usage

import ( "github.com/ikaiguang/go-sqlparser" )

example

parse_test.go

parse_next_test.go ...

my example


// TestParse Parse
func TestParse(t *testing.T) {
	return

	src := "CREATE TABLE foo (a SMALLINT UNSIGNED, b INT UNSIGNED); -- foo\nSelect --1 from foo;"

	stat, err := sqlparser.Parse(src)
	//stat, err := sqlparser.ParseStrictDDL(src)
	if err != nil {
		t.Errorf("sqlparser.Parse error : %v \n", err)
		return
	}
	t.Logf("sqlparser.Parse success \n")
	t.Logf("%#v \n", stat)

	ddl, ok := stat.(*sqlparser.DDL)
	if !ok {
		t.Errorf("stat.(*sqlparser.DDL) convert fail")
		return
	}
	t.Logf("%#v \n", ddl)
}

// TestTokenizer Tokenizer
func TestTokenizer(t *testing.T) {
	return

	src := `
CREATE TABLE foo (a SMALLINT UNSIGNED, b INT UNSIGNED);
CREATE TABLE bar (a SMALLINT UNSIGNED, b INT UNSIGNED);
CREATE TABLE bar (a SMALLINT UNSIGNED, b INT UNSIGNED);
`
	tokenizer := sqlparser.NewStringTokenizer(src)

	for {
		stat, err := sqlparser.ParseNext(tokenizer)
		if err != nil {
			if err == io.EOF {
				t.Logf("parse done !")
				break
			}
			t.Errorf("sqlparser.ParseNext(tokenizer) fail : %v", err)
			break
		}
		ddl, ok := stat.(*sqlparser.DDL)
		if !ok {
			t.Errorf("stat.(*sqlparser.DDL) convert fail")
			return
		}
		t.Logf("%#v \n", ddl)
	}
}

// TestSplitStatement SplitStatement
func TestSplitStatement(t *testing.T) {
	return

	src := `
CREATE TABLE foo (a SMALLINT UNSIGNED, b INT UNSIGNED);
CREATE TABLE bar (a SMALLINT UNSIGNED, b INT UNSIGNED);
CREATE TABLE bar (a SMALLINT UNSIGNED, b INT UNSIGNED);
`
	for len(src) > 0 {
		// get one
		str1, str2, err := sqlparser.SplitStatement(src)
		if err != nil {
			if err == io.EOF {
				t.Logf("split done !")
				break
			}
			t.Errorf("sqlparser.SplitStatement(src) fail : %v", err)
			break
		}
		t.Logf("%#v \n", str1)
		//t.Logf("%#v \n", str2)

		src = str2
	}
}

// TestSplitStatementToPieces SplitStatementToPieces
func TestSplitStatementToPieces(t *testing.T) {
	//return

	src := `
-- a
CREATE TABLE foo (a SMALLINT UNSIGNED, b INT UNSIGNED);
-- b
CREATE TABLE bar (a SMALLINT UNSIGNED, b INT UNSIGNED);
-- c
#CREATE TABLE ccc (a SMALLINT UNSIGNED, b INT UNSIGNED);
-- d
#CREATE TABLE ccc (a SMALLINT UNSIGNED, b INT UNSIGNED);
`
	pieces, err := sqlparser.SplitStatementToPieces(src)
	if err != nil {
		t.Errorf("sqlparser.SplitStatementToPieces(src) fail : %v", err)
		return
	}
	t.Logf("%#v \n", len(pieces))
	t.Logf("%#v \n", pieces)

	t.Logf("%#v \n", "~~~~~")

	for _, sql := range pieces {
		//stat, err := sqlparser.Parse(sql) // last sql error
		stat, err := sqlparser.ParseStrictDDL(sql) // last sql error
		if err != nil {
			t.Errorf("sqlparser.Parse error : %v \n", err)
			return
		}

		ddl, ok := stat.(*sqlparser.DDL)
		if !ok {
			t.Errorf("stat.(*sqlparser.DDL) convert fail")
			return
		}
		t.Logf("%#v \n", ddl)
	}
}

# Packages

No description provided by the author
Package hack gives you some efficient functionality at the cost of breaking some Go rules.
Package sqltypes implements interfaces and types that represent SQL values.
No description provided by the author

# Functions

Append appends the SQLNode to the buffer.
BuildParsedQuery builds a ParsedQuery from the input.
EncodeValue encodes one bind variable value into the query.
ExprFromValue converts the given Value into an Expr or returns an error.
ExtractCommentDirectives parses the comment list for any execution directives of the form: /*vt+ OPTION_ONE=1 OPTION_TWO OPTION_THREE=abcd */ It returns the map of the directive values or nil if there aren't any.
ExtractMysqlComment extracts the version and SQL from a comment-only query such as /*!50708 sql here */.
ExtractSetValues returns a map of key-value pairs if the query is a SET statement.
FetchBindVar resolves the bind variable by fetching it from bindVariables.
FormatImpossibleQuery creates an impossible query in a TrackedBuffer.
GetBindvars returns a map of the bind vars referenced in the statement.
GetTableName returns the table name from the SimpleTableExpr only if it's a simple expression.
IsColName returns true if the Expr is a *ColName.
IsDML returns true if the query is an INSERT, UPDATE or DELETE statement.
IsNull returns true if the Expr is SQL NULL.
IsSimpleTuple returns true if the Expr is a ValTuple that contains simple values or if it's a list arg.
IsValue returns true if the Expr is a string, integral or value arg.
KeywordString returns the string corresponding to the given keyword.
NewBitVal builds a new BitVal containing a bit literal.
NewColIdent makes a new ColIdent.
NewFloatVal builds a new FloatVal.
NewHexNum builds a new HexNum.
NewHexVal builds a new HexVal.
NewIntVal builds a new IntVal.
NewParsedQuery returns a ParsedQuery of the ast.
NewPlanValue builds a sqltypes.PlanValue from an Expr.
NewStringTokenizer creates a new Tokenizer for the sql string.
NewStrVal builds a new StrVal.
NewTableIdent creates a new TableIdent.
NewTokenizer creates a new Tokenizer reading a sql string from the io.Reader.
NewTrackedBuffer creates a new TrackedBuffer.
NewValArg builds a new ValArg.
NewWhere creates a WHERE or HAVING clause out of a Expr.
Normalize changes the statement to use bind values, and updates the bind vars to those values.
Parse parses the SQL in full and returns a Statement, which is the AST representation of the query.
ParseNext parses a single SQL statement from the tokenizer returning a Statement which is the AST representation of the query.
ParseStrictDDL is the same as Parse except it errors on partially parsed DDL statements.
Preview analyzes the beginning of the query using a simpler and faster textual comparison to identify the statement type.
RedactSQLQuery returns a sql string with the params stripped out for display.
ReplaceExpr finds the from expression from root and replaces it with to.
SplitMarginComments pulls out any leading or trailing comments from a raw sql query.
SplitStatement returns the first sql statement up to either a ; or EOF and the remainder from the given buffer.
SplitStatementToPieces split raw sql statement that may have multi sql pieces to sql pieces returns the sql pieces blob contains; or error if sql cannot be parsed.
StmtType returns the statement type as a string.
String returns a string representation of an SQLNode.
StringIn is a convenience function that returns true if str matches any of the values.
StripLeadingComments trims the SQL string and removes any leading comments.
TruncateForLog is used when displaying queries as part of error logs to avoid overwhelming logging systems with potentially long queries and bind value data.
TruncateForUI is used when displaying queries on various Vitess status pages to keep the pages small enough to load and render properly.
Walk calls visit on every node.

# Constants

No description provided by the author
DDL strings.
No description provided by the author
No description provided by the author
No description provided by the author
DDL strings.
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
Order.Direction.
No description provided by the author
UnaryExpr.Operator.
No description provided by the author
No description provided by the author
RangeCond.Operator.
No description provided by the author
No description provided by the author
UnaryExpr.Operator.
No description provided by the author
No description provided by the author
BinaryExpr.Operator.
BinaryExpr.Operator.
These are the possible Valtype values.
BinaryExpr.Operator.
No description provided by the author
No description provided by the author
No description provided by the author
MatchExpr.Option.
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
this string is "character set" and this comment is required.
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
No description provided by the author
DDL strings.
DDL strings.
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
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
Order.Direction.
No description provided by the author
Select.Distinct.
No description provided by the author
BinaryExpr.Operator.
No description provided by the author
No description provided by the author
DDL strings.
DDL strings.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ComparisonExpr.Operator.
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
These are the possible Valtype values.
No description provided by the author
No description provided by the author
Index hints.
No description provided by the author
Select.Lock.
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
Set.Scope or Show.Scope.
ComparisonExpr.Operator.
ComparisonExpr.Operator.
No description provided by the author
No description provided by the author
No description provided by the author
Where.Type.
No description provided by the author
These are the possible Valtype values.
No description provided by the author
These are the possible Valtype values.
No description provided by the author
No description provided by the author
No description provided by the author
Index hints.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
DDL strings.
ComparisonExpr.Operator.
No description provided by the author
BinaryExpr.Operator.
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
These are the possible Valtype values.
No description provided by the author
IsExpr.Operator.
IsExpr.Operator.
IsExpr.Operator.
IsExpr.Operator.
IsExpr.Operator.
IsExpr.Operator.
No description provided by the author
JoinTableExpr.Join.
No description provided by the author
No description provided by the author
No description provided by the author
ComparisonExpr.Operator.
ComparisonExpr.Operator.
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
JoinTableExpr.Join.
No description provided by the author
ComparisonExpr.Operator.
ComparisonExpr.Operator.
No description provided by the author
No description provided by the author
ComparisonExpr.Operator.
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
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
BinaryExpr.Operator.
No description provided by the author
No description provided by the author
BinaryExpr.Operator.
No description provided by the author
No description provided by the author
No description provided by the author
BinaryExpr.Operator.
No description provided by the author
No description provided by the author
JoinTableExpr.Join.
MatchExpr.Option.
MatchExpr.Option.
JoinTableExpr.Join.
JoinTableExpr.Join.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RangeCond.Operator.
ComparisonExpr.Operator.
ComparisonExpr.Operator.
ComparisonExpr.Operator.
ComparisonExpr.Operator.
No description provided by the author
No description provided by the author
ComparisonExpr.Operator.
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
No description provided by the author
BinaryExpr.Operator.
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
MatchExpr.Option.
No description provided by the author
No description provided by the author
ComparisonExpr.Operator.
No description provided by the author
DDL strings.
No description provided by the author
Partition strings.
No description provided by the author
No description provided by the author
DDL strings.
No description provided by the author
JoinTableExpr.Join.
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
Set.Scope or Show.Scope.
No description provided by the author
No description provided by the author
Select.Lock.
No description provided by the author
No description provided by the author
BinaryExpr.Operator.
BinaryExpr.Operator.
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
Select.Cache.
Select.Cache.
No description provided by the author
No description provided by the author
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
These constants are used to identify the SQL statement type.
No description provided by the author
Select.Distinct.
JoinTableExpr.Join.
No description provided by the author
No description provided by the author
These are the possible Valtype values.
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
UnaryExpr.Operator.
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
No description provided by the author
No description provided by the author
DDL strings.
UnaryExpr.Operator.
UnaryExpr.Operator.
No description provided by the author
No description provided by the author
No description provided by the author
Union.Type.
Union.Type.
Union.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
UnaryExpr.Operator.
No description provided by the author
Index hints.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
These are the possible Valtype values.
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
No description provided by the author
Vindex DDL param to specify the owner of a vindex.
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
Where.Type.
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

Aggregates is a map of all aggregate functions.
TruncateErrLen truncate queries in error logs to the given length.
TruncateUILen truncate queries in debug UIs to the given length.

# Structs

AliasedExpr defines an aliased SELECT expression.
AliasedTableExpr represents a table expression coupled with an optional alias or index hint.
AndExpr represents an AND expression.
Begin represents a Begin statement.
BinaryExpr represents a binary value expression.
CaseExpr represents a CASE expression.
ColIdent is a case insensitive SQL identifier.
CollateExpr represents dynamic collate operator.
ColName represents a column name.
ColumnDefinition describes a column in a CREATE TABLE statement.
ColumnType represents a sql type in a CREATE TABLE statement All optional fields are nil if not specified.
Commit represents a Commit statement.
ComparisonExpr represents a two-value comparison expression.
ConvertExpr represents a call to CONVERT(expr, type) or it's equivalent CAST(expr AS type).
ConvertType represents the type in call to CONVERT(expr, type).
ConvertUsingExpr represents a call to CONVERT(expr USING charset).
DBDDL represents a CREATE, DROP database statement.
DDL represents a CREATE, ALTER, DROP, RENAME or TRUNCATE statement.
Default represents a DEFAULT expression.
Delete represents a DELETE statement.
ExistsExpr represents an EXISTS expression.
FuncExpr represents a function call.
GroupConcatExpr represents a call to GROUP_CONCAT.
IndexColumn describes a column in an index definition with optional length.
IndexDefinition describes an index in a CREATE TABLE statement.
IndexHints represents a list of index hints.
IndexInfo describes the name and type of an index in a CREATE TABLE statement.
IndexOption is used for trailing options for indexes: COMMENT, KEY_BLOCK_SIZE, USING.
Insert represents an INSERT or REPLACE statement.
IntervalExpr represents a date-time INTERVAL expression.
IsExpr represents an IS ..
JoinCondition represents the join conditions (either a ON or USING clause) of a JoinTableExpr.
JoinTableExpr represents a TableExpr that's a JOIN operation.
LengthScaleOption is used for types that have an optional length and scale.
Limit represents a LIMIT clause.
MarginComments holds the leading and trailing comments that surround a query.
MatchExpr represents a call to the MATCH function.
Nextval defines the NEXT VALUE expression.
NotExpr represents a NOT expression.
NullVal represents a NULL value.
Order represents an ordering expression.
OrExpr represents an OR expression.
OtherAdmin represents a misc statement that relies on ADMIN privileges, such as REPAIR, OPTIMIZE, or TRUNCATE statement.
OtherRead represents a DESCRIBE, or EXPLAIN statement.
ParenExpr represents a parenthesized boolean expression.
ParenSelect is a parenthesized SELECT statement.
ParenTableExpr represents a parenthesized list of TableExpr.
ParsedQuery represents a parsed query where bind locations are precompued for fast substitutions.
PartitionDefinition describes a very minimal partition definition.
PartitionSpec describe partition actions (for alter and create).
RangeCond represents a BETWEEN or a NOT BETWEEN expression.
Rollback represents a Rollback statement.
Select represents a SELECT statement.
Set represents a SET statement.
SetExpr represents a set expression.
Show represents a show statement.
SQLVal represents a single value.
StarExpr defines a '*' or 'table.*' expression.
Stream represents a SELECT statement.
Subquery represents a subquery.
SubstrExpr represents a call to SubstrExpr(column, value_expression) or SubstrExpr(column, value_expression,value_expression) also supported syntax SubstrExpr(column from value_expression for value_expression).
TableIdent is a case sensitive SQL identifier.
TableName represents a table name.
TableSpec describes the structure of a table from a CREATE TABLE statement.
Tokenizer is the struct used to generate SQL tokens for the parser.
TrackedBuffer is used to rebuild a query from the ast.
TupleEqualityList is for generating equality constraints for tables that have composite primary keys.
UnaryExpr represents a unary value expression.
Union represents a UNION statement.
Update represents an UPDATE statement.
UpdateExpr represents an update expression.
Use represents a use statement.
ValuesFuncExpr represents a function call.
VindexParam defines a key/value parameter for a CREATE VINDEX statement.
VindexSpec defines a vindex for a CREATE VINDEX or DROP VINDEX statement.
When represents a WHEN sub-expression.
Where represents a WHERE or HAVING clause.

# Interfaces

ColTuple represents a list of column values.
Encodable defines the interface for types that can be custom-encoded into SQL.
Expr represents an expression.
InsertRows represents the rows for an INSERT statement.
SelectExpr represents a SELECT expression.
SelectStatement any SELECT statement.
SimpleTableExpr represents a simple table expression.
SQLNode defines the interface for all nodes generated by the parser.
Statement represents a statement.
TableExpr represents a table expression.

# Type aliases

BoolVal is true or false.
ColumnKeyOption indicates whether or not the given column is defined as an index element and contains the type of the option.
Columns represents an insert column list.
CommentDirectives is the parsed representation for execution directives conveyed in query comments.
Comments represents a list of comments.
Exprs represents a list of value expressions.
GroupBy represents a GROUP BY clause.
InsertValues is a custom SQL encoder for the values of an insert statement.
ListArg represents a named list argument.
NodeFormatter defines the signature of a custom node formatter function that can be given to TrackedBuffer for code generation.
OnDup represents an ON DUPLICATE KEY clause.
OrderBy represents an ORDER By clause.
Partitions is a type alias for Columns so we can handle printing efficiently.
SelectExprs represents SELECT expressions.
SetExprs represents a list of set expressions.
TableExprs represents a list of table expressions.
TableNames is a list of TableName.
UpdateExprs represents a list of update expressions.
ValTuple represents a tuple of actual values.
ValType specifies the type for SQLVal.
Values represents a VALUES clause.
Visit defines the signature of a function that can be used to visit all nodes of a parse tree.