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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! **THE COMPANION IS WRITTEN BY THE STATEMENT THAT WRITES THE ROW** — the `sortable` word's write
//! half, and the assignment a keyed replace must not leave out.
//!
//! A suite of its own beside [`super::tests`], for the reason the schema lane's shadow suite gives:
//! what is asserted here is ONE word's effect on two statements, and holding it apart from the
//! lane's own corpus keeps a change to either visible.
//!
//! # Two claims, and the second is where the silence would have been
//!
//! | claim | how it is measured |
//! |---|---|
//! | the companion rides the PARENT statement, so the dual write needs no bracket | the rendered text, and the statement COUNT being what it was without the word |
//! | an `upsert` REPLACES the companion beside the edge set | live: two writes, and the order read back off the column |
//!
//! The second is the one a text assertion alone would miss. `upsert` replaces the edge rows, so a
//! companion left out of the assignment list holds the PREVIOUS collection's order beside the new
//! collection — a wrong `ORDER BY` for every subsequent query, with no error anywhere.

#![cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]

use sqlx::QueryBuilder;

use super::{ShadowWrite, insert_statement, statements, upsert_statement};
use crate::{
  Carrier, ColumnName, ColumnShape, ComplexComparison, FieldName, Identifiable, Record,
  RecordAddress, Transformer, collection_sort_key,
  comparison::{Backend, sql::SqlKeyIn},
  query::sql::{CrossingError, EdgeShape, EdgeWrite, SqlQuery, SqlWrite},
};

/// A reel, whose tags are `sortable`: the collection lives in `reel_tags` and its ORDER lives in the
/// companion column beside the row.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Reel {
  id: i64,
  tags: Vec<String>,
}

impl Identifiable for Reel {
  type Id = i64;

  fn id_ref(&self) -> &i64 {
    &self.id
  }
}

impl Transformer for Reel {
  type Graphql = ();
  type Key = i64;
  type Lineage = i64;
  type Record = Self;

  fn key(&self) -> i64 {
    self.id
  }
}

impl<Db: Backend> Record<Db> for Reel {
  type Row = ReelRow;
  type Comparison = SqlKeyIn<i64, Db, 1>;
  type OrderBy = ();
}

/// The stored row. The collection is HERE and the companion is not: a companion is computed from the
/// collection at write time and never held, which is what keeps one value from being stored twice in
/// two states.
#[derive(Debug, Clone, PartialEq, Eq)]
struct ReelRow {
  id: i64,
  tags: Vec<String>,
}

const REEL: RecordAddress = RecordAddress::new(FieldName::new("reel"), &[FieldName::new("id")]);

/// The companion column's name, minted where the declaration is read and carried from there.
const TAGS_SORT: &FieldName = FieldName::new("__ig_tags_sort__");

impl<Db> SqlQuery<Db> for ReelRow
where
  Db: Backend + sqlx::Database,
  SqlKeyIn<i64, Db, 1>: ComplexComparison<Db>,
  i64: Carrier<Db>,
  for<'r> i64: sqlx::Decode<'r, Db> + sqlx::Type<Db>,
  for<'a> &'a str: sqlx::ColumnIndex<Db::Row>,
{
  type Record = Reel;
  type Address = SqlKeyIn<i64, Db, 1>;

  const ADDRESS: RecordAddress = REEL;
  const ALIASES: &'static [&'static str] = &["__ig_reel_0__", "__ig_reel_1__"];
  /// The collection is absent, as ever, and so is the COMPANION — a companion is no record column:
  /// nothing selects it and nothing decodes it back into a field.
  const COLUMNS: &'static [ColumnShape] = &[ColumnShape::Plain(ColumnName::new("id"))];

  fn addresses(ids: &[i64]) -> Result<Self::Address, CrossingError> {
    Ok(SqlKeyIn::new(
      ["id"],
      ids.iter().map(|id| [*id]).collect::<Vec<_>>(),
    ))
  }

  fn id(&self) -> Result<i64, CrossingError> {
    Ok(self.id)
  }

  fn record(self) -> Result<Reel, CrossingError> {
    Ok(Reel {
      id: self.id,
      tags: self.tags,
    })
  }
}

/// How many tags this row holds.
fn tags_len(row: &ReelRow) -> usize {
  row.tags.len()
}

/// The `at`th tag, bound.
fn tags_element<Db>(row: &ReelRow, at: usize, qb: &mut QueryBuilder<Db>)
where
  Db: sqlx::Database,
  String: Carrier<Db>,
{
  row.tags[at].push_bind(qb);
}

/// The whole collection's ORDER, bound — the one function a `sortable` collection adds to a write.
///
/// It is what an emitted plan writes, spelled here by hand: the framework's own
/// [`collection_sort_key`](crate::collection_sort_key) over the row's field, bound as bytes.
fn tags_sort<Db>(row: &ReelRow, qb: &mut QueryBuilder<Db>)
where
  Db: sqlx::Database,
  Vec<u8>: Carrier<Db>,
{
  collection_sort_key(&row.tags).push_bind(qb);
}

