bsql — Safe SQL for Rust
If it compiles, the SQL is correct.
bsql is a proc-macro library that validates every SQL query against a real
PostgreSQL instance at compile time. There is no query() function. There is
no escape hatch. There is query! — validated, typed, checked. If the binary
is produced, every SQL query in it is correct.
Quick Start
[]
= "0.6"
= { = "1", = ["rt-multi-thread", "macros"] }
Set the database URL for compile-time validation:
Then:
use ;
async
No escape hatch
There is no bsql::query() function. There is no raw_sql(). There is no
way to execute unchecked SQL through bsql. If you need unchecked SQL, use
tokio-postgres directly. bsql will not become the thing it replaces.
Execution methods
| Method | Returns | Error if |
|---|---|---|
.fetch_one(&pool) |
T |
0 rows, or 2+ rows |
.fetch_all(&pool) |
Vec<T> |
never (empty = empty vec) |
.fetch_optional(&pool) |
Option<T> |
2+ rows |
.fetch_stream(&pool) |
impl Stream<Item = Result<T>> |
never |
.execute(&pool) |
u64 (affected rows) |
never |