ingraph/comparison/sql/boolean.rs
1//! The boolean comparison: [`SqlBooleanComparison`].
2
3use core::{fmt::Display, marker::PhantomData};
4
5use super::{Carrier, SqlConjunction, push_cmp, push_in};
6use crate::{
7 BooleanFilter,
8 comparison::{At, Backend, ComplexComparison, ToDatabaseComparison, TryFromFilter},
9 graphql::filter::{map_operand, map_operands},
10};
11
12comparison_kind! {
13 /// A [`BooleanFilter`]'s storage-side twin: equality and membership, over the type this database
14 /// stores the column as.
15 ///
16 /// # It has a `V` where the filter has none
17 ///
18 /// [`BooleanFilter`] guards exactly one Rust type, so it needs no parameter. What a column STORES
19 /// is a different question, and the answers a schema actually gives are several: a native
20 /// `BOOLEAN`, an `INTEGER` of `0`/`1`, a `CHAR(1)` of `'Y'`/`'N'`. The storage form is a
21 /// declaration here as everywhere else, so the comparison is parameterised even though the filter
22 /// is not.
23 ///
24 /// That asymmetry is also why this kind has no operand-level `try_map` on the filter side: mapping
25 /// `bool` to some `V` has no [`BooleanFilter`] to land in, and this type IS the landing place. The
26 /// crossing therefore has one entry point rather than two, and
27 /// [`try_from_filter_with`](Self::try_from_filter_with) is it.
28 SqlBooleanComparison {
29 one { eq, ne }
30 many { in_, not_in }
31 }
32}
33
34impl<V, Db> SqlBooleanComparison<V, Db> {
35 /// Cross a [`BooleanFilter`] into this comparison, converting each operand with `f`.
36 ///
37 /// The entry point a DECLARED converter uses, and — see the type's own note — the only place a
38 /// boolean's operands can be mapped at all.
39 pub fn try_from_filter_with<E, F>(filter: BooleanFilter, mut f: F) -> Result<Self, E>
40 where
41 F: FnMut(bool) -> Result<V, E>,
42 {
43 Ok(Self {
44 eq: map_operand(filter.eq, &mut f)?,
45 ne: map_operand(filter.ne, &mut f)?,
46 in_: map_operands(filter.in_, &mut f)?,
47 not_in: map_operands(filter.not_in, &mut f)?,
48 db: PhantomData,
49 })
50 }
51}
52
53/// The bounds are the LOWERING's, restated — which is what the crossing's item bound costs a row
54/// whose comparison lowers under one generic impl. `Db: sqlx::Database` stays at the head of them
55/// for the coherence reason [`SqlScalarComparison`](super::SqlScalarComparison)'s row records.
56impl<V, Db> ToDatabaseComparison<V, Db> for BooleanFilter
57where
58 Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Buffer: Display>,
59 V: Carrier<Db>,
60{
61 type Comparison = SqlBooleanComparison<V, Db>;
62}
63
64/// The crossing with no declared converter. `bool` stored as `bool` is the identity and cannot fail;
65/// `bool` stored as an `i16` or an `i32` is a widening `From` and cannot fail either — so the common
66/// boolean column reaches its comparison with an [`Infallible`](core::convert::Infallible) error
67/// type.
68impl<V, Db> TryFromFilter<BooleanFilter> for SqlBooleanComparison<V, Db>
69where
70 V: TryFrom<bool>,
71{
72 type Error = V::Error;
73
74 fn try_from_filter(filter: BooleanFilter) -> Result<Self, Self::Error> {
75 Self::try_from_filter_with(filter, V::try_from)
76 }
77}
78
79/// ONE impl for every dialect, on the same positive finding
80/// [`SqlScalarComparison`](super::SqlScalarComparison) records:
81///
82/// - **The SQL is identical.** `"col" = ?` and `"col" IN (?, …)`, one spelling in all three.
83/// - **The bounds are identical, and admit the same types.** `V: Carrier<Db>`, and nothing else.
84///
85/// The storage forms a boolean column actually takes — a native `BOOLEAN`, MySQL's `TINYINT(1)`, an
86/// `INTEGER` of `0`/`1`, a `CHAR(1)` of `'Y'`/`'N'` — vary widely and change nothing here, because
87/// each is a `V` with its own [`Carrier`] row. The kind asks `= ?` whichever one it is.
88///
89/// `IS TRUE` / `IS NOT FALSE` are not in the vocabulary, so the three-valued forms whose spellings
90/// DO diverge are never reached from this kind; a null column's question belongs to
91/// [`SqlNullable`](super::SqlNullable), which owns the null test and renders it one way everywhere.
92impl<V, Db> ComplexComparison<Db> for SqlBooleanComparison<V, Db>
93where
94 Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Buffer: Display>,
95 V: Carrier<Db>,
96{
97 #[inline]
98 fn conjuncts(&self) -> usize {
99 Self::conjuncts(self)
100 }
101
102 fn apply<'q>(
103 &'q self,
104 at: At<'_>,
105 mut builder: Db::Builder<'q>,
106 ) -> Result<Db::Builder<'q>, Db::Error> {
107 if let Some(value) = &self.eq {
108 push_cmp(builder.open(), at, " = ", value, false);
109 }
110 if let Some(value) = &self.ne {
111 push_cmp(builder.open(), at, " = ", value, true);
112 }
113 if let Some(values) = &self.in_ {
114 push_in(builder.open(), at, values, false);
115 }
116 if let Some(values) = &self.not_in {
117 push_in(builder.open(), at, values, true);
118 }
119 Ok(builder)
120 }
121}
122
123#[cfg(test)]
124mod tests;