37 releases (breaking)

0.28.0 Oct 22, 2025
0.27.0 Sep 12, 2025
0.24.0 Dec 19, 2024
0.22.1 Nov 11, 2024
0.4.0 Mar 30, 2022

#567 in Database interfaces

Download history 86/week @ 2025-10-09 202/week @ 2025-10-16 182/week @ 2025-10-23 45/week @ 2025-10-30 30/week @ 2025-11-06 24/week @ 2025-11-13 46/week @ 2025-11-20 33/week @ 2025-11-27 31/week @ 2025-12-04 48/week @ 2025-12-11 10/week @ 2025-12-18 33/week @ 2026-01-01 5/week @ 2026-01-08 50/week @ 2026-01-15 77/week @ 2026-01-22

165 downloads per month
Used in 4 crates (2 directly)

Apache-2.0

365KB
9K SLoC

sql-parse

crates.io crates.io License actions-badge

Parse SQL into an AST

This crate provides an lexer and parser that can parse SQL into an Abstract Syntax Tree (AST). Currently primarily focused on MariaDB/Mysql.

Example code:

use sql_parse::{SQLDialect, SQLArguments, ParseOptions, parse_statement, Issues};

let options = ParseOptions::new()
    .dialect(SQLDialect::MariaDB)
    .arguments(SQLArguments::QuestionMark)
    .warn_unquoted_identifiers(true);


let sql = "SELECT `monkey`,
           FROM `t1` LEFT JOIN `t2` ON `t2`.`id` = `t1.two`
           WHERE `t1`.`id` = ?";

let mut issues = Issues::new(sql);
let ast = parse_statement(sql, &mut issues, &options);

println!("Issues: {}", issues);
println!("AST: {:#?}", ast);

Features

  • Good error recovery: The parser implements reasonable error recovery and will continue parsing long expressions if an error is found within.
  • Code span annotations: All AST notes implements Spanned that yields a byte span within the code. This means that errors and warnings generated from the parsing can be precented to the user in a nice ways. Also users of the AST can generate more issues that can also similarly be presented nicely.
  • No dependencies: We use no-std with alloc, and has no other dependencies
  • No unsafe code: We use #![forbid(unsafe_code)] to guarantee no unsafe code.
  • Fast parsing: The parser is a hand written recursive decent parser. To speed up parser expressions are parsed using a O(1) shift reduce mechanism.

No runtime deps