23 releases (15 breaking)

0.16.0 Oct 2, 2023
0.15.0 Jun 13, 2023
0.13.0 Mar 22, 2023
0.8.0 Jun 17, 2022
0.4.0 Mar 30, 2022

#580 in Database interfaces

Download history 41/week @ 2023-08-19 50/week @ 2023-08-26 35/week @ 2023-09-02 117/week @ 2023-09-09 93/week @ 2023-09-16 33/week @ 2023-09-23 57/week @ 2023-09-30 56/week @ 2023-10-07 44/week @ 2023-10-14 53/week @ 2023-10-21 56/week @ 2023-10-28 31/week @ 2023-11-04 31/week @ 2023-11-11 51/week @ 2023-11-18 80/week @ 2023-11-25 72/week @ 2023-12-02

234 downloads per month
Used in 3 crates (via sql-type)

Apache-2.0

305KB
8K 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};

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

let mut issues = Vec::new();

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

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