Skip to main content

GenericClient

Trait GenericClient 

Source
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§

Source

async fn query( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>>

Execute a query that returns rows.

Source

async fn query_one( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Row>

Execute a query that returns a single row.

Source

async fn query_opt( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Option<Row>>

Execute a query that returns an optional single row.

Source

async fn execute( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<u64>

Execute a non-SELECT query, returning rows affected.

Source

async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>

Execute a simple query (text protocol, no parameters).

Source

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).

Source

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.

Source

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.

Source

async fn execute_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<u64>

Execute a typed non-SELECT, returning rows affected.

Source

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
where P: AsPool + Sync,

Source§

async fn query( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>>

Source§

async fn query_one( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Row>

Source§

async fn query_opt( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Option<Row>>

Source§

async fn execute( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<u64>

Source§

async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>

Source§

async fn query_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Vec<Row>>

Source§

async fn query_typed_one( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Row>

Source§

async fn query_typed_opt( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Option<Row>>

Source§

async fn execute_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<u64>

Source§

async fn execute_pipeline( &mut self, batch: PipelineBatch, ) -> Result<Vec<QueryResult>>

Implementors§

Source§

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.

Source§

impl GenericClient for Connection

Source§

impl GenericClient for PooledConnection