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
use super::{SqlTextComparison, push_slots};
use crate::comparison::{
  At, Backend, ComplexComparison, TextMatch,
  sql::{Carrier, not_wrapped, push_column},
};

/// One impl per dialect, because what differs is the LEAF below rather than the walk above:
/// `push_slots` is this kind's one slot walk and every dialect calls it, so the impls cannot come to
/// disagree about which operators render or in what order.
impl<V> ComplexComparison<sqlx::Sqlite> for SqlTextComparison<V, sqlx::Sqlite>
where
  V: Carrier<sqlx::Sqlite>,
{
  #[inline]
  fn conjuncts(&self) -> usize {
    Self::conjuncts(self)
  }

  fn apply<'q>(
    &'q self,
    at: At<'_>,
    mut builder: <sqlx::Sqlite as Backend>::Builder<'q>,
  ) -> Result<<sqlx::Sqlite as Backend>::Builder<'q>, <sqlx::Sqlite as Backend>::Error> {
    push_slots(self, at, &mut builder, push_text_match);
    Ok(builder)
  }
}

/// SQLite's substring / affix leaf.
fn push_text_match<V>(
  qb: &mut sqlx::QueryBuilder<sqlx::Sqlite>,
  at: At<'_>,
  kind: TextMatch,
  operand: &V,
  negated: bool,
  nocase: bool,
) where
  V: Carrier<sqlx::Sqlite>,
{
  not_wrapped(qb, negated, |qb| match kind {
    TextMatch::Contains => {
      qb.push("instr(");
      push_folded_column(qb, at, nocase);
      qb.push(", ");
      push_folded_operand(qb, operand, nocase);
      qb.push(") > 0");
    }
    // `substr(col, 1, length(v)) = v` and `substr(col, -length(v)) = v` — the operand binds
    // TWICE in each, once to measure and once to compare.
    TextMatch::Prefix => {
      qb.push("substr(");
      push_folded_column(qb, at, nocase);
      qb.push(", 1, length(");
      push_folded_operand(qb, operand, nocase);
      qb.push(")) = ");
      push_folded_operand(qb, operand, nocase);
    }
    TextMatch::Suffix => {
      qb.push("substr(");
      push_folded_column(qb, at, nocase);
      qb.push(", -length(");
      push_folded_operand(qb, operand, nocase);
      qb.push(")) = ");
      push_folded_operand(qb, operand, nocase);
    }
  });
}

/// The operand, bound through its carrier and case-folded when asked.
fn push_folded_operand<V>(qb: &mut sqlx::QueryBuilder<sqlx::Sqlite>, operand: &V, nocase: bool)
where
  V: Carrier<sqlx::Sqlite>,
{
  if nocase {
    qb.push("lower(");
  }
  operand.push_bind(qb);
  if nocase {
    qb.push(")");
  }
}

/// The column identifier, bracketed for this dialect and case-folded when asked. BOTH sides fold or
/// neither: folding one alone is a comparison between a folded value and an unfolded one, which is
/// not case-insensitivity.
fn push_folded_column(qb: &mut sqlx::QueryBuilder<sqlx::Sqlite>, at: At<'_>, nocase: bool) {
  if nocase {
    qb.push("lower(");
  }
  push_column(qb, at);
  if nocase {
    qb.push(")");
  }
}