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
#[cfg(feature = "sqlite")]
use smol_bytes::Utf8BytesMut;

#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::FieldName;

/// The `LIKE` pattern never appears in the rendered SQL — it binds as a value — so a differential
/// over the text cannot see it going wrong, and the escaping is asserted here instead.
///
/// What it has to get right is that `\`, `%` and `_` in the OPERAND match literally: an unescaped
/// `%` would turn `contains: "50%"` into a match on everything beginning `50`, which is a wrong
/// result set reported as a right one.
///
/// Ungated, where the two dialects that USE it are not. The builder is shared rather than dispatched
/// now, so it compiles with the family root alone — and the family-root-with-no-dialect lane is the
/// combination that has caught real defects here twice.
#[test]
fn the_like_pattern_escapes_the_metacharacters_and_wraps_the_affix() {
  use super::like_pattern;
  use crate::comparison::TextMatch;

  assert_eq!(like_pattern("cam", TextMatch::Contains), "%cam%");
  assert_eq!(like_pattern("cam", TextMatch::Prefix), "cam%");
  assert_eq!(like_pattern("cam", TextMatch::Suffix), "%cam");

  // The three metacharacters, each escaped with the `\` that `ESCAPE '\'` names — and the wildcards
  // the affix adds are OUTSIDE the escaped operand, which is the whole distinction.
  assert_eq!(like_pattern("50%", TextMatch::Contains), "%50\\%%");
  assert_eq!(like_pattern("a_b", TextMatch::Prefix), "a\\_b%");
  assert_eq!(like_pattern("c:\\x", TextMatch::Suffix), "%c:\\\\x");
}

/// The identifier is the one thing on this path that becomes SQL TEXT rather than a bind, so unlike
/// every operand assertion in this crate, reading the rendered characters is the instrument that
/// fits: quoting IS text.
///
/// Each dialect gets its own test rather than one table, because the two halves fail differently.
/// The BRACKET is per dialect and a wrong one is not a compile error anywhere — it is a wrong
/// statement in the dialect that got it. The ESCAPE is one rule and each dialect escapes only its
/// OWN bracket: a `"` inside backticks is an ordinary character, and doubling it there would corrupt
/// a legal name.
#[cfg(feature = "sqlite")]
#[test]
fn sqlite_brackets_with_double_quotes_and_doubles_one_inside() {
  use super::quote_ident;

  assert_eq!(quote_ident::<sqlx::Sqlite>(FieldName::new("n")), "\"n\"");
  assert_eq!(
    quote_ident::<sqlx::Sqlite>(FieldName::new("a\"b")),
    "\"a\"\"b\""
  );
  // A backtick is not this dialect's bracket, so it is an ordinary character in a name.
  assert_eq!(
    quote_ident::<sqlx::Sqlite>(FieldName::new("a`b")),
    "\"a`b\""
  );

  // What the doubling is FOR: a name that closes its own quoting turns everything after it into
  // SQL. Escaped, the whole thing stays one identifier — a column that does not exist, which is an
  // error, rather than a predicate the caller never wrote.
  assert_eq!(
    quote_ident::<sqlx::Sqlite>(FieldName::new("n\" = 1 OR \"x")),
    "\"n\"\" = 1 OR \"\"x\""
  );

  // A dot is an ordinary character too: this is ONE identifier containing a dot, which is a
  // different reference from the qualified one `quote_qualified` renders below.
  assert_eq!(
    quote_ident::<sqlx::Sqlite>(FieldName::new("t.col")),
    "\"t.col\""
  );
}

/// A column PATH runs its segments together with `_` inside ONE pair of brackets, and escapes each
/// segment on its own — which is the same identifier a single name of the joined spelling would be,
/// and deliberately so: SQL's namespace is flat and `timebase_num` is what a column is called there.
///
/// The bracket character inside a segment is what says the two halves are in the right order. It
/// can only be escaped once the whole identifier is being written, which is after the join — so a
/// composite whose own name needs quoting reaches its columns intact, where a bracketed prefix
/// extended by a part would have produced `"timebase"_num` and no dialect parses that.
#[cfg(feature = "sqlite")]
#[test]
fn a_column_path_runs_its_segments_together_inside_one_pair_of_brackets() {
  use super::quote_column;
  use crate::{At, RecordAddress};

  const ROW: RecordAddress = RecordAddress::new(FieldName::new("t"), &[FieldName::new("id")]);

  let at = At::new(&ROW);
  let timebase = at.frame(FieldName::new("timebase"));
  let num = timebase.at().frame(FieldName::new("num"));

  let mut rendered = Utf8BytesMut::new();
  quote_column::<sqlx::Sqlite>(timebase.at(), &mut rendered);
  assert_eq!(rendered, "\"timebase\"");

  let mut rendered = Utf8BytesMut::new();
  quote_column::<sqlx::Sqlite>(num.at(), &mut rendered);
  assert_eq!(rendered, "\"timebase_num\"");

  let quoted = at.frame(FieldName::new("time\"base"));
  let part = quoted.at().frame(FieldName::new("num"));
  let mut rendered = Utf8BytesMut::new();
  quote_column::<sqlx::Sqlite>(part.at(), &mut rendered);
  assert_eq!(rendered, "\"time\"\"base_num\"");
}