impl<Db> SqlWrite<Db> for ReelRow
where
  Db: Backend + sqlx::Database,
  SqlKeyIn<i64, Db, 1>: ComplexComparison<Db>,
  i64: Carrier<Db>,
  String: Carrier<Db>,
  Vec<u8>: Carrier<Db>,
  for<'r> i64: sqlx::Decode<'r, Db> + sqlx::Type<Db>,
  for<'a> &'a str: sqlx::ColumnIndex<Db::Row>,
{
  const EDGES: &'static [EdgeWrite<Self, Db>] = &[EdgeWrite::new(
    &[FieldName::new("tags")],
    EdgeShape::Sequence,
    tags_len,
    tags_element::<Db>,
  )];
  const SHADOWS: &'static [ShadowWrite<Self, Db>] = &[ShadowWrite::new(TAGS_SORT, tags_sort::<Db>)];

  fn row(record: Reel) -> Result<Self, CrossingError> {
    Ok(Self {
      id: record.id,
      tags: record.tags,
    })
  }

  fn bind(&self, at: usize, qb: &mut QueryBuilder<Db>) {
    debug_assert_eq!(at, 0, "this row has one column");
    self.id.push_bind(qb);
  }

  fn bind_address(&self, _at: usize, qb: &mut QueryBuilder<Db>) {
    self.id.push_bind(qb);
  }
}

/// A row holding `tags`.
fn reel(id: i64, tags: &[&str]) -> ReelRow {
  ReelRow {
    id,
    tags: tags.iter().map(|tag| (*tag).to_owned()).collect(),
  }
}

/// One rendered statement's text — the same road the lane's own corpus reads a statement by.
fn text<Db>(mut statement: QueryBuilder<Db>) -> String
where
  Db: sqlx::Database,
  <Db as sqlx::Database>::Arguments: sqlx::IntoArguments<Db>,
{
  use sqlx::Execute as _;

  statement.build().sql().as_str().to_owned()
}

/// The `INSERT` one dialect renders for it.
fn inserted<Db>(row: &ReelRow) -> String
where
  ReelRow: SqlWrite<Db>,
  Db: crate::query::sql::SqlDialect,
  <Db as sqlx::Database>::Arguments: sqlx::IntoArguments<Db>,
{
  text(insert_statement::<ReelRow, Db>(row))
}

/// The `upsert` one dialect renders for it.
fn upserted<Db>(row: &ReelRow) -> String
where
  ReelRow: SqlWrite<Db>,
  Db: crate::query::sql::SqlDialect + super::Upsert,
  <Db as sqlx::Database>::Arguments: sqlx::IntoArguments<Db>,
{
  text(upsert_statement::<ReelRow, Db>(row))
}

/// **The companion is a column of the `INSERT`, and its value is one more placeholder.** One
/// statement writes the row and its order; there is no second statement for the dual write to be
/// split across.
#[cfg(feature = "sqlite")]
#[test]
fn the_insert_carries_the_companion() {
  assert_eq!(
    inserted::<sqlx::Sqlite>(&reel(1, &["a", "b"])),
    r#"INSERT INTO "reel" ("id", "__ig_tags_sort__") VALUES (?, ?)"#
  );
}

/// **The keyed replace ASSIGNS it**, which is the row that keeps a replaced collection from being
/// ordered by the collection it replaced.
#[cfg(feature = "sqlite")]
#[test]
fn the_keyed_replace_assigns_the_companion() {
  assert_eq!(
    upserted::<sqlx::Sqlite>(&reel(1, &["a"])),
    r#"INSERT INTO "reel" ("id", "__ig_tags_sort__") VALUES (?, ?) ON CONFLICT ("id") DO UPDATE SET "id" = "excluded"."id", "__ig_tags_sort__" = "excluded"."__ig_tags_sort__""#
  );
}

/// PostgreSQL renders the same shape with its own placeholders and the same assignment.
#[cfg(feature = "postgres")]
#[test]
fn postgres_renders_the_companion_in_both_halves() {
  assert_eq!(
    upserted::<sqlx::Postgres>(&reel(1, &["a"])),
    r#"INSERT INTO "reel" ("id", "__ig_tags_sort__") VALUES ($1, $2) ON CONFLICT ("id") DO UPDATE SET "id" = "excluded"."id", "__ig_tags_sort__" = "excluded"."__ig_tags_sort__""#
  );
}

/// MySQL's clause names no target and assigns the same list.
#[cfg(feature = "mysql")]
#[test]
fn mysql_renders_the_companion_in_both_halves() {
  assert_eq!(
    upserted::<sqlx::MySql>(&reel(1, &["a"])),
    "INSERT INTO `reel` (`id`, `__ig_tags_sort__`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `id` = \
     VALUES(`id`), `__ig_tags_sort__` = VALUES(`__ig_tags_sort__`)"
  );
}

