Crates.io | yesqlr |
lib.rs | yesqlr |
version | 0.1.0 |
source | src |
created_at | 2024-10-06 12:19:26.830587 |
updated_at | 2024-10-06 12:19:26.830587 |
description | Parse SQL files with multiple named queries into a map. Port of goyesql. |
homepage | https://github.com/knadh/yesqlr |
repository | https://github.com/knadh/yesqlr |
max_upload_size | |
id | 1399069 |
size | 16,289 |
yesqlr is a Rust port of the goyesql Go library.
It allows multiple SQL queries to be defined in an .sql
file, each separate by a specially formatted --name: $name
accompanying every query, which the library then parses to a HashMap<$name, Query{}>.
In addition, it also supports attaching arbitrary --$key: $value tags with every query
This allows better organization and handling of SQL code in Rust projects.
Create a .sql
file with multiple queries, each preceded by a -- name: query_name
tag. Additional tags can be added as needed.
-- name: get_user
-- raw: true
SELECT * FROM users WHERE id = $1;
-- name: create_user
INSERT INTO users (name, email) VALUES ($1, $2);
Use the parse_file()
function to read and parse the .sql
file.
use yesqlr::parse_file;
fn main() -> Result<(), yesqlr::ParseError> {
let queries = parse_file("test.sql").expect("error parsing file");
let q = &queries["simple"].query;
println!("the query is: {}", q);
Ok(())
}
Alternatively, parse SQL queries from a byte stream using the parse()
function.
use yesqlr::parse;
fn main() -> Result<(), yesqlr::ParseError> {
let raq = b"-- name: list_users\nSELECT * FROM users;";
let queries = parse(&raq[..])?;
let list_users_query = &queries["list_users"].query;
println!("user query is: {}", list_users_query);
Ok(())
}
This project is licensed under the MIT License. See the LICENSE file for details.