/// The TOP LEVEL names no column, so nothing is written — not even brackets.
///
/// An empty bracketed name would be an identifier, and whether an engine refuses one or resolves it
/// to something is the engine's business. Writing nothing leaves the predicate around it
/// unparseable, which is the right answer for a caller that applied a COLUMN's comparison to the
/// row itself.
#[cfg(feature = "sqlite")]
#[test]
fn the_top_level_names_no_column_so_nothing_is_written() {
  use super::quote_column;
  use crate::{At, RecordAddress};

  const ROW: RecordAddress = RecordAddress::new(FieldName::new("t"), &[FieldName::new("id")]);

  let mut rendered = Utf8BytesMut::from("WHERE ");
  quote_column::<sqlx::Sqlite>(At::new(&ROW), &mut rendered);
  assert_eq!(rendered, "WHERE ");
}

/// A qualified reference puts the dot BETWEEN the brackets, and each half escapes on its own — so a
/// qualifier carrying the bracket character can no more close its own quoting than a name can.
///
/// The last assertion is the pair this function exists to keep apart: one name spelled `t.col` and a
/// reference to `col` in `t` are different things, and only the caller knows which it meant.
#[cfg(feature = "sqlite")]
#[test]
fn a_qualified_reference_brackets_each_half_and_joins_them_with_the_dot() {
  use super::{quote_ident, quote_qualified};

  assert_eq!(
    quote_qualified::<sqlx::Sqlite>(FieldName::new("t"), FieldName::new("col")),
    "\"t\".\"col\""
  );
  assert_eq!(
    quote_qualified::<sqlx::Sqlite>(FieldName::new("a\"b"), FieldName::new("c\"d")),
    "\"a\"\"b\".\"c\"\"d\""
  );
  assert_ne!(
    quote_qualified::<sqlx::Sqlite>(FieldName::new("t"), FieldName::new("col")),
    quote_ident::<sqlx::Sqlite>(FieldName::new("t.col"))
  );
}

/// The bracket is the DIALECT's here too, which is what keeps a correlation from becoming a
/// comparison against two string literals on MySQL.
#[cfg(feature = "mysql")]
#[test]
fn a_qualified_reference_takes_the_dialects_own_bracket() {
  use super::quote_qualified;

  assert_eq!(
    quote_qualified::<sqlx::MySql>(FieldName::new("t"), FieldName::new("col")),
    "`t`.`col`"
  );
}

#[cfg(feature = "postgres")]
#[test]
fn postgres_brackets_with_double_quotes_and_doubles_one_inside() {
  use super::quote_ident;

  assert_eq!(quote_ident::<sqlx::Postgres>(FieldName::new("n")), "\"n\"");
  assert_eq!(
    quote_ident::<sqlx::Postgres>(FieldName::new("a\"b")),
    "\"a\"\"b\""
  );
  assert_eq!(
    quote_ident::<sqlx::Postgres>(FieldName::new("a`b")),
    "\"a`b\""
  );
}

#[cfg(feature = "mysql")]
#[test]
fn mysql_brackets_with_backticks_and_doubles_one_inside() {
  use super::quote_ident;

  assert_eq!(quote_ident::<sqlx::MySql>(FieldName::new("n")), "`n`");
  assert_eq!(quote_ident::<sqlx::MySql>(FieldName::new("a`b")), "`a``b`");
  // The other dialects' bracket, which MySQL reads as a STRING delimiter rather than an identifier
  // one — so it must survive a name untouched, and doubling it here would rename the column.
  assert_eq!(quote_ident::<sqlx::MySql>(FieldName::new("a\"b")), "`a\"b`");
}