IMPORTANT - Still experimental and extremely incomplete. All 0.0.x version will be experimental and probably break APIs in each release
sqlb is intended to be a simple and progressive SQLBuilder for Rust, independent from database SQL executor.
sqlb
api is about creating a parameterized sql (builder.sql() -> String
) and a list of values (builder.vals() -> Vec<val>
), integrating with any other database connectivity libraries should be trivial. NOTE: SQL Builders are typically not to be used directly by application business logic, but rather to be wrapped in some sort of Application Data Access Layer (DAOs, DM, patterns).
Scope for first 0.1.x releases:
Get_Fields
needs to be written by hand. As the APIs/Models mature, macros will be implemented to avoid boilerplate code.Val
is extremely rudimentary, and more thought is needed to find the right model there. Feedback welcome.Insert:
rust
let sb = sqlb::insert("todo").data(patch_data.fields());
let sb = sb.returning(&["id", "title"]);
let (_id, title) = sqlb::sqlx_exec::fetch_as_one::<(i64, String), _>(&db_pool, &sb).await?;
Select:
rust
let sb = sqlb::select("todo").columns(&["id", "title"]).order_by("!id");
let todos: Vec<Todo> = sqlb::sqlx_exec::fetch_as_all(&db_pool, &sb).await?;
assert_eq!(1, todos.len());
The data setup:
```rust
pub struct Todo { pub id: i64, pub title: String, }
pub struct TodoPatch {
pub title: Option
impl GetFields for TodoPatch {
fn fields(&self) -> Vec
Start a postgressql
```sh
docker run --rm --name pg -p 5432:5432 -e POSTGRES_PASSWORD=welcome postgres:13
docker exec -it -u postgres pg psql
cargo test
```