-
Notifications
You must be signed in to change notification settings - Fork 185
Open
Labels
A-postgresArea: PostgreSQL support / deadpool-postgresArea: PostgreSQL support / deadpool-postgresenhancementNew feature or requestNew feature or request
Description
I was using deadpool-postgres in my project and i started to feel disturbed by the verbosity of each request using a prepared statement.
I tried to implement a trait to minimize the code bloat looking like this :
#[async_trait]
pub trait GenericClientCached {
async fn query_cached(
&self,
query: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>, Error>;
...
}
#[async_trait]
impl<T: GenericClient> GenericClientCached for T {
async fn query_cached(
&self,
query: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>, Error> {
let stmt = self.prepare_cached(query).await?;
self.query(&stmt, params).await
}
// ... (same logic for all other functions using a prepared statement)
}Usage example:
client.execute_cached("UPDATE sessions SET valid = false WHERE id = $1",&[&0i64]).await.unwrap();Instead of:
let stmt = client.prepared_cached("UPDATE sessions SET valid = false WHERE id = $1").await.unwrap();
client.execute(stmt, &[&0i64]).await.unwrap();It's a small gain, but as it's a frequently repeated piece of code in a project's code base, I find it useful to minimize the code required for a query. Plus, it's easier to see which arguments match the SQL query.
What do you think? Is there any reason why it doesn't already exist?
Metadata
Metadata
Assignees
Labels
A-postgresArea: PostgreSQL support / deadpool-postgresArea: PostgreSQL support / deadpool-postgresenhancementNew feature or requestNew feature or request