Crates.io | sql_select_parser |
lib.rs | sql_select_parser |
version | 0.1.4 |
source | src |
created_at | 2024-11-12 21:55:30.717505 |
updated_at | 2024-11-16 06:55:34.398349 |
description | The SQL Parser CLI is a command-line tool designed to parse and analyze SQL SELECT queries. |
homepage | |
repository | https://github.com/heliochromic/sql_select_parser |
max_upload_size | |
id | 1445629 |
size | 42,477 |
Link to creates.io
Link to docs.rs
The SQL Parser CLI is a command-line tool designed to parse and analyze SQL SELECT queries. Built with Rust, it leverages the pest parser generator to interpret SQL syntax and provides a structured Abstract Syntax Tree (AST) representation of the parsed queries. This tool is ideal for developers and database administrators who need to validate, analyze, or transform SQL queries programmatically.
SelectQuery:
SelectItem:
Table:
Condition:
Value:
Clone repository and make sure that all dependecies installed properly
git clone https://github.com/heliochromic/sql_select_parser
cd sql_parser_cli
anyhow = "1.0.92"
clap = { version = "4.5.20", features = ["derive"] }
pest = "2.7.14"
pest_derive = "2.7.14"
thiserror = "1.0.66"
Run demo
make example
Input
select id, name, email from users where active = true;
Output
SelectQuery {
columns: [
Column(
"id",
),
Column(
"name",
),
Column(
"email",
),
],
table: Simple(
"users",
),
where_clause: Some(
Condition {
left: "active",
operator: "=",
right: Boolean(
true,
),
},
),
}
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
SELECT = { "select" }
FROM = { "from" }
WHERE = { "where" }
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
number = @{ "-"? ~ ASCII_DIGIT+ }
string = @{ "'" ~ (!"'" ~ ANY)* ~ "'" }
boolean = @{ "true" | "false" }
operator = @{ "!=" | ">=" | "<=" | "<" | ">" | "=" }
function_name = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
table = { identifier | "(" ~ select_query ~ ")" }
select_query = { SELECT ~ select_list ~ FROM ~ table ~ where_clause? }
select_list = { select_item ~ ("," ~ select_item)* }
star = { "*" }
select_item = { function_call | identifier | star }
function_call = { function_name ~ "(" ~ ("*" | select_item ~ ("," ~ select_item)*)? ~ ")" }
where_clause = { WHERE ~ condition }
condition = { identifier ~ operator ~ value }
value = { string | number | boolean }