Expand description
§bsql — Safe SQL for Rust
If it compiles, the SQL is correct.
bsql validates every SQL query against a real database at compile time. No runtime, no async — just safe, fast, synchronous SQL.
§Quick Start
[dependencies]
bsql = "0.17"ⓘ
use bsql::Pool;
fn main() -> Result<(), bsql::BsqlError> {
let pool = Pool::connect("postgres://user:pass@localhost/mydb")?;
let id = 1i32;
let users = bsql::query!(
"SELECT id, login, active FROM users WHERE id = $id: i32"
).fetch(&pool)?;
let user = &users[0];
// user.id: i32, user.login: String, user.active: bool
println!("{}: active={}", user.login, user.active);
Ok(())
}§Two methods — that’s it
| Method | Returns | Use |
|---|---|---|
.fetch(&pool) | Vec<Row> | SELECT queries |
.run(&pool) | u64 | INSERT, UPDATE, DELETE |
Also: fetch_one, fetch_optional, fetch_stream, for_each, defer (for transactions).
§Escape hatch
For rare cases requiring dynamic SQL (dynamic table names, pivots, DDL):
ⓘ
let rows = pool.raw_query("SELECT * FROM pg_tables LIMIT 5")?;
pool.raw_execute("CREATE INDEX CONCURRENTLY idx ON users (email)")?;raw_query / raw_execute bypass compile-time validation entirely.
Use query! for everything else.
Modules§
- error
- Error types for bsql.
Macros§
- query
- Validate a SQL query against PostgreSQL at compile time and generate typed Rust code for executing it.
Structs§
- Listener
- A dedicated LISTEN/NOTIFY connection to PostgreSQL.
- Notification
- A notification received from PostgreSQL via LISTEN/NOTIFY.
- Pool
- A PostgreSQL connection pool.
- Pool
Builder - Builder for configuring a connection pool.
- Pool
Status - Snapshot of pool utilization.
- Query
Stream - A stream of rows backed by true PG-level chunked fetching.
- RawRow
- A row of text values from a raw (unvalidated) SQL query.
- Transaction
- A database transaction.
Enums§
- Bsql
Error - The error type for all bsql operations.
- Isolation
Level - Transaction isolation levels supported by PostgreSQL.
Type Aliases§
- Bsql
Result - Convenience alias used throughout bsql.