#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqlType {
Varchar,
Text,
Integer,
BigInt,
Real,
Boolean,
Blob,
Timestamp,
Date,
Time,
Decimal,
Json,
}
impl SqlType {
#[must_use]
pub fn as_str(self) -> &'static str {
TYPE_MAP[self as usize]
}
}
type Row = [&'static str; 12];
#[allow(clippy::too_many_arguments)]
const fn row(
varchar: &'static str,
text: &'static str,
integer: &'static str,
bigint: &'static str,
real: &'static str,
boolean: &'static str,
blob: &'static str,
timestamp: &'static str,
date: &'static str,
time: &'static str,
decimal: &'static str,
json: &'static str,
) -> Row {
[
varchar, text, integer, bigint, real, boolean, blob, timestamp, date, time, decimal, json,
]
}
#[cfg(feature = "db-sqlite")]
const TYPE_MAP: Row = row(
"TEXT", "TEXT", "INTEGER", "INTEGER", "REAL", "BOOLEAN", "BLOB", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", );
#[cfg(feature = "db-postgres")]
const TYPE_MAP: Row = row(
"VARCHAR(255)", "TEXT", "INTEGER", "BIGINT", "DOUBLE PRECISION", "BOOLEAN", "BYTEA", "TIMESTAMPTZ(0)", "DATE", "TIMETZ", "NUMERIC(16,4)", "JSONB", );
#[cfg(feature = "db-mysql")]
const TYPE_MAP: Row = row(
"VARCHAR(255)", "TEXT", "INT", "BIGINT", "DOUBLE", "TINYINT(1)", "BLOB", "DATETIME", "DATE", "TIME", "DECIMAL(16,4)", "JSON", );
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_variants_have_non_empty_mapping() {
let variants = [
SqlType::Varchar,
SqlType::Text,
SqlType::Integer,
SqlType::BigInt,
SqlType::Real,
SqlType::Boolean,
SqlType::Blob,
SqlType::Timestamp,
SqlType::Date,
SqlType::Time,
SqlType::Decimal,
SqlType::Json,
];
for v in &variants {
assert!(!v.as_str().is_empty(), "{v:?} returned empty string");
}
}
}