use super::{StoredAs, spans};
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,
});
#[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
});
#[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 });
}
#[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]
);
}
}
#[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"]);
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"]);
}
#[tokio::test]
async fn declared_words_create_and_echo() {
use crate::query::sql::AsyncSchemaDialect;
use sqlx::Connection as _;
#[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,
),
]);
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");
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}"));
assert!(
echoed.eq_ignore_ascii_case(word),
"{name}: declared {word}, catalogue reports {echoed}"
);
}
}
#[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]);
}
}
#[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"]);
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"
);
}
#[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");
}
#[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"]
);
}
#[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"]
);
}
#[test]
fn the_dialect_s_own_columns_agree_with_the_types_they_hold() {
super::dialect_columns_agree::<sqlx::Postgres>();
}
}
#[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"]);
}
#[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"
);
}
#[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"]);
}
#[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"
);
}
#[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)"
]
);
}
#[test]
fn the_dialect_s_own_columns_agree_with_the_types_they_hold() {
super::dialect_columns_agree::<sqlx::MySql>();
}
}
#[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"]
);
}
#[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
);
}
#[test]
fn a_typed_id_is_declared_as_its_id() {
struct Media;
assert_eq!(
<crate::TypedId<i64, Media> as StoredAs<sqlx::Postgres>>::WORDS,
["bigint"]
);
}
}
#[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"
);
}
}