On this page
Package sqlparse wraps wasilibs/go-pgquery, a WASM-based PostgreSQL parser requiring no CGo, for SQL statement splitting and expression deparsing.
#internal/sqlparse
#internal/sqlparse
Package sqlparse wraps wasilibs/go-pgquery, a WASM-based PostgreSQL parser requiring no CGo, for SQL statement splitting and expression deparsing.
The WASM implementation requires no C compiler and no native libpg_query build.
SplitStatements splits SQL text into individual statements using the real PostgreSQL parser, correctly handling dollar-quoting ($$ blocks), string literals containing semicolons, PL/pgSQL function bodies, and other syntax that naive semicolon splitting breaks on. It replaces the earlier ";\n" split approach.
DeparseExpr converts pg_query AST nodes back to SQL text, used for expression normalization.
The first call incurs approximately 600ms of latency for WASM runtime initialization; subsequent calls are fast.
#NormalizeExpr
func NormalizeExpr(expr string) stringNormalizeExpr returns the canonical (≈_syn-normal) form of a SQL scalar expression. On parse/deparse failure it returns the trimmed input verbatim.
#ExprEqual
func ExprEqual(a, b string) boolExprEqual reports whether two SQL expressions are ≈_syn-equal: N(a) = N(b). This is the kernel of N and the single equality every comparison engine uses.
#SplitStatements
func SplitStatements(sql string) ([]string, error)SplitStatements splits a SQL string into individual statements using pg_query's parser. This correctly handles dollar-quoted strings, string literals containing semicolons, and other PostgreSQL syntax.
#ExtractTableRefs
func ExtractTableRefs(sql string) ([]string, error)ExtractTableRefs parses the given SQL and returns a sorted, deduplicated list of table names referenced in the statement. Schema-qualified references are returned as "schema.table". Returns nil, nil for empty input. Returns an error if the SQL cannot be parsed (e.g., PL/pgSQL bodies).
#DeparseExpr
func DeparseExpr(node *pg.Node) (string, error)DeparseExpr converts a pg_query AST expression node back to its SQL string representation. It works by wrapping the node in a synthetic SELECT statement and deparsing the full statement, then stripping the "SELECT " prefix.