/// **THE SAME-TRANSACTION PIN.** The word adds NO statement: an `upsert` over this row renders the
/// parent, the edge delete and the edge insert — three, exactly what it rendered before the
/// companion existed — so the companion is inside whatever bracket the collection is inside, by
/// construction rather than by arrangement.
#[cfg(feature = "sqlite")]
#[test]
fn the_companion_adds_no_statement_to_the_transaction() {
  let row = reel(1, &["a", "b"]);
  let rendered: Vec<String> = statements::<ReelRow, sqlx::Sqlite>(
    &row,
    upsert_statement::<ReelRow, sqlx::Sqlite>(&row),
    true,
  )
  .expect("this row renders")
  .into_iter()
  .map(text)
  .collect();

  assert_eq!(rendered.len(), 3, "parent, edge delete, edge insert");
  assert!(
    rendered[0].contains("__ig_tags_sort__"),
    "the companion is in the PARENT statement: {}",
    rendered[0]
  );
  assert!(
    rendered[1..]
      .iter()
      .all(|statement| !statement.contains("__ig_tags_sort__")),
    "and in no other"
  );
}

/// The half no rendered statement can show.
#[cfg(feature = "sqlite")]
mod live {
  use sqlx::{Connection as _, Executor as _, sqlite::SqliteConnection};

  use super::{Reel, ReelRow, reel};
  use crate::{
    collection_sort_key,
    query::sql::{insert, upsert},
  };

  /// The two tables this row needs, created by hand — the schema lane's own suite is where the DDL
  /// is the subject, and here it is a fixture.
  async fn empty() -> SqliteConnection {
    let mut db = SqliteConnection::connect("sqlite::memory:")
      .await
      .expect("an in-memory database");
    for ddl in [
      r#"CREATE TABLE "reel" ("id" INTEGER PRIMARY KEY, "__ig_tags_sort__" BLOB NOT NULL)"#,
      r#"CREATE TABLE "reel_tags" ("id" INTEGER, "tags_ordinal" INTEGER, "tags" TEXT,
         PRIMARY KEY ("id", "tags_ordinal"))"#,
    ] {
      db.execute(ddl).await.expect("a table");
    }
    db
  }

  /// What the companion column holds for `id`.
  async fn stored(db: &mut SqliteConnection, id: i64) -> Vec<u8> {
    sqlx::query_scalar(r#"SELECT "__ig_tags_sort__" FROM "reel" WHERE "id" = ?"#)
      .bind(id)
      .fetch_one(db)
      .await
      .expect("the engine answers")
  }

  /// **The write puts the collection's own order in the column** — the encoding a reader can check
  /// against [`collection_sort_key`](crate::collection_sort_key) rather than against a byte pattern.
  #[tokio::test]
  async fn an_insert_stores_the_collections_order() {
    let mut db = empty().await;
    let row = reel(1, &["a", "b"]);

    insert::<ReelRow, sqlx::Sqlite, _>(&mut db, Reel::from(row.clone()))
      .await
      .expect("the row is written");

    assert_eq!(
      stored(&mut db, 1).await,
      collection_sort_key(&row.tags),
      "the column holds the order of the collection that was written"
    );
  }

  /// **AND AN UPSERT REPLACES IT.** The mutation this catches is a companion left out of the keyed
  /// replace's assignment list: the edge rows would be the new collection's and the companion the
  /// OLD one's, so every ordered scan would answer from a collection the row no longer holds.
  #[tokio::test]
  async fn an_upsert_replaces_the_companion_beside_the_edge_set() {
    let mut db = empty().await;

    upsert::<ReelRow, sqlx::Sqlite, _>(&mut db, Reel::from(reel(1, &["b"])))
      .await
      .expect("the first write");
    let first = stored(&mut db, 1).await;

    let second = reel(1, &["a"]);
    upsert::<ReelRow, sqlx::Sqlite, _>(&mut db, Reel::from(second.clone()))
      .await
      .expect("the second write");

    assert_ne!(stored(&mut db, 1).await, first, "the order was replaced");
    assert_eq!(
      stored(&mut db, 1).await,
      collection_sort_key(&second.tags),
      "and replaced with the NEW collection's order"
    );

    let tags: Vec<String> = sqlx::query_scalar(
      r#"SELECT "tags" FROM "reel_tags" WHERE "id" = ? ORDER BY "tags_ordinal""#,
    )
    .bind(1i64)
    .fetch_all(&mut db)
    .await
    .expect("the engine answers");
    assert_eq!(
      tags,
      ["a"],
      "the edge set was replaced too — the pair agrees"
    );
  }

  /// **The empty collection is a stored value**, not an absent one: one byte, which is what makes it
  /// least in the order and keeps a `NOT NULL` companion writable.
  #[tokio::test]
  async fn an_empty_collection_stores_one_byte() {
    let mut db = empty().await;

    insert::<ReelRow, sqlx::Sqlite, _>(&mut db, Reel::from(reel(1, &[])))
      .await
      .expect("a reel with no tags");

    assert_eq!(
      stored(&mut db, 1).await,
      collection_sort_key::<[&str; 0]>([])
    );
    assert_eq!(stored(&mut db, 1).await.len(), 1);
  }
}

impl From<ReelRow> for Reel {
  fn from(row: ReelRow) -> Self {
    Self {
      id: row.id,
      tags: row.tags,
    }
  }
}