Skip to main content

sentinel_driver/
generic_client.rs

1use crate::error::Result;
2use crate::row::{Row, SimpleQueryMessage};
3use crate::types::ToSql;
4use crate::Connection;
5
6/// A trait for types that can execute PostgreSQL queries.
7///
8/// Allows writing code that is generic over [`Connection`](crate::Connection)
9/// and [`PooledConnection`](crate::PooledConnection).
10///
11/// ```rust,no_run
12/// use sentinel_driver::{GenericClient, Result, Row};
13///
14/// async fn get_user_name(client: &mut impl GenericClient, id: i32) -> Result<String> {
15///     let row = client.query_one("SELECT name FROM users WHERE id = $1", &[&id]).await?;
16///     row.try_get(0)
17/// }
18/// ```
19#[allow(async_fn_in_trait)]
20pub trait GenericClient {
21    /// Execute a query that returns rows.
22    async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>>;
23
24    /// Execute a query that returns a single row.
25    async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row>;
26
27    /// Execute a query that returns an optional single row.
28    async fn query_opt(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)])
29        -> Result<Option<Row>>;
30
31    /// Execute a non-SELECT query, returning rows affected.
32    async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64>;
33
34    /// Execute a simple query (text protocol, no parameters).
35    async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>;
36}
37
38impl GenericClient for Connection {
39    async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>> {
40        Connection::query(self, sql, params).await
41    }
42
43    async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row> {
44        Connection::query_one(self, sql, params).await
45    }
46
47    async fn query_opt(
48        &mut self,
49        sql: &str,
50        params: &[&(dyn ToSql + Sync)],
51    ) -> Result<Option<Row>> {
52        Connection::query_opt(self, sql, params).await
53    }
54
55    async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64> {
56        Connection::execute(self, sql, params).await
57    }
58
59    async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>> {
60        Connection::simple_query(self, sql).await
61    }
62}
63
64impl GenericClient for crate::PooledConnection {
65    async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>> {
66        Connection::query(self, sql, params).await
67    }
68
69    async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row> {
70        Connection::query_one(self, sql, params).await
71    }
72
73    async fn query_opt(
74        &mut self,
75        sql: &str,
76        params: &[&(dyn ToSql + Sync)],
77    ) -> Result<Option<Row>> {
78        Connection::query_opt(self, sql, params).await
79    }
80
81    async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64> {
82        Connection::execute(self, sql, params).await
83    }
84
85    async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>> {
86        Connection::simple_query(self, sql).await
87    }
88}