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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! The word tables, asserted three ways: the WIDTH agreement in a `const`, the roster per dialect,
//! and — against a live SQLite — the one thing a table of strings cannot show.

use super::{StoredAs, spans};

/// Every shipped type's word count against the width its shape claims, in a `const` so a row that
/// disagreed would not compile.
///
/// It is the check the two `const`s exist to make possible: a composite whose `SEGMENTS` says five
/// columns and whose `WORDS` names four renders a `CREATE TABLE` narrower than the `INSERT` that
/// fills it, and both halves are written by hand.
mod widths {
  use super::spans;

  macro_rules! agree {
    ($db:ty { $( $ty:ty ),+ $(,)? }) => {
      $( const _: () = assert!(spans::<$ty, $db>()); )+
    };
  }

  #[cfg(feature = "sqlite")]
  agree!(sqlx::Sqlite {
    bool, i8, i16, i32, i64, u8, u16, u32, f32, f64,
    String, smol_bytes::Utf8Bytes, Vec<u8>,
    crate::BeU64, crate::BeU128, crate::BeI128, crate::Count,
    uuid::Uuid, Option<uuid::Uuid>,
    core::num::NonZeroI32, core::num::NonZeroU32,
    crate::Timestamp, crate::TimeRange,
    Option<i64>, Option<crate::Timestamp>, &i64,
  });

  #[cfg(feature = "postgres")]
  agree!(sqlx::Postgres {
    bool, i8, i16, i32, i64, u8, u16, u32, f32, f64,
    String, smol_bytes::Utf8Bytes, Vec<u8>,
    crate::BeU64, crate::BeU128, crate::BeI128, crate::Count,
    uuid::Uuid,
    core::time::Duration, sqlx::postgres::types::PgInterval,
    crate::Timestamp, crate::TimeRange,
  });

  #[cfg(feature = "mysql")]
  agree!(sqlx::MySql {
    bool, i8, i16, i32, i64, u8, u16, u32, u64, f32, f64,
    String, smol_bytes::Utf8Bytes, Vec<u8>,
    crate::BeU128, crate::BeI128, crate::Count,
    uuid::Uuid,
    core::num::NonZeroU64,
    crate::Timestamp, crate::TimeRange,
  });

  // The media citizens ride `mediatime`, which is the gate their names are exported behind.
  #[cfg(all(feature = "sqlite", feature = "mediatime"))]
  agree!(sqlx::Sqlite {
    crate::Timebase, crate::MediaTimestamp, crate::MediaTimeRange
  });
  #[cfg(all(feature = "postgres", feature = "mediatime"))]
  agree!(sqlx::Postgres {
    crate::Timebase, crate::MediaTimestamp, crate::MediaTimeRange
  });
  #[cfg(all(feature = "mysql", feature = "mediatime"))]
  agree!(sqlx::MySql {
    crate::Timebase, crate::MediaTimestamp, crate::MediaTimeRange
  });

  // The language citizens ride `derive`, likewise.
  #[cfg(all(feature = "sqlite", feature = "derive"))]
  agree!(sqlx::Sqlite {
    crate::LanguageId, crate::DetectedLanguage,
    crate::Language, crate::Region, crate::ScriptSubtag
  });
  #[cfg(all(feature = "postgres", feature = "derive"))]
  agree!(sqlx::Postgres { crate::LanguageId, crate::DetectedLanguage });
  #[cfg(all(feature = "mysql", feature = "derive"))]
  agree!(sqlx::MySql { crate::LanguageId, crate::DetectedLanguage });
}

