rust-postgres/postgres/src/generic_client.rs

330 lines
8.7 KiB
Rust
Raw Normal View History

2023-07-07 03:29:28 +00:00
#![allow(missing_docs)]
2023-07-27 20:18:43 +00:00
use std::time::Duration;
use fallible_iterator::FallibleIterator;
use tokio_postgres::{Column, Notification};
2023-07-07 03:29:28 +00:00
use tokio_postgres::row::RowIndex;
use tokio_postgres::types::FromSql;
use crate::types::{BorrowToSql, ToSql, Type};
2020-01-31 21:59:37 +00:00
use crate::{
Client, CopyInWriter, CopyOutReader, Error, Row, RowIter, SimpleQueryMessage, Statement,
2023-07-28 22:09:51 +00:00
ToStatement, Transaction};
2020-01-31 21:59:37 +00:00
2023-07-07 03:29:28 +00:00
macro_rules! common {
($transaction:ty, $err:ty) => {
/// Like `Client::execute`.
2023-07-07 03:29:28 +00:00
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, $err>
where
T: ?Sized + ToStatement;
/// Like `Client::query`.
2023-07-07 03:29:28 +00:00
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Self::Row>, $err>
where
T: ?Sized + ToStatement;
2020-01-31 21:59:37 +00:00
/// Like `Client::query_one`.
2023-07-07 03:29:28 +00:00
fn query_one<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Self::Row, $err>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement;
/// Like `Client::query_opt`.
fn query_opt<T>(
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
2023-07-07 03:29:28 +00:00
) -> Result<Option<Self::Row>, $err>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement;
/// Like `Client::query_raw`.
2023-07-07 03:29:28 +00:00
fn query_raw<T, P, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, $err>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
P: BorrowToSql,
I: IntoIterator<Item = P>,
2020-01-31 21:59:37 +00:00
I::IntoIter: ExactSizeIterator;
/// Like `Client::prepare`.
2023-07-07 03:29:28 +00:00
fn prepare(&mut self, query: &str) -> Result<Statement, $err>;
2020-01-31 21:59:37 +00:00
/// Like `Client::prepare_typed`.
2023-07-07 03:29:28 +00:00
fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, $err>;
2020-01-31 21:59:37 +00:00
/// Like `Client::copy_in`.
2023-07-07 03:29:28 +00:00
fn copy_in<T>(&mut self, query: &T) -> Result<CopyInWriter<'_>, $err>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement;
/// Like `Client::copy_out`.
2023-07-07 03:29:28 +00:00
fn copy_out<T>(&mut self, query: &T) -> Result<CopyOutReader<'_>, $err>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement;
/// Like `Client::simple_query`.
2023-07-07 03:29:28 +00:00
fn simple_query(&mut self, query: &str) -> Result<Vec<SimpleQueryMessage>, $err>;
2020-01-31 21:59:37 +00:00
/// Like `Client::batch_execute`.
2023-07-07 03:29:28 +00:00
fn batch_execute(&mut self, query: &str) -> Result<(), $err>;
2020-01-31 21:59:37 +00:00
/// Like `Client::transaction`.
2023-07-07 03:29:28 +00:00
fn transaction(&mut self) -> Result<$transaction, $err>;
};
}
2020-01-31 21:59:37 +00:00
2023-07-07 03:29:28 +00:00
pub trait GenericRow {
type Error: core::fmt::Debug;
fn columns(&self) -> &[Column];
fn len(&self) -> usize;
fn get<'a, I, T>(&'a self, idx: I) -> T
where
I: RowIndex + core::fmt::Display,
T: FromSql<'a>;
fn try_get<'a, I, T>(&'a self, idx: I) -> Result<T, Self::Error>
where
I: RowIndex + core::fmt::Display,
T: FromSql<'a>;
}
pub trait GenericTransaction<'a> where Self: Sized {
type Error: core::fmt::Debug;
type NestedTransaction<'b> where Self: 'b;
type Row: GenericRow<Error = Self::Error>;
fn commit(self) -> Result<(), Self::Error>;
fn rollback(self) -> Result<(), Self::Error>;
common!(Self::NestedTransaction<'_>, Self::Error);
}
/// A trait allowing abstraction over connections and transactions.
pub trait GenericClient {
type Error: core::fmt::Debug;
type Transaction<'a>: GenericTransaction<'a> where Self: 'a;
type Row: GenericRow<Error = Self::Error>;
common!(Self::Transaction<'_>, Self::Error);
2023-07-24 04:05:31 +00:00
fn check_for_notify(&mut self) -> Result<Option<Notification>, Self::Error>;
2023-07-07 03:29:28 +00:00
}
2020-01-31 21:59:37 +00:00
impl GenericClient for Client {
2023-07-07 03:29:28 +00:00
type Error = Error;
type Transaction<'a> = Transaction<'a>;
type Row = Row;
2020-01-31 21:59:37 +00:00
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
{
self.execute(query, params)
}
2023-07-07 03:29:28 +00:00
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Self::Row>, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
{
self.query(query, params)
}
2023-07-07 03:29:28 +00:00
fn query_one<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Self::Row, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
{
self.query_one(query, params)
}
fn query_opt<T>(
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
2023-07-07 03:29:28 +00:00
) -> Result<Option<Self::Row>, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
{
self.query_opt(query, params)
}
fn query_raw<T, P, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
P: BorrowToSql,
I: IntoIterator<Item = P>,
2020-01-31 21:59:37 +00:00
I::IntoIter: ExactSizeIterator,
{
self.query_raw(query, params)
}
fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query)
}
fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
self.prepare_typed(query, types)
}
fn copy_in<T>(&mut self, query: &T) -> Result<CopyInWriter<'_>, Error>
where
T: ?Sized + ToStatement,
{
self.copy_in(query)
}
fn copy_out<T>(&mut self, query: &T) -> Result<CopyOutReader<'_>, Error>
where
T: ?Sized + ToStatement,
{
self.copy_out(query)
}
fn simple_query(&mut self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
self.simple_query(query)
}
fn batch_execute(&mut self, query: &str) -> Result<(), Error> {
self.batch_execute(query)
}
2023-07-07 03:29:28 +00:00
fn transaction(&mut self) -> Result<Self::Transaction<'_>, Error> {
2020-01-31 21:59:37 +00:00
self.transaction()
}
2023-07-24 04:05:31 +00:00
fn check_for_notify(&mut self) -> Result<Option<Notification>, Self::Error> {
let mut n = self.notifications();
2023-07-27 20:18:43 +00:00
let mut n = n.timeout_iter(Duration::from_millis(500));
n.next()
}
2020-01-31 21:59:37 +00:00
}
2023-07-07 03:29:28 +00:00
macro_rules! transaction_common {
($transaction:ty) => {
2020-01-31 21:59:37 +00:00
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
{
self.execute(query, params)
}
2023-07-07 03:29:28 +00:00
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Self::Row>, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
{
self.query(query, params)
}
2023-07-07 03:29:28 +00:00
fn query_one<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Self::Row, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
{
self.query_one(query, params)
}
fn query_opt<T>(
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
2023-07-07 03:29:28 +00:00
) -> Result<Option<Self::Row>, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
{
self.query_opt(query, params)
}
fn query_raw<T, P, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
2020-01-31 21:59:37 +00:00
where
T: ?Sized + ToStatement,
P: BorrowToSql,
I: IntoIterator<Item = P>,
2020-01-31 21:59:37 +00:00
I::IntoIter: ExactSizeIterator,
{
self.query_raw(query, params)
}
fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query)
}
fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
self.prepare_typed(query, types)
}
fn copy_in<T>(&mut self, query: &T) -> Result<CopyInWriter<'_>, Error>
where
T: ?Sized + ToStatement,
{
self.copy_in(query)
}
fn copy_out<T>(&mut self, query: &T) -> Result<CopyOutReader<'_>, Error>
where
T: ?Sized + ToStatement,
{
self.copy_out(query)
}
fn simple_query(&mut self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
self.simple_query(query)
}
fn batch_execute(&mut self, query: &str) -> Result<(), Error> {
self.batch_execute(query)
}
2023-07-07 03:29:28 +00:00
fn transaction(&mut self) -> Result<$transaction, Error> {
2020-01-31 21:59:37 +00:00
self.transaction()
}
2023-07-07 03:29:28 +00:00
};
}
impl<'a> GenericTransaction<'a> for Transaction<'a> {
type Error = Error;
type NestedTransaction<'b> = Transaction<'b> where Self: 'b;
type Row = Row;
fn commit(self) -> Result<(), Error> {Transaction::commit(self)}
fn rollback(self) -> Result<(), Error> {Transaction::rollback(self)}
transaction_common!(Self::NestedTransaction<'_>);
}
impl GenericClient for Transaction<'_> {
type Error = Error;
type Transaction<'a> = Transaction<'a> where Self: 'a;
type Row = Row;
transaction_common!(Self::Transaction<'_>);
2023-07-24 04:05:31 +00:00
fn check_for_notify(&mut self) -> Result<Option<Notification>, Self::Error> {
Ok(None)
}
2023-07-07 03:29:28 +00:00
}
impl GenericRow for Row {
type Error = Error;
fn columns(&self) -> &[Column] {
self.columns()
}
fn len(&self) -> usize {
self.len()
}
fn get<'a, I, T>(&'a self, idx: I) -> T
where
I: RowIndex + core::fmt::Display,
T: FromSql<'a> {
self.get(idx)
}
fn try_get<'a, I, T>(&'a self, idx: I) -> Result<T, Error>
where
I: RowIndex + core::fmt::Display,
T: FromSql<'a> {
self.try_get(idx)
}
2020-01-31 21:59:37 +00:00
}