pub trait GenericClient {
// Required methods
async fn query(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>>;
async fn query_one(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row>;
async fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>>;
async fn execute(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<u64>;
async fn simple_query(
&mut self,
sql: &str,
) -> Result<Vec<SimpleQueryMessage>>;
async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Vec<Row>>;
async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Row>;
async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Option<Row>>;
async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<u64>;
async fn execute_pipeline(
&mut self,
batch: PipelineBatch,
) -> Result<Vec<QueryResult>>;
}Expand description
A trait for types that can execute PostgreSQL queries.
Allows writing code that is generic over Connection
and PooledConnection.
use sentinel_driver::{GenericClient, Result, Row};
async fn get_user_name(client: &mut impl GenericClient, id: i32) -> Result<String> {
let row = client.query_one("SELECT name FROM users WHERE id = $1", &[&id]).await?;
row.try_get(0)
}Required Methods§
Sourceasync fn query(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>>
async fn query( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>>
Execute a query that returns rows.
Sourceasync fn query_one(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row>
async fn query_one( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Row>
Execute a query that returns a single row.
Sourceasync fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>>
async fn query_opt( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Option<Row>>
Execute a query that returns an optional single row.
Sourceasync fn execute(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<u64>
async fn execute( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<u64>
Execute a non-SELECT query, returning rows affected.
Sourceasync fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>
async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>
Execute a simple query (text protocol, no parameters).
Sourceasync fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Vec<Row>>
async fn query_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Vec<Row>>
Execute a typed query (skips the prepare round-trip).
Sourceasync fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Row>
async fn query_typed_one( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Row>
Execute a typed query that returns a single row.
Sourceasync fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Option<Row>>
async fn query_typed_opt( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Option<Row>>
Execute a typed query that returns an optional row.
Sourceasync fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<u64>
async fn execute_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<u64>
Execute a typed non-SELECT, returning rows affected.
Sourceasync fn execute_pipeline(
&mut self,
batch: PipelineBatch,
) -> Result<Vec<QueryResult>>
async fn execute_pipeline( &mut self, batch: PipelineBatch, ) -> Result<Vec<QueryResult>>
Run a pre-built PipelineBatch in one round-trip.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<P> GenericClient for &P
impl<P> GenericClient for &P
async fn query( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>>
async fn query_one( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Row>
async fn query_opt( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Option<Row>>
async fn execute( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<u64>
async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>
async fn query_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Vec<Row>>
async fn query_typed_one( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Row>
async fn query_typed_opt( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Option<Row>>
async fn execute_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<u64>
async fn execute_pipeline( &mut self, batch: PipelineBatch, ) -> Result<Vec<QueryResult>>
Implementors§
impl GenericClient for &Pool
GenericClient for &Pool — acquire one connection, run the query,
return it to the pool on drop.
This is a direct impl rather than going through AsPool::with_conn
because the 'a-fixed closure signature in AsPool cannot be satisfied
for a locally-acquired PooledConnection in safe Rust (the borrow
checker cannot prove the local outlives 'a even though the async block
that owns it is itself 'a-bounded, and unsafe_code is forbidden
workspace-wide). The direct impl avoids that lifetime knot entirely.