/// **The framework's OWN three columns, held against the rows for the types they are stored at.**
///
/// A dialect states `ORDINAL`, `DISCRIMINANT` and `SORT_KEY` for itself — they are columns the
/// framework mints rather than columns a declaration names, so the word is the DIALECT's answer and
/// not a type's. But each is stored AS something: an ordinal is bound as an `i64`, a discriminant is
/// text, a companion column is the collection's sort-key bytes. Two files therefore say the same
/// thing about one column, and this is what keeps them saying it.
///
/// # The comparison is the AUDIT's, which is why it is not `assert_eq!`
///
/// [`str::eq_ignore_ascii_case`] is what `audit_columns` uses, so it is the only agreement that has
/// to hold — and it is not the same as string equality here: MySQL states `BIGINT` where this
/// module's row says `bigint`, and both are the catalogue's `bigint`. Asserting exact case would
/// assert a convention the system does not have, and would fail on a difference no database can see.
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
fn dialect_columns_agree<Db>()
where
  Db: crate::query::sql::AsyncSchemaDialect,
  i64: StoredAs<Db>,
  String: StoredAs<Db>,
  Vec<u8>: StoredAs<Db>,
{
  use crate::query::sql::AsyncSchemaDialect;

  for (mine, dialect_s, seat) in [
    (
      <i64 as StoredAs<Db>>::WORDS,
      <Db as AsyncSchemaDialect>::ORDINAL,
      "an ordinal is bound as an i64",
    ),
    (
      <String as StoredAs<Db>>::WORDS,
      <Db as AsyncSchemaDialect>::DISCRIMINANT,
      "a discriminant is the word, and the word is text",
    ),
    (
      <Vec<u8> as StoredAs<Db>>::WORDS,
      <Db as AsyncSchemaDialect>::SORT_KEY,
      "a companion column is the collection's sort-key bytes",
    ),
  ] {
    assert_eq!(mine.len(), 1, "{seat}: one column");
    assert!(
      mine[0].eq_ignore_ascii_case(dialect_s),
      "{seat}: the type says {}, the dialect says {dialect_s}",
      mine[0]
    );
  }
}

/// SQLite's roster: the storage classes, echoed by `pragma_table_info` exactly as declared.
#[cfg(feature = "sqlite")]
mod sqlite {
  use super::*;

  #[test]
  fn scalars() {
    type Db = sqlx::Sqlite;
    assert_eq!(<bool as StoredAs<Db>>::WORDS, ["INTEGER"]);
    assert_eq!(<i8 as StoredAs<Db>>::WORDS, ["INTEGER"]);
    assert_eq!(<i64 as StoredAs<Db>>::WORDS, ["INTEGER"]);
    assert_eq!(<u32 as StoredAs<Db>>::WORDS, ["INTEGER"]);
    assert_eq!(<f32 as StoredAs<Db>>::WORDS, ["REAL"]);
    assert_eq!(<f64 as StoredAs<Db>>::WORDS, ["REAL"]);
    assert_eq!(<String as StoredAs<Db>>::WORDS, ["TEXT"]);
    assert_eq!(<smol_bytes::Utf8Bytes as StoredAs<Db>>::WORDS, ["TEXT"]);
    assert_eq!(<Vec<u8> as StoredAs<Db>>::WORDS, ["BLOB"]);
    assert_eq!(<crate::BeU64 as StoredAs<Db>>::WORDS, ["BLOB"]);
    assert_eq!(<crate::Count as StoredAs<Db>>::WORDS, ["INTEGER"]);
    // Sixteen bytes under BLOB affinity, which is the class sqlx binds a `Uuid` into and the one
    // that makes the stored value compare bytewise.
    assert_eq!(<uuid::Uuid as StoredAs<Db>>::WORDS, ["BLOB"]);
  }

  #[test]
  fn the_dialect_s_own_columns_agree_with_the_types_they_hold() {
    super::dialect_columns_agree::<sqlx::Sqlite>();
  }

  #[test]
  fn composites() {
    type Db = sqlx::Sqlite;
    assert_eq!(
      <crate::Timestamp as StoredAs<Db>>::WORDS,
      ["INTEGER", "INTEGER"]
    );
    assert_eq!(
      <crate::TimeRange as StoredAs<Db>>::WORDS,
      ["INTEGER", "INTEGER", "INTEGER", "INTEGER"]
    );
  }

