diesel_async/mysql_like/
serialize.rs1use core::marker::PhantomData;
2use diesel::QueryResult;
3use diesel::data_types::MysqlTime;
4use diesel::mysql_like::MysqlType;
5use diesel::mysql_like::{MysqlLikeBackend, MysqlValue};
6use mysql_async::{Params, Value};
7use std::convert::TryInto;
8
9pub(super) struct ToSqlHelper<DB: MysqlLikeBackend> {
10 pub(super) metadata: Vec<MysqlType>,
11 pub(super) binds: Vec<Option<Vec<u8>>>,
12 pub(super) _phantom: PhantomData<DB>,
13}
14
15fn to_value<DB: MysqlLikeBackend>(
16 (metadata, bind): (MysqlType, Option<Vec<u8>>),
17) -> QueryResult<Value> {
18 let cast_helper = |e| diesel::result::Error::SerializationError(Box::new(e));
19 let v = match bind {
20 Some(bind) => match metadata {
21 MysqlType::Tiny => Value::Int(i8::from_be_bytes([bind[0]]) as i64),
22 MysqlType::Short => Value::Int(i16::from_ne_bytes(bind.try_into().unwrap()) as _),
23 MysqlType::Long => Value::Int(i32::from_ne_bytes(bind.try_into().unwrap()) as _),
24 MysqlType::LongLong => Value::Int(i64::from_ne_bytes(bind.try_into().unwrap())),
25
26 MysqlType::UnsignedTiny => Value::UInt(bind[0] as _),
27 MysqlType::UnsignedShort => {
28 Value::UInt(u16::from_ne_bytes(bind.try_into().unwrap()) as _)
29 }
30 MysqlType::UnsignedLong => {
31 Value::UInt(u32::from_ne_bytes(bind.try_into().unwrap()) as _)
32 }
33 MysqlType::UnsignedLongLong => {
34 Value::UInt(u64::from_ne_bytes(bind.try_into().unwrap()))
35 }
36 MysqlType::Float => Value::Float(f32::from_ne_bytes(bind.try_into().unwrap())),
37 MysqlType::Double => Value::Double(f64::from_ne_bytes(bind.try_into().unwrap())),
38
39 MysqlType::Time => {
40 let time: MysqlTime =
41 diesel::deserialize::FromSql::<diesel::sql_types::Time, DB>::from_sql(
42 MysqlValue::new(&bind, metadata),
43 )
44 .expect("This does not fail");
45 Value::Time(
46 time.neg,
47 time.day,
48 time.hour.try_into().map_err(cast_helper)?,
49 time.minute.try_into().map_err(cast_helper)?,
50 time.second.try_into().map_err(cast_helper)?,
51 time.second_part.try_into().expect("Cast does not fail"),
52 )
53 }
54 MysqlType::Date | MysqlType::DateTime | MysqlType::Timestamp => {
55 let time: MysqlTime = diesel::deserialize::FromSql::<
56 diesel::sql_types::Timestamp,
57 DB,
58 >::from_sql(MysqlValue::new(&bind, metadata))
59 .expect("This does not fail");
60 Value::Date(
61 time.year.try_into().map_err(cast_helper)?,
62 time.month.try_into().map_err(cast_helper)?,
63 time.day.try_into().map_err(cast_helper)?,
64 time.hour.try_into().map_err(cast_helper)?,
65 time.minute.try_into().map_err(cast_helper)?,
66 time.second.try_into().map_err(cast_helper)?,
67 time.second_part.try_into().expect("Cast does not fail"),
68 )
69 }
70 MysqlType::Numeric
71 | MysqlType::Set
72 | MysqlType::Enum
73 | MysqlType::String
74 | MysqlType::Blob => Value::Bytes(bind),
75 MysqlType::Bit => unimplemented!(),
76 _ => unreachable!(),
77 },
78 None => Value::NULL,
79 };
80 Ok(v)
81}
82
83impl<DB: MysqlLikeBackend> TryFrom<ToSqlHelper<DB>> for Params {
84 type Error = diesel::result::Error;
85
86 fn try_from(
87 ToSqlHelper {
88 metadata,
89 binds,
90 _phantom,
91 }: ToSqlHelper<DB>,
92 ) -> Result<Self, Self::Error> {
93 let values = metadata
94 .into_iter()
95 .zip(binds)
96 .map(to_value::<DB>)
97 .collect::<Result<Vec<_>, Self::Error>>()?;
98 Ok(Params::Positional(values))
99 }
100}