Write SQL queries in a simple and composable way.
The main goal is to find the best balance between write idiomatic SQL queries and manage cenarios of complex query composition mixed with conditional clauses.
Quick Start
use SelectBuilder;
let mut select = new
.select
.from
.where_clause;
let is_admin = true;
if is_admin
let query = select.as_string;
println!;
Output
SELECT id, login FROM users WHERE login = $1 AND is_admin = true
In simple terms this library will not try to understand what you are writing inside the arguments, this is good
because it's removes a lot complexity and verbosity that other libraries needs to generate a SQL query,
in contrast debugging tends to be more difficult and silly error can araise.
The lib has .debug()
method with a nice output to minimize the effort to debug a complex query.
How it's works
Consecutive calls to the same clause will accumulates values respecting the order of the calls, the two select produce the same SQL query
use SelectBuilder;
let select = new
.select;
let select = new
.select
.select;
Methods like limit
and offset
will override the previous value, the two select produce the same SQL query
use SelectBuilder;
let select = new
.limit;
let select = new
.limit
.limit;
The library ignores the order between clauses so the two selects produce the same SQL query
use SelectBuilder;
let select = new
.select
.from
.where_clause;
let select = new
.from
.where_clause
.select;
You can conditionally add a clause mutating the select
use SelectBuilder;
let mut select = new
.select
.from
.where_clause;
let shouldIncludesAddress = true;
if shouldIncludesAddress
Composition
Composition is very welcome to write complex queries, this feature makes the library shine
use SelectBuilder;
let query = Some
.map
.map
.map
.map
.unwrap;
println!;
Output (indented for redability)
SELECT u.id, u.name as user_name, u.login, a.name as address_name, o.name as product_name
FROM users u
INNER JOIN address a ON a.user_login = u.login
INNER JOIN orders o ON o.user_login = u.login
WHERE u.login = $1 AND o.id = $2
Raw queries
You can use the raw method to accomplish some edge cases that are hard to rewrite into the SelectBuilder syntax.
The select.raw()
method will put any SQL you define at top of the output
use SelectBuilder;
let raw_query = "\
select u.id as user_id, addr.* \
from users u \
inner join address addr on u.login = addr.owner_login\
";
let select = new
.raw
.where_clause;
To a more precisely use case your can use the select.raw_before()
and select.raw_after()
use ;
let raw_query = "\
from users u \
inner join address addr on u.login = addr.owner_login\
";
let select = new
.select
.raw_before
.where_clause;
use ;
let raw_query = "\
from users u \
inner join address addr on u.login = addr.owner_login\
";
let select = new
.select
.raw_after
.where_clause;
Crate features
SQL Query Builder comes with the following optional features:
postgresql
enable Postgres syntax
You can enable features like
# Cargo.toml
= { = "0.x.x", = ["postgresql"] }