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 dialect rows.
//!
//! Gated on there being a dialect at all: with the family root on and none of the three, this trait
//! is implemented for nothing and there is no row to assert about.
#![cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]

use super::Backend;
use crate::{
  BindCeiling,
  comparison::sql::{SqlConjunction, SqlLoweringError},
};

/// Open the accumulator a comparison would be threaded, and render two conjuncts into it.
///
/// The BOUND is the assertion and the annotation on `accumulator` is what discharges it: the row's
/// builder must BE the AND accumulator over this dialect, which is exactly what one generic
/// `ComplexComparison` impl per uniform kind rests on. Writing it as a function is what forces each
/// call below to prove it separately.
fn two_conjuncts<Db>() -> String
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
{
  let mut qb = sqlx::QueryBuilder::<Db>::default();
  {
    // A fragment: what is asserted below is the SEPARATOR between two conjuncts, so the accumulator
    // opens no clause around them.
    let mut accumulator: <Db as Backend>::Builder<'_> = SqlConjunction::with_lead(&mut qb, "");
    accumulator.open().push("\"a\" = 1");
    accumulator.open().push("\"b\" = 2");
  }
  qb.into_string()
}

/// Every enabled dialect's row names the same accumulator over itself — and the separator between
/// two conjuncts is the accumulator's, which is why neither writer above pushed one.
#[test]
fn every_dialect_row_names_the_accumulator_over_itself() {
  const RENDERED: &str = "\"a\" = 1 AND \"b\" = 2";

  #[cfg(feature = "sqlite")]
  assert_eq!(two_conjuncts::<sqlx::Sqlite>(), RENDERED);
  #[cfg(feature = "postgres")]
  assert_eq!(two_conjuncts::<sqlx::Postgres>(), RENDERED);
  #[cfg(feature = "mysql")]
  assert_eq!(two_conjuncts::<sqlx::MySql>(), RENDERED);
}

/// This dialect's bind ceiling, reached through a bound that is itself half the assertion: the row
/// must name this family's ONE refusal type, which is what a single generic
/// [`ComplexComparison`](crate::ComplexComparison) impl over three dialects rests on for its errors
/// exactly as it does for its builder.
fn ceiling<Db>() -> usize
where
  Db: BindCeiling + Backend<Error = SqlLoweringError>,
{
  Db::MAX_BIND_PARAMETERS
}

/// Every enabled dialect's row refuses with the same type and states its OWN number — the split
/// [`quote_ident`](crate::comparison::sql::quote_ident) already makes one level down, where the
/// rule is one everywhere and the character is not.
#[test]
fn every_dialect_row_refuses_with_the_same_type_and_its_own_number() {
  #[cfg(feature = "sqlite")]
  assert_eq!(ceiling::<sqlx::Sqlite>(), 32766);
  #[cfg(feature = "postgres")]
  assert_eq!(ceiling::<sqlx::Postgres>(), 65535);
  #[cfg(feature = "mysql")]
  assert_eq!(ceiling::<sqlx::MySql>(), 65535);
}

/// A SQL lowering refuses an OPERAND past the bind ceiling, an ADDRESS naming no field, an element
/// SCOPE MySQL's JSON columns have no spelling for, and an address naming MORE columns than a
/// relation joins on — and nothing else. The EXHAUSTIVE `match` is the assertion rather than a claim
/// in prose: it stops compiling the moment a SQL lowering acquires a further way to fail, which is
/// the moment that failure owes a name of its own.
///
/// It has now fired three times. It began with NO arms, saying the same thing about a family that
/// could not fail at all; [`SqlKeyIn`](crate::comparison::sql::SqlKeyIn)'s ceiling was the first
/// arm, [`edge_table`](crate::comparison::sql::edge_table)'s refusal the second, and the relation
/// kinds' one-column join the fourth, and their alias CEILING the fifth. The assertion moves rather
/// than going away: what a lowering refuses stays pinned by the compiler and not by prose.
#[cfg(feature = "sqlite")]
#[test]
fn a_sql_lowering_refuses_a_ceiling_an_edge_a_json_scope_a_wide_relation_a_deep_one_and_no_more() {
  // Inside the function, under the test's own `sqlite` gate: the three payloads are named HERE and
  // nowhere else in the module, so at module scope they are unused imports in every single-dialect
  // build that is not this one.
  use crate::{
    FieldName,
    comparison::sql::{BindCeilingError, RelationDepthError, RelationWidthError},
  };

  /// What a caller reads off each refusal — the number to batch by, and the row that was addressed.
  fn reason(error: <sqlx::Sqlite as Backend>::Error) -> String {
    match error {
      SqlLoweringError::BindCeiling(error) => error.ceiling().to_string(),
      SqlLoweringError::EdgeWithoutField(table) => (**table).to_owned(),
      SqlLoweringError::JsonElementScope => "json".to_owned(),
      SqlLoweringError::RelationWidth(width) => {
        format!("{}/{}", &**width.table(), width.columns())
      }
      SqlLoweringError::RelationDepth(depth) => {
        format!("{}@{}/{}", &**depth.table(), depth.depth(), depth.ceiling())
      }
    }
  }

  let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> = Err(SqlLoweringError::BindCeiling(
    BindCeilingError::new(16384, 2, <sqlx::Sqlite as BindCeiling>::MAX_BIND_PARAMETERS),
  ));
  match lowered {
    Ok(()) => panic!("the refusals here are the ones under test"),
    Err(error) => assert_eq!(reason(error), "32766"),
  }

  let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> =
    Err(SqlLoweringError::EdgeWithoutField(FieldName::new("track")));
  match lowered {
    Ok(()) => panic!("the refusals here are the ones under test"),
    Err(error) => assert_eq!(reason(error), "track"),
  }

  let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> = Err(SqlLoweringError::RelationDepth(
    RelationDepthError::new(FieldName::new("scene"), 8, 8),
  ));
  match lowered {
    Ok(()) => panic!("the refusals here are the ones under test"),
    Err(error) => assert_eq!(reason(error), "scene@8/8"),
  }

  let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> = Err(SqlLoweringError::RelationWidth(
    RelationWidthError::new(FieldName::new("splice"), 2),
  ));
  match lowered {
    Ok(()) => panic!("the refusals here are the ones under test"),
    Err(error) => assert_eq!(reason(error), "splice/2"),
  }
}