postgres-named-parameters
postgres-named-parameters
is a lightweight macro wrapper around the postgres
crate which gives you the ergonomics of named parameters in your raw SQL
queries. Under the hood, your named parameters are transformed into numbered
parameters at compile time.
Usage example
use FromRow;
use Query;
// Use the postgres-from-row crate to decode each row returned
// from a query into a struct.
// Use a struct to define a query. The #[derive(Query)] will implement
// query functions that handle passing in the parameters, and the named
// parameters are converted to numbered parameters ($1, $2,...) at compile
// time.
For a more thorough example (including bulk queries), see the example project folder in the GitHub repository.
Features
- Supports transactions
- SQL transformation to numbered parameters happens at compile time
- Mis-typing a named parameter (e.g.
@naame
instead of@name
) produces a compile-time error
Attribution & Related Libraries
This crate was inspired by the following libraries:
- couch/aykroyd provided the idea of representing queries as structs
- nolanderc/rust-postgres-query and solidsnack/rust-postgres-named-parameters were used as reference when creating the SQL parser
- 3noch/postgres-simple-interpolate is the Haskell library that motivated creating an ergonomic way of dealing with SQL parameters in Rust