Helper macros and traits built around tokio-postgres to define queries with human readable parameters and return values.
Example
# use Client;
# use ;
#
# async
Queries
The preferred way of constructing a new Query
is through the query!
macro. It uses a
syntax similar to the format!(...)
family of macros from the standard library. The first
parameter is the SQL query and is always given as a string literal (this might be relaxed in the
future). This string literal may contain parameter bindings on the form $ident
where ident
is any valid Rust identifier ($abc
, $value_123
, etc.).
# use query;
let age = 42;
let insert_person = query!;
During compilation the query is converted into the format expected by PostgreSQL: parameter bindings are converted to using numbers ($1, $2, etc.) and the actual parameter values are put into a 1-indexed array. The code snippet above would be expanded into the following:
# use *;
let age = 42;
let insert_person = Query ;
Data Extraction
In addition to helping you define new queries this crate provides the FromSqlRow
trait which
makes it easy to extract typed values from the resulting rows. The easiest way to implement this
trait for new struct
s is to use the included derive(FromSqlRow)
macro.
- If used on a tuple struct, values will be extracted from the corresponding columns based on their position in the tuple.
- If used on a stuct with named fields, values will be extracted from the column with the same name as the field.
# use *;
;
;
Caching queries
From time to time you probably want to execute the same query multiple times, but with different
parameters. In times like these we can decrease the load on the database by preparing our
queries before executing them. By wrapping a client in a Caching
struct this behaviour is
automatically provided for all queries that originate from this crate:
# use Client;
# use ;
#
# async