  #[cfg(feature = "mediatime")]
  #[test]
  fn the_media_citizens() {
    type Db = sqlx::Sqlite;
    assert_eq!(
      <crate::Timebase as StoredAs<Db>>::WORDS,
      ["INTEGER", "INTEGER"]
    );
    assert_eq!(
      <crate::MediaTimestamp as StoredAs<Db>>::WORDS,
      ["INTEGER", "INTEGER", "INTEGER", "INTEGER"]
    );
    assert_eq!(
      <crate::MediaTimeRange as StoredAs<Db>>::WORDS,
      [
        "INTEGER", "INTEGER", "INTEGER", "INTEGER", "INTEGER", "INTEGER"
      ]
    );
  }

  #[cfg(feature = "derive")]
  #[test]
  fn the_language_citizens() {
    type Db = sqlx::Sqlite;
    assert_eq!(
      <crate::LanguageId as StoredAs<Db>>::WORDS,
      ["TEXT", "TEXT", "TEXT", "TEXT"]
    );
    assert_eq!(
      <crate::DetectedLanguage as StoredAs<Db>>::WORDS,
      ["TEXT", "TEXT", "TEXT", "TEXT", "REAL"]
    );
    assert_eq!(<crate::Language as StoredAs<Db>>::WORDS, ["TEXT"]);
  }

