330 lines
8.7 KiB
Rust
330 lines
8.7 KiB
Rust
#![allow(missing_docs)]
|
|
|
|
use std::time::Duration;
|
|
|
|
use fallible_iterator::FallibleIterator;
|
|
use tokio_postgres::{Column, Notification};
|
|
use tokio_postgres::row::RowIndex;
|
|
use tokio_postgres::types::FromSql;
|
|
|
|
use crate::types::{BorrowToSql, ToSql, Type};
|
|
use crate::{
|
|
Client, CopyInWriter, CopyOutReader, Error, Row, RowIter, SimpleQueryMessage, Statement,
|
|
ToStatement, Transaction};
|
|
|
|
macro_rules! common {
|
|
($transaction:ty, $err:ty) => {
|
|
/// Like `Client::execute`.
|
|
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, $err>
|
|
where
|
|
T: ?Sized + ToStatement;
|
|
|
|
/// Like `Client::query`.
|
|
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Self::Row>, $err>
|
|
where
|
|
T: ?Sized + ToStatement;
|
|
|
|
/// Like `Client::query_one`.
|
|
fn query_one<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Self::Row, $err>
|
|
where
|
|
T: ?Sized + ToStatement;
|
|
|
|
/// Like `Client::query_opt`.
|
|
fn query_opt<T>(
|
|
&mut self,
|
|
query: &T,
|
|
params: &[&(dyn ToSql + Sync)],
|
|
) -> Result<Option<Self::Row>, $err>
|
|
where
|
|
T: ?Sized + ToStatement;
|
|
|
|
/// Like `Client::query_raw`.
|
|
fn query_raw<T, P, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, $err>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
P: BorrowToSql,
|
|
I: IntoIterator<Item = P>,
|
|
I::IntoIter: ExactSizeIterator;
|
|
|
|
/// Like `Client::prepare`.
|
|
fn prepare(&mut self, query: &str) -> Result<Statement, $err>;
|
|
|
|
/// Like `Client::prepare_typed`.
|
|
fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, $err>;
|
|
|
|
/// Like `Client::copy_in`.
|
|
fn copy_in<T>(&mut self, query: &T) -> Result<CopyInWriter<'_>, $err>
|
|
where
|
|
T: ?Sized + ToStatement;
|
|
|
|
/// Like `Client::copy_out`.
|
|
fn copy_out<T>(&mut self, query: &T) -> Result<CopyOutReader<'_>, $err>
|
|
where
|
|
T: ?Sized + ToStatement;
|
|
|
|
/// Like `Client::simple_query`.
|
|
fn simple_query(&mut self, query: &str) -> Result<Vec<SimpleQueryMessage>, $err>;
|
|
|
|
/// Like `Client::batch_execute`.
|
|
fn batch_execute(&mut self, query: &str) -> Result<(), $err>;
|
|
|
|
/// Like `Client::transaction`.
|
|
fn transaction(&mut self) -> Result<$transaction, $err>;
|
|
};
|
|
}
|
|
|
|
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);
|
|
|
|
fn check_for_notify(&mut self) -> Result<Option<Notification>, Self::Error>;
|
|
}
|
|
|
|
impl GenericClient for Client {
|
|
type Error = Error;
|
|
type Transaction<'a> = Transaction<'a>;
|
|
type Row = Row;
|
|
|
|
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.execute(query, params)
|
|
}
|
|
|
|
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Self::Row>, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.query(query, params)
|
|
}
|
|
|
|
fn query_one<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Self::Row, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.query_one(query, params)
|
|
}
|
|
|
|
fn query_opt<T>(
|
|
&mut self,
|
|
query: &T,
|
|
params: &[&(dyn ToSql + Sync)],
|
|
) -> Result<Option<Self::Row>, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.query_opt(query, params)
|
|
}
|
|
|
|
fn query_raw<T, P, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
P: BorrowToSql,
|
|
I: IntoIterator<Item = P>,
|
|
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)
|
|
}
|
|
|
|
fn transaction(&mut self) -> Result<Self::Transaction<'_>, Error> {
|
|
self.transaction()
|
|
}
|
|
|
|
fn check_for_notify(&mut self) -> Result<Option<Notification>, Self::Error> {
|
|
let mut n = self.notifications();
|
|
let mut n = n.timeout_iter(Duration::from_millis(500));
|
|
n.next()
|
|
}
|
|
}
|
|
|
|
macro_rules! transaction_common {
|
|
($transaction:ty) => {
|
|
fn execute<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.execute(query, params)
|
|
}
|
|
|
|
fn query<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Self::Row>, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.query(query, params)
|
|
}
|
|
|
|
fn query_one<T>(&mut self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Self::Row, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.query_one(query, params)
|
|
}
|
|
|
|
fn query_opt<T>(
|
|
&mut self,
|
|
query: &T,
|
|
params: &[&(dyn ToSql + Sync)],
|
|
) -> Result<Option<Self::Row>, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
{
|
|
self.query_opt(query, params)
|
|
}
|
|
|
|
fn query_raw<T, P, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
|
|
where
|
|
T: ?Sized + ToStatement,
|
|
P: BorrowToSql,
|
|
I: IntoIterator<Item = P>,
|
|
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)
|
|
}
|
|
|
|
fn transaction(&mut self) -> Result<$transaction, Error> {
|
|
self.transaction()
|
|
}
|
|
};
|
|
}
|
|
|
|
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<'_>);
|
|
|
|
fn check_for_notify(&mut self) -> Result<Option<Notification>, Self::Error> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|