A Rust library that uses the sqlparser
crate to parse SQL statements and convert them to JSON representation.
- Parse SQL statements using various SQL dialects
- Convert parsed Abstract Syntax Tree (AST) to JSON
- Support for 10+ SQL dialects including PostgreSQL, MySQL, SQLite, BigQuery, and more
- Comprehensive error handling
use sql_parser_wasm::parse_sql;
fn main() {
let sql = "SELECT id, name FROM users WHERE active = true";
let dialect = "postgresql";
match parse_sql(dialect, sql) {
Ok(json_ast) => {
println!("Parsed AST: {}", json_ast);
}
Err(error) => {
eprintln!("Parse error: {}", error);
}
}
}
generic
- Generic SQL dialectpostgresql
/postgres
- PostgreSQLmysql
- MySQLsqlite
- SQLitemssql
/sqlserver
- Microsoft SQL Serversnowflake
- Snowflakeredshift
- Amazon Redshiftbigquery
- Google BigQueryclickhouse
- ClickHousehive
- Apache Hive
Parses a SQL statement using the specified dialect and returns the AST as a pretty-printed JSON string.
Parameters:
dialect
- The SQL dialect to use for parsingsql
- The SQL statement to parse
Returns:
Ok(String)
- JSON representation of the parsed ASTErr(String)
- Error message if parsing fails
let result = parse_sql("postgresql", "SELECT * FROM users");
// Returns JSON representation of the SELECT statement AST
let sql = r#"
SELECT u.name, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.active = true
GROUP BY u.name
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC
LIMIT 10
"#;
let result = parse_sql("mysql", sql);
// Returns detailed JSON AST including joins, aggregations, etc.
// Invalid SQL
let result = parse_sql("postgresql", "SELEC * FRO users");
assert!(result.is_err());
// Invalid dialect
let result = parse_sql("invalid", "SELECT * FROM users");
assert!(result.is_err());
# Build the library
cargo build
# Run tests
cargo test
# Build with optimizations
cargo build --release
To build for WebAssembly:
# Install wasm-pack if not already installed
cargo install wasm-pack
# Build for web
wasm-pack build --target web --features wasm
# Build for Node.js
wasm-pack build --target nodejs --features wasm
Run the basic usage example:
cargo run --example basic_usage
This library also supports WebAssembly for use in web browsers and Node.js environments.
# Install wasm-pack if not already installed
cargo install wasm-pack
# Build for web
wasm-pack build --target web --features wasm --out-dir pkg
# Or use the provided build scripts
./build-wasm.sh # Linux/macOS
build-wasm.bat # Windows
The WASM build provides three main functions:
Returns the parsed AST as a JavaScript object:
import init, { parse_sql_wasm } from './pkg/sql_parser_wasm.js';
await init();
try {
const ast = parse_sql_wasm("postgresql", "SELECT * FROM users");
console.log("Parsed AST:", ast);
} catch (error) {
console.error("Parse error:", error);
}
Returns the parsed AST as a JSON string:
import { parse_sql_json } from './pkg/sql_parser_wasm.js';
try {
const jsonString = parse_sql_json("postgresql", "SELECT * FROM users");
const ast = JSON.parse(jsonString);
console.log("Parsed AST:", ast);
} catch (error) {
console.error("Parse error:", error);
}
Returns an array of supported dialect names:
import { get_supported_dialects } from './pkg/sql_parser_wasm.js';
const dialects = get_supported_dialects();
console.log("Supported dialects:", dialects);
// Output: ["generic", "postgresql", "mysql", "sqlite", ...]
See examples/wasm_example.html
for a complete web-based example that demonstrates all WASM functionality.
To run the example:
- Build the WASM package:
wasm-pack build --target web --features wasm
- Serve the project directory with an HTTP server
- Open
examples/wasm_example.html
in your browser
# Using Python
python -m http.server 8000
# Using Node.js
npx serve .
cargo test
Generate and view documentation:
cargo doc --open
cargo clippy
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Full SQL grammar support
- Query optimization suggestions
- SQL formatting and pretty-printing
- Support for different SQL dialects (PostgreSQL, MySQL, SQLite, etc.)
- Advanced error recovery and suggestions
- TypeScript type definitions for WASM bindings