  /// **THE PROOF A TABLE OF STRINGS CANNOT GIVE**: every SQLite word is declared into a real
  /// `CREATE TABLE`, and `pragma_table_info` is asked what it now holds.
  ///
  /// It tests both halves of the spelling law at once for the one dialect this repository can reach.
  /// That the statement EXECUTES is the first reader satisfied — a word this crate invented would be
  /// a syntax error here — and that the echo compares equal under the audit's own
  /// [`str::eq_ignore_ascii_case`] is the second. SQLite echoes the declaration verbatim, so the
  /// second half can only fail if a word is not a word at all, which is exactly the failure worth
  /// catching.
  #[tokio::test]
  async fn declared_words_create_and_echo() {
    use crate::query::sql::AsyncSchemaDialect;
    use sqlx::Connection as _;

    // MUTATED ONLY WHERE `mediatime` IS ON — the extension below is that word's, and the roster is
    // otherwise built once and read. The allow is scoped to the build that makes the binding honest
    // rather than written flat, because a `mut` this lane does need is a `mut` worth keeping.
    #[cfg_attr(not(feature = "mediatime"), allow(unused_mut))]
    let mut roster: Vec<(&str, &[&str])> = vec![
      ("c_bool", <bool as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_i64", <i64 as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_u32", <u32 as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_f64", <f64 as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_text", <String as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_blob", <Vec<u8> as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_be", <crate::BeU64 as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_count", <crate::Count as StoredAs<sqlx::Sqlite>>::WORDS),
      ("c_ts", <crate::Timestamp as StoredAs<sqlx::Sqlite>>::WORDS),
      (
        "c_range",
        <crate::TimeRange as StoredAs<sqlx::Sqlite>>::WORDS,
      ),
    ];
    #[cfg(feature = "mediatime")]
    roster.extend([
      ("c_tb", <crate::Timebase as StoredAs<sqlx::Sqlite>>::WORDS),
      (
        "c_mts",
        <crate::MediaTimestamp as StoredAs<sqlx::Sqlite>>::WORDS,
      ),
      (
        "c_mtr",
        <crate::MediaTimeRange as StoredAs<sqlx::Sqlite>>::WORDS,
      ),
    ]);
    #[cfg(feature = "derive")]
    roster.extend([
      (
        "c_lang",
        <crate::LanguageId as StoredAs<sqlx::Sqlite>>::WORDS,
      ),
      (
        "c_heard",
        <crate::DetectedLanguage as StoredAs<sqlx::Sqlite>>::WORDS,
      ),
    ]);

    // One column per WORD, named for the seat it came from — a composite contributing as many as it
    // is wide, which is the arrangement a real `CREATE TABLE` has.
    let mut declared: Vec<(String, &'static str)> = Vec::new();
    for (seat, words) in &roster {
      for (at, word) in words.iter().enumerate() {
        declared.push((format!("{seat}_{at}"), *word));
      }
    }
    let body = declared
      .iter()
      .map(|(name, word)| format!("\"{name}\" {word}"))
      .collect::<Vec<_>>()
      .join(", ");

    let mut db = sqlx::SqliteConnection::connect("sqlite::memory:")
      .await
      .expect("an in-memory database opens");
    // Through the dialect's OWN door — the one a `Create` walk sends its statements down — so what
    // is proved legal is proved legal by the party that would send it.
    let create = format!("CREATE TABLE roster ({body})");
    <sqlx::Sqlite as AsyncSchemaDialect>::execute(&create, &mut db)
      .await
      .expect("every declared word is legal SQLite DDL");

    let stored: Vec<(String, String)> =
      sqlx::query_as("SELECT name, type FROM pragma_table_info('roster')")
        .fetch_all(&mut db)
        .await
        .expect("the catalogue answers");

    assert_eq!(stored.len(), declared.len(), "one column per word");
    for (name, word) in &declared {
      let (_, echoed) = stored
        .iter()
        .find(|(stored, _)| stored == name)
        .unwrap_or_else(|| panic!("the catalogue reports {name}"));
      // The audit's own comparison, on the audit's own terms.
      assert!(
        echoed.eq_ignore_ascii_case(word),
        "{name}: declared {word}, catalogue reports {echoed}"
      );
    }
  }

  /// A value bound through [`Carrier`](crate::Carrier) into a column declared through [`StoredAs`]
  /// comes back through [`Assemble`](crate::Assemble) — the three faces of one column, on one row.
  ///
  /// This is what the word being merely LEGAL would not prove. `bool` is declared `INTEGER` rather
  /// than `BOOLEAN` and the three `time` types `TEXT` rather than `DATETIME`, both on the argument
  /// that the word should name the storage class sqlx actually writes; if that argument were wrong
  /// the round trip is where it would show.
  #[tokio::test]
  async fn a_declared_column_carries_and_reads_back() {
    use crate::query::sql::AsyncSchemaDialect;
    use sqlx::{Connection as _, Row as _};

    let mut db = sqlx::SqliteConnection::connect("sqlite::memory:")
      .await
      .expect("an in-memory database opens");
    let create = format!(
      "CREATE TABLE trip (b {}, i {}, f {}, t {}, v {})",
      <bool as StoredAs<sqlx::Sqlite>>::WORDS[0],
      <i64 as StoredAs<sqlx::Sqlite>>::WORDS[0],
      <f64 as StoredAs<sqlx::Sqlite>>::WORDS[0],
      <String as StoredAs<sqlx::Sqlite>>::WORDS[0],
      <Vec<u8> as StoredAs<sqlx::Sqlite>>::WORDS[0],
    );
    <sqlx::Sqlite as AsyncSchemaDialect>::execute(&create, &mut db)
      .await
      .expect("the declaration is legal");

    let mut qb = sqlx::QueryBuilder::<sqlx::Sqlite>::new("INSERT INTO trip VALUES (");
    crate::Carrier::push_bind(&true, &mut qb);
    qb.push(", ");
    crate::Carrier::push_bind(&-7i64, &mut qb);
    qb.push(", ");
    crate::Carrier::push_bind(&1.5f64, &mut qb);
    qb.push(", ");
    crate::Carrier::push_bind(&String::from("hi"), &mut qb);
    qb.push(", ");
    crate::Carrier::push_bind(&vec![1u8, 2, 3], &mut qb);
    qb.push(")");
    qb.build()
      .execute(&mut db)
      .await
      .expect("the bind fits the columns it declared");

    let row = sqlx::query("SELECT b, i, f, t, v FROM trip")
      .fetch_one(&mut db)
      .await
      .expect("the row reads");
    assert!(row.get::<bool, _>(0), "a bool survives an INTEGER column");
    assert_eq!(row.get::<i64, _>(1), -7);
    assert!((row.get::<f64, _>(2) - 1.5).abs() < f64::EPSILON);
    assert_eq!(row.get::<String, _>(3), "hi");
    assert_eq!(row.get::<Vec<u8>, _>(4), vec![1, 2, 3]);
  }
}

/// PostgreSQL's roster: `information_schema.columns.data_type`'s own spellings.
///
/// # These are GOLDENS, and the catalogue they are golden against is named
///
/// This repository has no live PostgreSQL lane — the crate's async tests reach SQLite alone — so
/// what holds these words is the assertion below plus the measurement that produced them: each was
/// read back off `information_schema.columns.data_type` on PostgreSQL 16 after declaring a column
/// with it. The failure this guards is a row silently respelled to a legal alias, which is the one
/// mistake a reader is most likely to make here: `int8`, `float8`, `bool` and `timestamptz` all
/// BUILD and all report as something else.
#[cfg(feature = "postgres")]
mod postgres {
  use super::*;

  #[test]
  fn scalars() {
    type Db = sqlx::Postgres;
    assert_eq!(<bool as StoredAs<Db>>::WORDS, ["boolean"]);
    assert_eq!(<i16 as StoredAs<Db>>::WORDS, ["smallint"]);
    assert_eq!(<i32 as StoredAs<Db>>::WORDS, ["integer"]);
    assert_eq!(<i64 as StoredAs<Db>>::WORDS, ["bigint"]);
    assert_eq!(<f32 as StoredAs<Db>>::WORDS, ["real"]);
    assert_eq!(<f64 as StoredAs<Db>>::WORDS, ["double precision"]);
    assert_eq!(<String as StoredAs<Db>>::WORDS, ["text"]);
    assert_eq!(<Vec<u8> as StoredAs<Db>>::WORDS, ["bytea"]);
    assert_eq!(<crate::BeU128 as StoredAs<Db>>::WORDS, ["bytea"]);
    assert_eq!(<crate::Count as StoredAs<Db>>::WORDS, ["bigint"]);
    assert_eq!(<core::time::Duration as StoredAs<Db>>::WORDS, ["interval"]);
    // The one dialect with a TYPE of its own for the value — `data_type` and `udt_name` both answer
    // `uuid`, measured on PostgreSQL 16 — so the word is that type and not the `bytea` a
    // sixteen-byte column would otherwise take.
    assert_eq!(<uuid::Uuid as StoredAs<Db>>::WORDS, ["uuid"]);
    assert_ne!(
      <uuid::Uuid as StoredAs<Db>>::WORDS,
      <Vec<u8> as StoredAs<Db>>::WORDS,
      "a uuid is not a byte string here, and the catalogue would say so"
    );
  }

  /// **The widened four**: a word follows the CARRIER and not the Rust type, and PostgreSQL is where
  /// the two part company — it has no one-byte and no unsigned integer, so four widths bind into
  /// wider columns and are declared as those.
  #[test]
  fn the_widened_integers_are_declared_at_the_column_they_reach() {
    type Db = sqlx::Postgres;
    assert_eq!(<i8 as StoredAs<Db>>::WORDS, ["smallint"]);
    assert_eq!(<u8 as StoredAs<Db>>::WORDS, ["smallint"]);
    assert_eq!(<u16 as StoredAs<Db>>::WORDS, ["integer"]);
    assert_eq!(<u32 as StoredAs<Db>>::WORDS, ["bigint"], "not smallint");
  }

  /// **THE SPELLING LAW, stated as an assertion**: not one row is spelled the way the DRIVER names
  /// the same type.
  ///
  /// sqlx answers `INT8`, `FLOAT8`, `BOOL` and `TIMESTAMPTZ` for these four, each a legal
  /// `CREATE TABLE` input and each a word `information_schema` never reports. Taking the driver's
  /// vocabulary would have compiled, created the right columns, and failed the drift audit — which
  /// is why the difference is asserted rather than left as a comment.
  #[test]
  fn no_row_takes_the_driver_s_alias() {
    type Db = sqlx::Postgres;
    for (ours, alias) in [
      (<i64 as StoredAs<Db>>::WORDS[0], "INT8"),
      (<f64 as StoredAs<Db>>::WORDS[0], "FLOAT8"),
      (<i16 as StoredAs<Db>>::WORDS[0], "INT2"),
      (<i32 as StoredAs<Db>>::WORDS[0], "INT4"),
      (<f32 as StoredAs<Db>>::WORDS[0], "FLOAT4"),
      (<bool as StoredAs<Db>>::WORDS[0], "BOOL"),
    ] {
      assert!(
        !ours.eq_ignore_ascii_case(alias),
        "{ours} is the catalogue's word; {alias} is the driver's"
      );
    }
  }

  #[test]
  fn composites() {
    type Db = sqlx::Postgres;
    assert_eq!(
      <crate::Timestamp as StoredAs<Db>>::WORDS,
      ["bigint", "integer"]
    );
    assert_eq!(
      <crate::TimeRange as StoredAs<Db>>::WORDS,
      ["bigint", "integer", "bigint", "integer"]
    );
  }

  /// **The order is the SEGMENTS' and not the struct's** — a range's timebase sits BETWEEN its
  /// extents and its derived nanosecond columns, which is the arrangement this asserts.
  #[cfg(feature = "mediatime")]
  #[test]
  fn the_media_citizens() {
    type Db = sqlx::Postgres;
    assert_eq!(
      <crate::Timebase as StoredAs<Db>>::WORDS,
      ["integer", "integer"]
    );
    assert_eq!(
      <crate::MediaTimestamp as StoredAs<Db>>::WORDS,
      ["bigint", "integer", "integer", "bigint"]
    );
    assert_eq!(
      <crate::MediaTimeRange as StoredAs<Db>>::WORDS,
      ["bigint", "bigint", "integer", "integer", "bigint", "bigint"]
    );
  }

  #[cfg(feature = "derive")]
  #[test]
  fn the_language_citizens() {
    type Db = sqlx::Postgres;
    assert_eq!(
      <crate::LanguageId as StoredAs<Db>>::WORDS,
      ["text", "text", "text", "text"]
    );
    assert_eq!(
      <crate::DetectedLanguage as StoredAs<Db>>::WORDS,
      ["text", "text", "text", "text", "real"]
    );
  }

  #[cfg(feature = "time")]
  #[test]
  fn the_temporal_three_take_the_standard_s_long_forms() {
    type Db = sqlx::Postgres;
    assert_eq!(
      <time::OffsetDateTime as StoredAs<Db>>::WORDS,
      ["timestamp with time zone"]
    );
    assert_eq!(<time::Date as StoredAs<Db>>::WORDS, ["date"]);
    assert_eq!(
      <time::Time as StoredAs<Db>>::WORDS,
      ["time without time zone"]
    );
  }

  /// The dialect's own three columns against the types they hold, as the SQLite lane does.
  #[test]
  fn the_dialect_s_own_columns_agree_with_the_types_they_hold() {
    super::dialect_columns_agree::<sqlx::Postgres>();
  }
}

/// MySQL's roster: `COLUMN_TYPE`'s spellings, width and attributes included.
///
/// Goldens on the same terms the PostgreSQL module states, measured the same way — each word read
/// back off `information_schema.COLUMNS.COLUMN_TYPE` on MySQL 8.4 after declaring a column with it.
#[cfg(feature = "mysql")]
mod mysql {
  use super::*;

  #[test]
  fn scalars() {
    type Db = sqlx::MySql;
    assert_eq!(<i8 as StoredAs<Db>>::WORDS, ["tinyint"]);
    assert_eq!(<i16 as StoredAs<Db>>::WORDS, ["smallint"]);
    assert_eq!(<i32 as StoredAs<Db>>::WORDS, ["int"]);
    assert_eq!(<i64 as StoredAs<Db>>::WORDS, ["bigint"]);
    assert_eq!(<f32 as StoredAs<Db>>::WORDS, ["float"]);
    assert_eq!(<f64 as StoredAs<Db>>::WORDS, ["double"]);
    assert_eq!(<String as StoredAs<Db>>::WORDS, ["varchar(255)"]);
    assert_eq!(<Vec<u8> as StoredAs<Db>>::WORDS, ["blob"]);
    assert_eq!(<crate::BeI128 as StoredAs<Db>>::WORDS, ["blob"]);
  }

  /// **`binary(16)` CARRIES ITS WIDTH, for `tinyint(1)`'s own reason** — MySQL's audit reads
  /// `COLUMN_TYPE`, so a row saying `binary` would create a `binary(1)` and then disagree with its
  /// own catalogue. Measured on MySQL 8.4: a column created `binary(16)` reports `binary(16)`, where
  /// `DATA_TYPE` alone answers `binary` and loses the number.
  ///
  /// Sixteen is not a taste either. sqlx encodes the sixteen raw bytes and its decode REFUSES any
  /// other length by name, so a narrower or wider column is a read that fails on the row.
  #[test]
  fn a_uuid_is_the_fixed_width_binary_the_bytes_need() {
    type Db = sqlx::MySql;
    assert_eq!(<uuid::Uuid as StoredAs<Db>>::WORDS, ["binary(16)"]);
    assert_ne!(<uuid::Uuid as StoredAs<Db>>::WORDS, ["binary"], "the width");
    assert_ne!(
      <uuid::Uuid as StoredAs<Db>>::WORDS,
      <Vec<u8> as StoredAs<Db>>::WORDS,
      "a fixed-width value is not a blob"
    );
  }

  /// **`bool` IS `tinyint(1)`, AND THIS IS THE ROW MOST LIKELY TO BE "CORRECTED".**
  ///
  /// MySQL has no boolean type — `BOOLEAN` is a spelling of `TINYINT(1)` — and `COLUMN_TYPE` answers
  /// with what the column became. So the obvious word builds the right column and then fails its own
  /// audit, which is the whole failure mode the spelling law names.
  #[test]
  fn bool_is_the_tinyint_the_column_actually_becomes() {
    assert_eq!(<bool as StoredAs<sqlx::MySql>>::WORDS, ["tinyint(1)"]);
    assert_ne!(<bool as StoredAs<sqlx::MySql>>::WORDS, ["boolean"]);
  }

  /// **UNSIGNEDNESS IS PART OF THE WORD**, `COLUMN_TYPE` carrying the attribute rather than a
  /// separate catalogue column — and MySQL is the one dialect here whose column holds a `u64` whole.
  #[test]
  fn the_unsigned_four_carry_the_attribute() {
    type Db = sqlx::MySql;
    assert_eq!(<u8 as StoredAs<Db>>::WORDS, ["tinyint unsigned"]);
    assert_eq!(<u16 as StoredAs<Db>>::WORDS, ["smallint unsigned"]);
    assert_eq!(<u32 as StoredAs<Db>>::WORDS, ["int unsigned"]);
    assert_eq!(<u64 as StoredAs<Db>>::WORDS, ["bigint unsigned"]);
    assert_eq!(
      <crate::Count as StoredAs<Db>>::WORDS,
      <u64 as StoredAs<Db>>::WORDS,
      "the cardinality rides the one column that holds it whole"
    );
  }

  /// **A TEMPORAL COLUMN DECLARES ITS PRECISION OR SILENTLY LOSES IT.** A plain `TIMESTAMP` stores
  /// whole seconds, so `22:13:20.123456` comes back `22:13:20`; sqlx binds microseconds, so the
  /// word carries `(6)` and the catalogue reports it with the word.
  #[cfg(feature = "time")]
  #[test]
  fn the_temporal_words_carry_their_precision() {
    type Db = sqlx::MySql;
    assert_eq!(
      <time::OffsetDateTime as StoredAs<Db>>::WORDS,
      ["timestamp(6)"]
    );
    assert_eq!(<time::Time as StoredAs<Db>>::WORDS, ["time(6)"]);
    assert_eq!(<time::Date as StoredAs<Db>>::WORDS, ["date"], "no sub-day");
  }

  #[test]
  fn composites() {
    type Db = sqlx::MySql;
    assert_eq!(<crate::Timestamp as StoredAs<Db>>::WORDS, ["bigint", "int"]);
    assert_eq!(
      <crate::TimeRange as StoredAs<Db>>::WORDS,
      ["bigint", "int", "bigint", "int"]
    );
  }

  #[cfg(feature = "mediatime")]
  #[test]
  fn the_media_citizens() {
    type Db = sqlx::MySql;
    assert_eq!(<crate::Timebase as StoredAs<Db>>::WORDS, ["int", "int"]);
    assert_eq!(
      <crate::MediaTimeRange as StoredAs<Db>>::WORDS,
      ["bigint", "bigint", "int", "int", "bigint", "bigint"]
    );
  }

  #[cfg(feature = "derive")]
  #[test]
  fn the_language_citizens() {
    assert_eq!(
      <crate::LanguageId as StoredAs<sqlx::MySql>>::WORDS,
      [
        "varchar(255)",
        "varchar(255)",
        "varchar(255)",
        "varchar(255)"
      ]
    );
  }

  /// The dialect's own three columns against the types they hold, as the other two lanes do.
  #[test]
  fn the_dialect_s_own_columns_agree_with_the_types_they_hold() {
    super::dialect_columns_agree::<sqlx::MySql>();
  }
}

/// The delegating wrappers, which are one row apiece and whose whole content is that they add
/// nothing.
#[cfg(feature = "postgres")]
mod delegation {
  use super::*;

  #[test]
  fn a_wrapper_is_declared_as_what_it_wraps() {
    type Db = sqlx::Postgres;
    assert_eq!(<Option<i64> as StoredAs<Db>>::WORDS, ["bigint"]);
    assert_eq!(<&i64 as StoredAs<Db>>::WORDS, ["bigint"]);
    assert_eq!(<core::num::NonZeroI32 as StoredAs<Db>>::WORDS, ["integer"]);
    assert_eq!(<crate::Score as StoredAs<Db>>::WORDS, ["real"]);
    assert_eq!(
      <crate::Confidence<f64> as StoredAs<Db>>::WORDS,
      ["double precision"]
    );
  }

  /// **A NULLABLE COMPOSITE IS AS WIDE AS THE COMPOSITE.** Nullability decides what a column may
  /// hold, not how many there are, so an optional instant is still two words — the same claim
  /// [`Carrier`](crate::Carrier)'s `Option` row makes when it renders one `NULL` per column.
  #[test]
  fn an_optional_composite_keeps_its_width() {
    assert_eq!(
      <Option<crate::Timestamp> as StoredAs<sqlx::Postgres>>::WORDS,
      <crate::Timestamp as StoredAs<sqlx::Postgres>>::WORDS
    );
  }

  /// The typed id reaches the column its id does, the entity marker reaching none.
  #[test]
  fn a_typed_id_is_declared_as_its_id() {
    struct Media;
    assert_eq!(
      <crate::TypedId<i64, Media> as StoredAs<sqlx::Postgres>>::WORDS,
      ["bigint"]
    );
  }
}

/// **THE PATTERN TAKES THE HOUSE'S TEXT WORD ON EVERY DIALECT**, which is the whole of what the
/// storage face claims: a pattern column is a text column, and nothing about it is a second kind of
/// text.
///
/// The MySQL row carries the `varchar(255)` ceiling with it, deliberately and visibly: it is the
/// word `String` and `smol_bytes::Utf8Bytes` take, and it is a fact about this table's convention
/// rather than about patterns. Asserted so that a build changing the house's text word moves this
/// row with it instead of leaving a pattern column behind on a word nothing else uses.
#[cfg(feature = "regex")]
#[test]
fn a_pattern_is_declared_as_the_house_s_own_text_word() {
  #[cfg(feature = "sqlite")]
  {
    assert_eq!(<regex::Regex as StoredAs<sqlx::Sqlite>>::WORDS, ["TEXT"]);
    assert_eq!(
      <regex::Regex as StoredAs<sqlx::Sqlite>>::WORDS,
      <String as StoredAs<sqlx::Sqlite>>::WORDS,
      "a pattern is stored exactly as text is"
    );
  }
  #[cfg(feature = "postgres")]
  {
    assert_eq!(<regex::Regex as StoredAs<sqlx::Postgres>>::WORDS, ["text"]);
  }
  #[cfg(feature = "mysql")]
  {
    assert_eq!(
      <regex::Regex as StoredAs<sqlx::MySql>>::WORDS,
      ["varchar(255)"],
      "the house's own text word, ceiling and all"
    );
    assert_eq!(
      <regex::Regex as StoredAs<sqlx::MySql>>::WORDS,
      <smol_bytes::Utf8Bytes as StoredAs<sqlx::MySql>>::WORDS,
      "and it moves with the house's, not on its own"
    );
  }
}