rust_ef_sqlite/type_conversion.rs
1use rust_ef::provider::DbValue;
2
3/// Converts `DbValue` parameters into boxed `ToSql` trait objects for
4/// `rusqlite`.
5///
6/// Native chrono/uuid/decimal variants are collapsed to their canonical
7/// string form because SQLite has no native TIMESTAMPTZ/UUID type — TEXT is
8/// the correct storage (matching v1.0 behavior). This also keeps the
9/// round-trip path symmetric with the read-back path in `connection.rs`,
10/// which decodes everything to `String` via `IFromRow::from_row`.
11pub fn to_rusqlite_params(params: &[DbValue]) -> Vec<Box<dyn rusqlite::types::ToSql>> {
12 params
13 .iter()
14 .map(|v| match v {
15 DbValue::Null => Box::new(None::<String>) as Box<dyn rusqlite::types::ToSql>,
16 DbValue::Bool(b) => Box::new(*b),
17 DbValue::I16(n) => Box::new(*n),
18 DbValue::I32(n) => Box::new(*n),
19 DbValue::I64(n) => Box::new(*n),
20 DbValue::F32(n) => Box::new(*n as f64),
21 DbValue::F64(n) => Box::new(*n),
22 DbValue::String(s) => Box::new(s.clone()),
23 DbValue::Bytes(b) => Box::new(b.clone()),
24 // Collapse native chrono variants to canonical string form.
25 // RFC 3339 for DateTime<Utc>, "YYYY-MM-DD HH:MM:SS" for
26 // NaiveDateTime, "YYYY-MM-DD" for NaiveDate.
27 DbValue::DateTime(dt) => Box::new(dt.to_rfc3339()),
28 DbValue::NaiveDateTime(ndt) => Box::new(ndt.to_string()),
29 DbValue::NaiveDate(nd) => Box::new(nd.to_string()),
30 // UUID hyphenated lowercase form.
31 DbValue::Uuid(u) => Box::new(u.to_string()),
32 // Decimal canonical string form (preserves full precision).
33 DbValue::Decimal(d) => Box::new(d.to_string()),
34 })
35 .collect()
36}