use async_trait::async_trait;
use postgres::types::FromSql;
use std::fmt::Debug;
use tokio_postgres::types::ToSql;
use tokio_postgres::{Error, Row};
pub trait SqlQuery<R> {
fn query() -> String;
}
pub trait SqlCommand {
fn query() -> String;
}
pub trait SqlParams {
fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
}
pub trait UpdateParams {
fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
}
pub trait FromRow {
fn from_row(row: &Row) -> Result<Self, Error>
where
Self: Sized;
}
#[async_trait]
pub trait CrudOps {
async fn insert<T, P: for<'a> FromSql<'a> + Send + Sync>(&self, entity: T) -> Result<P, Error>
where
T: SqlCommand + SqlParams + Send + Sync;
async fn update<T>(&self, entity: T) -> Result<u64, Error>
where
T: SqlCommand + UpdateParams + Send + Sync;
async fn delete<T>(&self, entity: T) -> Result<u64, Error>
where
T: SqlCommand + SqlParams + Send + Sync;
async fn fetch<P, R>(&self, params: &P) -> Result<R, Error>
where
P: SqlQuery<R> + SqlParams + Send + Sync,
R: FromRow + Send + Sync;
async fn fetch_all<P, R>(&self, params: &P) -> Result<Vec<R>, Error>
where
P: SqlQuery<R> + SqlParams + Send + Sync,
R: FromRow + Send + Sync;
async fn select<T, R, F>(&self, entity: T, to_model: F) -> Result<R, Error>
where
T: SqlQuery<T> + SqlParams + Send + Sync,
F: FnOnce(&Row) -> Result<R, Error> + Send + Sync;
async fn select_all<T, R, F>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
where
T: SqlQuery<T> + SqlParams + Send + Sync,
F: Fn(&Row) -> R + Send + Sync;
}
#[async_trait]
pub trait TransactionOps {
async fn tx_insert<T, P>(&self, entity: T) -> Result<P, Error>
where
T: SqlCommand + SqlParams + Debug + Send + 'static,
P: for<'a> tokio_postgres::types::FromSql<'a> + Send + Sync;
async fn tx_update<T>(&self, entity: T) -> Result<bool, Error>
where
T: SqlCommand + UpdateParams + SqlParams + Debug + Send + 'static;
async fn tx_delete<T>(&self, entity: T) -> Result<u64, Error>
where
T: SqlCommand + SqlParams + Debug + Send + 'static;
async fn tx_fetch<P, R>(&self, params: &P) -> Result<R, Error>
where
P: SqlQuery<R> + SqlParams + Debug + Send + Sync + Clone + 'static,
R: FromRow + Debug + Send + Sync + Clone + 'static;
async fn tx_fetch_all<P, R>(&self, params: &P) -> Result<Vec<R>, Error>
where
P: SqlQuery<R> + SqlParams + Debug + Send + Sync + Clone + 'static,
R: FromRow + Debug + Send + Sync + Clone + 'static;
async fn tx_select<T, F, R>(&self, entity: T, to_model: F) -> Result<R, Error>
where
T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
F: Fn(&Row) -> Result<R, Error> + Send + Sync + 'static,
R: Send + 'static;
async fn tx_select_all<T, F, R>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
where
T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
F: Fn(&Row) -> R + Send + Sync + 'static,
R: Send + 'static;
#[deprecated(
since = "0.2.0",
note = "Renamed to `tx_insert`. Please use `tx_insert` function instead."
)]
async fn insert<T>(&self, entity: T) -> Result<u64, Error>
where
T: SqlCommand + SqlParams + Debug + Send + 'static;
#[deprecated(
since = "0.2.0",
note = "Renamed to `tx_update`. Please use `tx_update` function instead."
)]
async fn update<T>(&self, entity: T) -> Result<u64, Error>
where
T: SqlCommand + UpdateParams + SqlParams + Debug + Send + 'static;
#[deprecated(
since = "0.2.0",
note = "Renamed to `tx_delete`. Please use `tx_delete` function instead."
)]
async fn delete<T>(&self, entity: T) -> Result<u64, Error>
where
T: SqlCommand + SqlParams + Debug + Send + 'static;
#[deprecated(
since = "0.2.0",
note = "Renamed to `tx_fetch`. Please use `tx_fetch` function instead."
)]
async fn get<T>(&self, params: &T) -> Result<T, Error>
where
T: SqlQuery<T> + FromRow + SqlParams + Debug + Send + Sync + Clone + 'static;
#[deprecated(
since = "0.2.0",
note = "Renamed to `tx_fetch_all`. Please use `tx_fetch_all` function instead."
)]
async fn get_all<T>(&self, params: &T) -> Result<Vec<T>, Error>
where
T: SqlQuery<T> + FromRow + SqlParams + Debug + Send + Sync + Clone + 'static;
#[deprecated(
since = "0.2.0",
note = "Renamed to `tx_select`. Please use `tx_select` function instead."
)]
async fn select<T, R, F>(&self, entity: T, to_model: F) -> Result<R, Error>
where
T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
F: for<'a> Fn(&'a Row) -> Result<R, Error> + Send + Sync + 'static,
R: Send + 'static;
#[deprecated(
since = "0.2.0",
note = "Renamed to `tx_select_all`. Please use `tx_select_all` function instead."
)]
async fn select_all<T, R, F>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
where
T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
F: Fn(&Row) -> R + Send + Sync + 'static,
R: Send + 'static;
}