Documentation
¶
Overview ¶
Package machparse provides a high-performance SQL parser.
machparse is a dialect-agnostic SQL parser that supports MySQL, PostgreSQL, and SQLite query syntax. It provides Parse, Walk, and Rewrite functionality similar to vitess-sqlparser.
Basic usage:
stmt, err := machparse.Parse("SELECT * FROM users WHERE id = 1")
if err != nil {
log.Fatal(err)
}
fmt.Println(machparse.String(stmt))
Walking the AST:
machparse.Walk(stmt, func(node ast.Node) bool {
if col, ok := node.(*ast.ColName); ok {
fmt.Printf("Found column: %s\n", col.Name)
}
return true
})
Rewriting nodes:
rewritten := machparse.Rewrite(stmt, func(n ast.Node) ast.Node {
// Transform nodes as needed
return n
})
Index ¶
- Constants
- func Parse(sql string) (ast.Statement, error)
- func ParseAll(sql string) ([]ast.Statement, error)
- func Repool(stmt Statement)
- func Rewrite(node ast.Node, fn func(ast.Node) ast.Node) ast.Node
- func String(node ast.Node) string
- func Walk(node ast.Node, fn func(ast.Node) bool)
- type AliasedExpr
- type AliasedTableExpr
- type AlterTableStmt
- type BetweenExpr
- type BinaryExpr
- type CTE
- type CaseExpr
- type CastExpr
- type ColName
- type CreateIndexStmt
- type CreateTableStmt
- type DeleteStmt
- type DropIndexStmt
- type DropTableStmt
- type ExistsExpr
- type ExplainStmt
- type Expr
- type FuncExpr
- type InExpr
- type InsertStmt
- type IsExpr
- type JoinExpr
- type LikeExpr
- type Limit
- type Literal
- type Node
- type OrderByExpr
- type ParenExpr
- type SelectStmt
- type StarExpr
- type Statement
- type Subquery
- type TableName
- type TruncateStmt
- type UnaryExpr
- type UpdateStmt
- type WithClause
Constants ¶
const ( JoinInner = ast.JoinInner JoinLeft = ast.JoinLeft JoinRight = ast.JoinRight JoinFull = ast.JoinFull JoinCross = ast.JoinCross )
Join types
const ( LiteralNull = ast.LiteralNull LiteralInt = ast.LiteralInt LiteralFloat = ast.LiteralFloat LiteralString = ast.LiteralString LiteralBool = ast.LiteralBool )
Literal types
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse parses a single SQL statement. The parser uses internal pooling for efficiency. For maximum performance when parsing many queries, call Repool(stmt) when done with the statement (optional, see Repool).
func ParseAll ¶
ParseAll parses all statements in the input. For maximum performance, call Repool on each statement when done (optional).
func Repool ¶
func Repool(stmt Statement)
Repool returns AST nodes to internal pools for reuse. This is optional - if not called, nodes are garbage collected normally. Calling Repool after you're done with a statement improves performance when parsing many queries by reducing allocations.
Example:
stmt, err := machparse.Parse(sql)
if err != nil {
return err
}
defer machparse.Repool(stmt)
// ... use stmt ...
Types ¶
type AliasedTableExpr ¶
type AliasedTableExpr = ast.AliasedTableExpr
Common type aliases for convenience.
type CreateIndexStmt ¶
type CreateIndexStmt = ast.CreateIndexStmt
Common type aliases for convenience.
type CreateTableStmt ¶
type CreateTableStmt = ast.CreateTableStmt
Common type aliases for convenience.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package ast defines the abstract syntax tree for SQL statements.
|
Package ast defines the abstract syntax tree for SQL statements. |
|
Package format provides SQL generation from AST nodes.
|
Package format provides SQL generation from AST nodes. |
|
Package lexer provides a lexical scanner for SQL.
|
Package lexer provides a lexical scanner for SQL. |
|
Package parser provides a recursive descent SQL parser.
|
Package parser provides a recursive descent SQL parser. |
|
Package token defines SQL token types and position tracking.
|
Package token defines SQL token types and position tracking. |
|
Package visitor provides AST traversal and rewriting utilities.
|
Package visitor provides AST traversal and rewriting utilities. |