ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
//! The boolean comparison: [`SqlBooleanComparison`].

use core::{fmt::Display, marker::PhantomData};

use super::{Carrier, SqlConjunction, push_cmp, push_in};
use crate::{
  BooleanFilter,
  comparison::{At, Backend, ComplexComparison, ToDatabaseComparison, TryFromFilter},
  graphql::filter::{map_operand, map_operands},
};

comparison_kind! {
  /// A [`BooleanFilter`]'s storage-side twin: equality and membership, over the type this database
  /// stores the column as.
  ///
  /// # It has a `V` where the filter has none
  ///
  /// [`BooleanFilter`] guards exactly one Rust type, so it needs no parameter. What a column STORES
  /// is a different question, and the answers a schema actually gives are several: a native
  /// `BOOLEAN`, an `INTEGER` of `0`/`1`, a `CHAR(1)` of `'Y'`/`'N'`. The storage form is a
  /// declaration here as everywhere else, so the comparison is parameterised even though the filter
  /// is not.
  ///
  /// That asymmetry is also why this kind has no operand-level `try_map` on the filter side: mapping
  /// `bool` to some `V` has no [`BooleanFilter`] to land in, and this type IS the landing place. The
  /// crossing therefore has one entry point rather than two, and
  /// [`try_from_filter_with`](Self::try_from_filter_with) is it.
  SqlBooleanComparison {
    one { eq, ne }
    many { in_, not_in }
  }
}

impl<V, Db> SqlBooleanComparison<V, Db> {
  /// Cross a [`BooleanFilter`] into this comparison, converting each operand with `f`.
  ///
  /// The entry point a DECLARED converter uses, and — see the type's own note — the only place a
  /// boolean's operands can be mapped at all.
  pub fn try_from_filter_with<E, F>(filter: BooleanFilter, mut f: F) -> Result<Self, E>
  where
    F: FnMut(bool) -> Result<V, E>,
  {
    Ok(Self {
      eq: map_operand(filter.eq, &mut f)?,
      ne: map_operand(filter.ne, &mut f)?,
      in_: map_operands(filter.in_, &mut f)?,
      not_in: map_operands(filter.not_in, &mut f)?,
      db: PhantomData,
    })
  }
}

/// The bounds are the LOWERING's, restated — which is what the crossing's item bound costs a row
/// whose comparison lowers under one generic impl. `Db: sqlx::Database` stays at the head of them
/// for the coherence reason [`SqlScalarComparison`](super::SqlScalarComparison)'s row records.
impl<V, Db> ToDatabaseComparison<V, Db> for BooleanFilter
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Buffer: Display>,
  V: Carrier<Db>,
{
  type Comparison = SqlBooleanComparison<V, Db>;
}

/// The crossing with no declared converter. `bool` stored as `bool` is the identity and cannot fail;
/// `bool` stored as an `i16` or an `i32` is a widening `From` and cannot fail either — so the common
/// boolean column reaches its comparison with an [`Infallible`](core::convert::Infallible) error
/// type.
impl<V, Db> TryFromFilter<BooleanFilter> for SqlBooleanComparison<V, Db>
where
  V: TryFrom<bool>,
{
  type Error = V::Error;

  fn try_from_filter(filter: BooleanFilter) -> Result<Self, Self::Error> {
    Self::try_from_filter_with(filter, V::try_from)
  }
}

/// ONE impl for every dialect, on the same positive finding
/// [`SqlScalarComparison`](super::SqlScalarComparison) records:
///
/// - **The SQL is identical.** `"col" = ?` and `"col" IN (?, …)`, one spelling in all three.
/// - **The bounds are identical, and admit the same types.** `V: Carrier<Db>`, and nothing else.
///
/// The storage forms a boolean column actually takes — a native `BOOLEAN`, MySQL's `TINYINT(1)`, an
/// `INTEGER` of `0`/`1`, a `CHAR(1)` of `'Y'`/`'N'` — vary widely and change nothing here, because
/// each is a `V` with its own [`Carrier`] row. The kind asks `= ?` whichever one it is.
///
/// `IS TRUE` / `IS NOT FALSE` are not in the vocabulary, so the three-valued forms whose spellings
/// DO diverge are never reached from this kind; a null column's question belongs to
/// [`SqlNullable`](super::SqlNullable), which owns the null test and renders it one way everywhere.
impl<V, Db> ComplexComparison<Db> for SqlBooleanComparison<V, Db>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Buffer: Display>,
  V: Carrier<Db>,
{
  #[inline]
  fn conjuncts(&self) -> usize {
    Self::conjuncts(self)
  }

  fn apply<'q>(
    &'q self,
    at: At<'_>,
    mut builder: Db::Builder<'q>,
  ) -> Result<Db::Builder<'q>, Db::Error> {
    if let Some(value) = &self.eq {
      push_cmp(builder.open(), at, " = ", value, false);
    }
    if let Some(value) = &self.ne {
      push_cmp(builder.open(), at, " = ", value, true);
    }
    if let Some(values) = &self.in_ {
      push_in(builder.open(), at, values, false);
    }
    if let Some(values) = &self.not_in {
      push_in(builder.open(), at, values, true);
    }
    Ok(builder)
  }
}

#[cfg(test)]
mod tests;