use core::marker::PhantomData;
#[cfg(any(feature = "postgres", feature = "mysql"))]
use core::fmt::Display;
use jiff::SignedDuration;
#[cfg(any(feature = "postgres", feature = "mysql"))]
use super::{Carrier, SqlConjunction, push_cmp, push_in};
#[cfg(any(feature = "postgres", feature = "mysql"))]
use crate::comparison::{At, Backend, TryFromFilter};
use crate::{
DurationFilter,
graphql::filter::{map_operand, map_operands},
};
comparison_kind! {
SqlDurationComparison {
one { eq, ne, gt, gte, lt, lte }
many { in_, not_in }
}
}
impl<V, Db> SqlDurationComparison<V, Db> {
pub fn try_from_filter_with<E, F>(filter: DurationFilter, mut f: F) -> Result<Self, E>
where
F: FnMut(SignedDuration) -> Result<V, E>,
{
Ok(Self {
eq: map_operand(filter.eq, &mut f)?,
ne: map_operand(filter.ne, &mut f)?,
gt: map_operand(filter.gt, &mut f)?,
gte: map_operand(filter.gte, &mut f)?,
lt: map_operand(filter.lt, &mut f)?,
lte: map_operand(filter.lte, &mut f)?,
in_: map_operands(filter.in_, &mut f)?,
not_in: map_operands(filter.not_in, &mut f)?,
db: PhantomData,
})
}
}
#[cfg(any(feature = "postgres", feature = "mysql"))]
impl<V, Db> TryFromFilter<DurationFilter> for SqlDurationComparison<V, Db>
where
V: TryFrom<SignedDuration>,
{
type Error = V::Error;
fn try_from_filter(filter: DurationFilter) -> Result<Self, Self::Error> {
Self::try_from_filter_with(filter, V::try_from)
}
}
#[cfg(any(feature = "postgres", feature = "mysql"))]
fn push_slots<V, Db>(
comparison: &SqlDurationComparison<V, Db>,
at: At<'_>,
out: &mut SqlConjunction<'_, Db>,
) where
Db: sqlx::Database + Backend<Buffer: Display>,
V: Carrier<Db>,
{
if let Some(value) = &comparison.eq {
push_cmp(out.open(), at, " = ", value, false);
}
if let Some(value) = &comparison.ne {
push_cmp(out.open(), at, " = ", value, true);
}
if let Some(value) = &comparison.gt {
push_cmp(out.open(), at, " > ", value, false);
}
if let Some(value) = &comparison.gte {
push_cmp(out.open(), at, " >= ", value, false);
}
if let Some(value) = &comparison.lt {
push_cmp(out.open(), at, " < ", value, false);
}
if let Some(value) = &comparison.lte {
push_cmp(out.open(), at, " <= ", value, false);
}
if let Some(values) = &comparison.in_ {
push_in(out.open(), at, values, false);
}
if let Some(values) = &comparison.not_in {
push_in(out.open(), at, values, true);
}
}
#[cfg(feature = "mysql")]
mod mysql;
#[cfg(feature = "postgres")]
mod postgres;
#[cfg(test)]
mod tests;