use super::ProjectionResult;
#[cfg(feature = "postgres")]
use sqlx::{
Executor, PgConnection, Postgres, Transaction,
postgres::{PgArguments, PgQueryResult, PgRow},
query::Query,
};
#[async_trait::async_trait]
pub trait Handler<Envelope>: Send + 'static {
async fn process(&mut self, envelope: &Envelope) -> ProjectionResult<()>;
}
#[async_trait::async_trait]
impl<Envelope, H> Handler<Envelope> for Box<H>
where
Envelope: Sync + 'static,
H: Handler<Envelope> + ?Sized,
{
async fn process(&mut self, envelope: &Envelope) -> ProjectionResult<()> {
(**self).process(envelope).await
}
}
#[async_trait::async_trait]
pub trait GroupedHandler<Envelope>: Send + 'static {
async fn process(&mut self, envelopes: &[Envelope]) -> ProjectionResult<()>;
}
#[async_trait::async_trait]
impl<Envelope, H> GroupedHandler<Envelope> for Box<H>
where
Envelope: Sync + 'static,
H: GroupedHandler<Envelope> + ?Sized,
{
async fn process(&mut self, envelopes: &[Envelope]) -> ProjectionResult<()> {
(**self).process(envelopes).await
}
}
#[cfg(feature = "postgres")]
pub struct PostgresProjectionTx {
pub(crate) inner: Option<Transaction<'static, Postgres>>,
}
#[cfg(feature = "postgres")]
impl PostgresProjectionTx {
pub(crate) fn new(transaction: Transaction<'static, Postgres>) -> Self {
Self {
inner: Some(transaction),
}
}
fn connection(&mut self) -> ProjectionResult<&mut PgConnection> {
self.inner.as_deref_mut().ok_or_else(|| {
super::ProjectionError::Runtime(
"PostgreSQL projection transaction is already complete".into(),
)
})
}
pub async fn execute<'query>(
&mut self,
query: Query<'query, Postgres, PgArguments>,
) -> ProjectionResult<PgQueryResult> {
self.connection()?
.execute(query)
.await
.map_err(handler_query_error)
}
pub async fn fetch_one<'query>(
&mut self,
query: Query<'query, Postgres, PgArguments>,
) -> ProjectionResult<PgRow> {
self.connection()?
.fetch_one(query)
.await
.map_err(handler_query_error)
}
pub async fn fetch_optional<'query>(
&mut self,
query: Query<'query, Postgres, PgArguments>,
) -> ProjectionResult<Option<PgRow>> {
self.connection()?
.fetch_optional(query)
.await
.map_err(handler_query_error)
}
pub async fn fetch_all<'query>(
&mut self,
query: Query<'query, Postgres, PgArguments>,
) -> ProjectionResult<Vec<PgRow>> {
self.connection()?
.fetch_all(query)
.await
.map_err(handler_query_error)
}
pub fn sqlx_connection(&mut self) -> ProjectionResult<&mut PgConnection> {
self.connection()
}
pub(crate) fn connection_for_cursor(&mut self) -> ProjectionResult<&mut PgConnection> {
self.connection()
}
pub(crate) async fn commit(mut self) -> ProjectionResult<()> {
self.inner
.take()
.ok_or_else(|| {
super::ProjectionError::Runtime(
"PostgreSQL projection transaction is already complete".into(),
)
})?
.commit()
.await
.map_err(|error| {
super::ProjectionError::OffsetStore(format!(
"commit PostgreSQL exactly-once transaction: {error}"
))
})
}
pub(crate) async fn rollback(mut self) -> ProjectionResult<()> {
self.inner
.take()
.ok_or_else(|| {
super::ProjectionError::Runtime(
"PostgreSQL projection transaction is already complete".into(),
)
})?
.rollback()
.await
.map_err(|error| {
super::ProjectionError::OffsetStore(format!(
"rollback PostgreSQL exactly-once transaction: {error}"
))
})
}
}
#[cfg(feature = "postgres")]
fn handler_query_error(error: sqlx::Error) -> super::ProjectionError {
super::ProjectionError::Handler(format!(
"PostgreSQL exactly-once handler query failed: {error}"
))
}
#[cfg(feature = "postgres")]
#[async_trait::async_trait]
pub trait ExactlyOncePostgresHandler<Envelope>: Send + 'static {
async fn process(
&mut self,
transaction: &mut PostgresProjectionTx,
envelope: &Envelope,
) -> ProjectionResult<()>;
}
#[cfg(feature = "postgres")]
#[async_trait::async_trait]
impl<Envelope, H> ExactlyOncePostgresHandler<Envelope> for Box<H>
where
Envelope: Sync + 'static,
H: ExactlyOncePostgresHandler<Envelope> + ?Sized,
{
async fn process(
&mut self,
transaction: &mut PostgresProjectionTx,
envelope: &Envelope,
) -> ProjectionResult<()> {
(**self).process(transaction, envelope).await
}
}
#[cfg(feature = "postgres")]
#[async_trait::async_trait]
pub trait GroupedExactlyOncePostgresHandler<Envelope>: Send + 'static {
async fn process(
&mut self,
transaction: &mut PostgresProjectionTx,
envelopes: &[Envelope],
) -> ProjectionResult<()>;
}
#[cfg(feature = "postgres")]
#[async_trait::async_trait]
impl<Envelope, H> GroupedExactlyOncePostgresHandler<Envelope> for Box<H>
where
Envelope: Sync + 'static,
H: GroupedExactlyOncePostgresHandler<Envelope> + ?Sized,
{
async fn process(
&mut self,
transaction: &mut PostgresProjectionTx,
envelopes: &[Envelope],
) -> ProjectionResult<()> {
(**self).process(transaction, envelopes).await
}
}