Skip to main content

rust_ef_mysql/
type_conversion.rs

1use rust_ef::provider::DbValue;
2
3/// Converts `DbValue` parameters into `sqlx` bindings for MySQL.
4///
5/// Native chrono/uuid/decimal variants are collapsed to their canonical
6/// string form. MySQL's DATETIME/CHAR(36)/DECIMAL columns accept string
7/// input and preserve precision (matching v1.0 behavior). This keeps the
8/// write path symmetric with the read-back path in `connection.rs`, which
9/// decodes everything to `String` via `IFromRow::from_row`.
10pub fn build_mysql_query<'q>(
11    sql: &'q str,
12    params: &'q [DbValue],
13) -> sqlx::query::Query<'q, sqlx::MySql, sqlx::mysql::MySqlArguments> {
14    let mut query = sqlx::query::<sqlx::MySql>(sql);
15    for param in params {
16        query = match param {
17            DbValue::Null => query.bind(None::<String>),
18            DbValue::Bool(v) => query.bind(*v),
19            DbValue::I16(v) => query.bind(*v),
20            DbValue::I32(v) => query.bind(*v),
21            DbValue::I64(v) => query.bind(*v),
22            DbValue::F32(v) => query.bind(*v),
23            DbValue::F64(v) => query.bind(*v),
24            DbValue::String(v) => query.bind(v.as_str()),
25            DbValue::Bytes(v) => query.bind(v.as_slice()),
26            // Collapse native chrono variants to canonical string form.
27            DbValue::DateTime(dt) => query.bind(dt.to_rfc3339()),
28            DbValue::NaiveDateTime(ndt) => query.bind(ndt.to_string()),
29            DbValue::NaiveDate(nd) => query.bind(nd.to_string()),
30            // UUID hyphenated lowercase form — fits CHAR(36).
31            DbValue::Uuid(u) => query.bind(u.to_string()),
32            // Decimal canonical string form — fits DECIMAL(38,18).
33            DbValue::Decimal(d) => query.bind(d.to_string()),
34        };
35    }
36    query
37}