orm_mysql/
mysql.rs

1use std::str::FromStr;
2use mysql_async::{prelude::Queryable, Params};
3use crate::mysql::con_value::ValueConv;
4use crate::OrmR;
5
6#[allow(async_fn_in_trait)]
7pub trait OrmMySqlTrait {
8    /// The type of the primary key field (e.g., i64, String)
9    type IdType: Into<mysql_async::Value> + Send;
10
11    async fn query<C>(
12        comm: &mut C,
13        where_sql: &str,
14        limit: Option<usize>,
15    ) -> OrmR<Vec<Self>>
16    where
17        Self: Sized,
18        C: mysql_async::prelude::Queryable + Send + Sync;
19
20    async fn query_first<C>(comm: &mut C, where_sql: &str) -> OrmR<Option<Self>>
21    where
22        Self: Sized,
23        C: mysql_async::prelude::Queryable + Send + Sync;
24
25    /// Find a record by its primary key.
26    /// 
27    /// # Example
28    /// ```ignore
29    /// // For struct with #[orm_mysql(id)] on user_id field:
30    /// let user: Option<User> = User::find_by_id(&mut conn, 1).await?;
31    /// 
32    /// // For struct with String id:
33    /// let user: Option<User> = User::find_by_id(&mut conn, "uuid-xxx".to_string()).await?;
34    /// ```
35    async fn find_by_id<C>(comm: &mut C, id: Self::IdType) -> OrmR<Option<Self>>
36    where
37        Self: Sized,
38        C: mysql_async::prelude::Queryable + Send + Sync;
39
40    /// Delete a record by its primary key.
41    /// 
42    /// # Example
43    /// ```ignore
44    /// let affected = User::delete_by_id(&mut conn, 1).await?;
45    /// ```
46    async fn delete_by_id<C>(comm: &mut C, id: Self::IdType) -> OrmR<i64>
47    where
48        Self: Sized,
49        C: mysql_async::prelude::Queryable + Send + Sync;
50
51    async fn insert<C>(self, comm: &mut C) -> OrmR<i64>
52    where
53        Self: Sized,
54        C: mysql_async::prelude::Queryable + Send + Sync;
55
56    fn insert_sql() -> String
57    where
58        Self: Sized;
59
60    async fn update<C>(self, comm: &mut C) -> OrmR<i64>
61    where
62        Self: Sized,
63        C: mysql_async::prelude::Queryable + Send + Sync;
64
65    async fn delete<C>(&self, comm: &mut C) -> OrmR<i64>
66    where
67        Self: Sized,
68        C: mysql_async::prelude::Queryable + Send + Sync;
69
70    async fn delete_where<C>(comm: &mut C, where_sql: &str) -> OrmR<i64>
71    where
72        Self: Sized,
73        C: mysql_async::prelude::Queryable + Send + Sync;
74
75    fn delete_sql() -> String
76    where
77        Self: Sized;
78
79    fn where_id(&self) -> impl Into<mysql_async::Value>
80    where
81        Self: Sized;
82}
83
84#[allow(async_fn_in_trait)]
85pub trait OrmMySqlTraitConn<
86    T: OrmMySqlTrait + Send + Sync + Into<Params>,
87    C: mysql_async::prelude::Queryable + Send + Sync,
88> where
89    Self: Queryable + Sized + Send + Sync,
90{
91    async fn insert_arr(&mut self, arr: Vec<T>) -> OrmR<i64> {
92        if arr.is_empty() {
93            return Ok(0);
94        }
95        let sql = T::insert_sql();
96        let len = arr.len();
97        self.exec_batch(sql, arr).await?;
98        Ok(len as i64)
99    }
100    async fn delete_arr(&mut self, arr: Vec<T>) -> OrmR<i64> {
101        if arr.is_empty() {
102            return Ok(0);
103        }
104        let sql = T::delete_sql();
105        let params: Vec<(mysql_async::Value,)> = arr
106            .iter()
107            .map(|x| (x.where_id().into(),))
108            .collect::<Vec<_>>();
109        self.exec_batch(sql, params).await?;
110        Ok(arr.len() as i64)
111    }
112}
113
114impl<T: OrmMySqlTrait + Send + Sync + Into<Params>> OrmMySqlTraitConn<T, Self>
115    for mysql_async::Conn
116{
117}
118
119impl<'a, T: OrmMySqlTrait + Send + Sync + Clone + Into<Params>> OrmMySqlTraitConn<T, Self>
120    for mysql_async::Transaction<'a>
121{
122}
123
124pub mod con_value {
125    use std::str::FromStr;
126    use crate::OrmR;
127
128    pub trait ValueConv<T> {
129        fn conv(&self) -> OrmR<T>;
130    }
131
132    #[cfg(feature = "chrono")]
133    impl ValueConv<chrono::NaiveTime> for mysql_async::Value {
134        fn conv(&self) -> OrmR<chrono::NaiveTime> {
135            let v = match self.clone() {
136                mysql_async::Value::Bytes(bytes) => {
137                    let s = String::from_utf8(bytes)?;
138                    let v = chrono::NaiveTime::parse_from_str(&s, "%H:%M:%S")?;
139                    v
140                }
141                mysql_async::Value::Time(_is_negative, days, hours, minutes, seconds, micro) => {
142                    let mut v = chrono::NaiveTime::from_hms(0, 0, 0);
143                    v = v + chrono::Duration::days(days as i64);
144                    v = v + chrono::Duration::hours(hours as i64);
145                    v = v + chrono::Duration::minutes(minutes as i64);
146                    v = v + chrono::Duration::seconds(seconds as i64);
147                    v = v + chrono::Duration::microseconds(micro as i64);
148                    // if is_negative{
149                    //     v = v.checked_neg().unwrap();
150                    // }
151                    v
152                }
153                _ => {
154                    return Err("mysql_async::Value::Time")?;
155                }
156            };
157            Ok(v)
158        }
159    }
160    #[cfg(feature = "chrono")]
161    impl ValueConv<Option<chrono::NaiveTime>> for mysql_async::Value {
162        fn conv(&self) -> OrmR<Option<chrono::NaiveTime>> {
163            let v = match self.clone() {
164                mysql_async::Value::NULL => return Ok(None),
165                other => other.conv(),
166            }?;
167            Ok(Some(v))
168        }
169    }
170
171    #[cfg(feature = "chrono")]
172    impl ValueConv<chrono::NaiveDateTime> for mysql_async::Value {
173        fn conv(&self) -> OrmR<chrono::NaiveDateTime> {
174            let v = match self.clone() {
175                mysql_async::Value::Bytes(bytes) => {
176                    let s = String::from_utf8(bytes)?;
177                    chrono::NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S")
178                }
179                mysql_async::Value::Date(year, month, day, hour, minutes, seconds, micro) => {
180                    chrono::NaiveDateTime::parse_from_str(
181                        &format!("{year}-{month}-{day} {hour}:{minutes}:{seconds}.{micro:0>6}"),
182                        "%Y-%m-%d %H:%M:%S.f6",
183                    )
184                }
185                _ => {
186                    return Err("mysql_async::Value::Date")?;
187                }
188            }?;
189            Ok(v)
190        }
191    }
192
193    #[cfg(feature = "chrono")]
194    impl ValueConv<Option<chrono::NaiveDateTime>> for mysql_async::Value {
195        fn conv(&self) -> OrmR<Option<chrono::NaiveDateTime>> {
196            let v = match self.clone() {
197                mysql_async::Value::NULL => return Ok(None),
198                other => other.conv(),
199            }?;
200            Ok(Some(v))
201        }
202    }
203
204    #[cfg(feature = "chrono")]
205    impl ValueConv<chrono::NaiveDate> for mysql_async::Value {
206        fn conv(&self) -> OrmR<chrono::NaiveDate> {
207            let v = match self.clone() {
208                mysql_async::Value::Bytes(bytes) => {
209                    let s = String::from_utf8(bytes)?;
210                    chrono::NaiveDate::parse_from_str(&s, "%Y-%m-%d")
211                }
212                mysql_async::Value::Date(year, month, day, hour, minutes, seconds, micro) => {
213                    chrono::NaiveDate::parse_from_str(&format!("{year}-{month}-{day}"), "%Y-%m-%d")
214                }
215                mysql_async::Value::Time(_is_negative, days, hours, minutes, seconds, micro) => {
216                    let mut v = chrono::NaiveDate::from_ymd(1970, 1, 1);
217                    v = v + chrono::Duration::days(days as i64);
218                    Ok(v)
219                }
220                _ => {
221                    return Err("mysql_async deser tos chrono::NaiveDate")?;
222                }
223            }?;
224            Ok(v)
225        }
226    }
227    #[cfg(feature = "chrono")]
228    impl ValueConv<Option<chrono::NaiveDate>> for mysql_async::Value {
229        fn conv(&self) -> OrmR<Option<chrono::NaiveDate>> {
230            let v = match self.clone() {
231                mysql_async::Value::NULL => return Ok(None),
232                other => other.conv(),
233            }?;
234            Ok(Some(v))
235        }
236    }
237
238    #[cfg(feature = "chrono")]
239    impl ValueConv<chrono::DateTime<chrono::Local>> for mysql_async::Value {
240        fn conv(&self) -> OrmR<chrono::DateTime<chrono::Local>> {
241            let v = match self.clone() {
242                mysql_async::Value::Int(timestament) => {
243                    use chrono::TimeZone;
244                    let dt = chrono::Local.timestamp(timestament as i64, 0);
245                    dt
246                }
247                mysql_async::Value::UInt(timestament) => {
248                    use chrono::TimeZone;
249                    let dt = chrono::Local.timestamp(timestament as i64, 0);
250                    dt
251                }
252                mysql_async::Value::Time(_is_negative, days, hours, minutes, seconds, micro) => {
253                    let mut v = chrono::NaiveTime::from_hms(0, 0, 0);
254                    v = v + chrono::Duration::days(days as i64);
255                    v = v + chrono::Duration::hours(hours as i64);
256                    v = v + chrono::Duration::minutes(minutes as i64);
257                    v = v + chrono::Duration::seconds(seconds as i64);
258                    v = v + chrono::Duration::microseconds(micro as i64);
259                    // if is_negative{
260                    //     v = v.checked_neg().unwrap();
261                    // }
262                    chrono::Local::now().date().and_time(v).unwrap_or_default()
263                }
264                mysql_async::Value::Date(year, month, day, hour, minutes, seconds, micro) => {
265                    use chrono::TimeZone;
266                    let dt = chrono::Local
267                        .ymd(year as i32, month as u32, day as u32)
268                        .and_hms_micro(hour as u32, minutes as u32, seconds as u32, micro as u32);
269                    dt
270                }
271                other => Err(format!("ValueConv Error: {:?}", other))?,
272            };
273            Ok(v)
274        }
275    }
276
277    #[cfg(feature = "chrono")]
278    impl ValueConv<Option<chrono::DateTime<chrono::Local>>> for mysql_async::Value {
279        fn conv(&self) -> OrmR<Option<chrono::DateTime<chrono::Local>>> {
280            let v = match self.clone() {
281                mysql_async::Value::NULL => {
282                    return Ok(None);
283                }
284                other => other.conv()?,
285            };
286            Ok(v)
287        }
288    }
289
290    #[cfg(feature = "chrono")]
291    impl ValueConv<Option<chrono::DateTime<chrono::Utc>>> for mysql_async::Value {
292        fn conv(&self) -> OrmR<Option<chrono::DateTime<chrono::Utc>>> {
293            let v = match self.clone() {
294                mysql_async::Value::NULL => {
295                    return Ok(None);
296                }
297                other => other.conv()?,
298            };
299            Ok(v)
300        }
301    }
302
303    #[cfg(feature = "rust_decimal")]
304    impl ValueConv<Option<rust_decimal::Decimal>> for mysql_async::Value {
305        fn conv(&self) -> OrmR<Option<rust_decimal::Decimal>> {
306            use rust_decimal::prelude::*;
307            let v = match self.clone() {
308                mysql_async::Value::NULL => None,
309                mysql_async::Value::Bytes(v) => {
310                    Some(rust_decimal::Decimal::from_str(&(String::from_utf8(v)?))?)
311                }
312                mysql_async::Value::Int(v) => rust_decimal::Decimal::from_i64(v),
313                mysql_async::Value::UInt(v) => Decimal::from_i64(v as i64),
314                mysql_async::Value::Float(v) => Decimal::from_f32(v),
315                mysql_async::Value::Double(v) => Decimal::from_f64(v),
316                other => Err(format!("ValueConv Error: {:?}", other))?,
317            };
318            Ok(v)
319        }
320    }
321
322    #[cfg(feature = "rust_decimal")]
323    impl ValueConv<rust_decimal::Decimal> for mysql_async::Value {
324        fn conv(&self) -> OrmR<rust_decimal::Decimal> {
325            use rust_decimal::prelude::*;
326            let v = match self.clone() {
327                mysql_async::Value::NULL => None,
328                mysql_async::Value::Bytes(v) => {
329                    Some(rust_decimal::Decimal::from_str(&(String::from_utf8(v)?))?)
330                }
331                mysql_async::Value::Int(v) => rust_decimal::Decimal::from_i64(v),
332                mysql_async::Value::UInt(v) => Decimal::from_i64(v as i64),
333                mysql_async::Value::Float(v) => Decimal::from_f32(v),
334                mysql_async::Value::Double(v) => Decimal::from_f64(v),
335                other => Err(format!("ValueConv Error: {:?}", other))?,
336            };
337            Ok(v.unwrap_or_else(|| rust_decimal::Decimal::ZERO))
338        }
339    }
340
341    #[cfg(feature = "bigdecimal")]
342    impl ValueConv<Option<bigdecimal::BigDecimal>> for mysql_async::Value {
343        fn conv(&self) -> OrmR<Option<bigdecimal::BigDecimal>> {
344            use bigdecimal::BigDecimal as Decimal;
345            use bigdecimal::FromPrimitive;
346            let v = match self.clone() {
347                mysql_async::Value::NULL => None,
348                mysql_async::Value::Bytes(v) => Some(Decimal::from_str(&(String::from_utf8(v)?))?),
349                mysql_async::Value::Int(v) => Decimal::from_i64(v),
350                mysql_async::Value::UInt(v) => Decimal::from_i64(v as i64),
351                mysql_async::Value::Float(v) => Decimal::from_f32(v),
352                mysql_async::Value::Double(v) => Decimal::from_f64(v),
353                other => Err(format!("ValueConv Error: {:?}", other))?,
354            };
355            Ok(v)
356        }
357    }
358
359    #[cfg(feature = "bigdecimal")]
360    impl ValueConv<bigdecimal::BigDecimal> for mysql_async::Value {
361        fn conv(&self) -> OrmR<bigdecimal::BigDecimal> {
362            use bigdecimal::BigDecimal as Decimal;
363            use bigdecimal::FromPrimitive;
364            let v = match self.clone() {
365                mysql_async::Value::NULL => None,
366                mysql_async::Value::Bytes(v) => Some(Decimal::from_str(&(String::from_utf8(v)?))?),
367                mysql_async::Value::Int(v) => Decimal::from_i64(v),
368                mysql_async::Value::UInt(v) => Decimal::from_i64(v as i64),
369                mysql_async::Value::Float(v) => Decimal::from_f32(v),
370                mysql_async::Value::Double(v) => Decimal::from_f64(v),
371                other => Err(format!("ValueConv Error: {:?}", other))?,
372            };
373            Ok(v.unwrap_or_else(|| Decimal::default()))
374        }
375    }
376
377    impl ValueConv<f64> for mysql_async::Value {
378        fn conv(&self) -> OrmR<f64> {
379            let v = match self.clone() {
380                mysql_async::Value::NULL => 0.0,
381                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
382                mysql_async::Value::Int(v) => v as f64,
383                mysql_async::Value::UInt(v) => v as f64,
384                mysql_async::Value::Float(v) => v as f64,
385                mysql_async::Value::Double(v) => v,
386                other => Err(format!("ValueConv Error: {:?}", other))?,
387            };
388            Ok(v)
389        }
390    }
391    impl ValueConv<Option<f64>> for mysql_async::Value {
392        fn conv(&self) -> OrmR<Option<f64>> {
393            let v = match self.clone() {
394                mysql_async::Value::NULL => None,
395                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
396                mysql_async::Value::Int(v) => Some(v as f64),
397                mysql_async::Value::UInt(v) => Some(v as f64),
398                mysql_async::Value::Float(v) => Some(v as f64),
399                mysql_async::Value::Double(v) => Some(v),
400                other => Err(format!("ValueConv Error: {:?}", other))?,
401            };
402            Ok(v)
403        }
404    }
405
406    impl ValueConv<f32> for mysql_async::Value {
407        fn conv(&self) -> OrmR<f32> {
408            let v = match self.clone() {
409                mysql_async::Value::NULL => 0.0,
410                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
411                mysql_async::Value::Int(v) => v as f32,
412                mysql_async::Value::UInt(v) => v as f32,
413                mysql_async::Value::Float(v) => v,
414                mysql_async::Value::Double(v) => v as f32,
415                other => Err(format!("ValueConv Error: {:?}", other))?,
416            };
417            Ok(v)
418        }
419    }
420    impl ValueConv<Option<f32>> for mysql_async::Value {
421        fn conv(&self) -> OrmR<Option<f32>> {
422            let v = match self.clone() {
423                mysql_async::Value::NULL => None,
424                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
425                mysql_async::Value::Int(v) => Some(v as f32),
426                mysql_async::Value::UInt(v) => Some(v as f32),
427                mysql_async::Value::Float(v) => Some(v),
428                mysql_async::Value::Double(v) => Some(v as f32),
429                other => Err(format!("ValueConv Error: {:?}", other))?,
430            };
431            Ok(v)
432        }
433    }
434
435    impl ValueConv<isize> for mysql_async::Value {
436        fn conv(&self) -> OrmR<isize> {
437            let v = match self.clone() {
438                mysql_async::Value::NULL => 0,
439                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
440                mysql_async::Value::Int(v) => v as isize,
441                mysql_async::Value::UInt(v) => v as isize,
442                mysql_async::Value::Float(v) => v as isize,
443                mysql_async::Value::Double(v) => v as isize,
444                other => Err(format!("ValueConv Error: {:?}", other))?,
445            };
446            Ok(v)
447        }
448    }
449    impl ValueConv<Option<isize>> for mysql_async::Value {
450        fn conv(&self) -> OrmR<Option<isize>> {
451            let v = match self.clone() {
452                mysql_async::Value::NULL => None,
453                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
454                mysql_async::Value::Int(v) => Some(v as isize),
455                mysql_async::Value::UInt(v) => Some(v as isize),
456                mysql_async::Value::Float(v) => Some(v as isize),
457                mysql_async::Value::Double(v) => Some(v as isize),
458                other => Err(format!("ValueConv Error: {:?}", other))?,
459            };
460            Ok(v)
461        }
462    }
463
464    impl ValueConv<u64> for mysql_async::Value {
465        fn conv(&self) -> OrmR<u64> {
466            let v = match self.clone() {
467                mysql_async::Value::NULL => 0,
468                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
469                mysql_async::Value::Int(v) => v as u64,
470                mysql_async::Value::UInt(v) => v as u64,
471                mysql_async::Value::Float(v) => v as u64,
472                mysql_async::Value::Double(v) => v as u64,
473                other => Err(format!("ValueConv Error: {:?}", other))?,
474            };
475            Ok(v)
476        }
477    }
478    impl ValueConv<Option<u64>> for mysql_async::Value {
479        fn conv(&self) -> OrmR<Option<u64>> {
480            let v = match self.clone() {
481                mysql_async::Value::NULL => None,
482                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
483                mysql_async::Value::Int(v) => Some(v as u64),
484                mysql_async::Value::UInt(v) => Some(v as u64),
485                mysql_async::Value::Float(v) => Some(v as u64),
486                mysql_async::Value::Double(v) => Some(v as u64),
487                other => Err(format!("ValueConv Error: {:?}", other))?,
488            };
489            Ok(v)
490        }
491    }
492
493    impl ValueConv<u32> for mysql_async::Value {
494        fn conv(&self) -> OrmR<u32> {
495            let v = match self.clone() {
496                mysql_async::Value::NULL => 0,
497                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
498                mysql_async::Value::Int(v) => v as u32,
499                mysql_async::Value::UInt(v) => v as u32,
500                mysql_async::Value::Float(v) => v as u32,
501                mysql_async::Value::Double(v) => v as u32,
502                other => Err(format!("ValueConv Error: {:?}", other))?,
503            };
504            Ok(v)
505        }
506    }
507    impl ValueConv<Option<u32>> for mysql_async::Value {
508        fn conv(&self) -> OrmR<Option<u32>> {
509            let v = match self.clone() {
510                mysql_async::Value::NULL => None,
511                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
512                mysql_async::Value::Int(v) => Some(v as u32),
513                mysql_async::Value::UInt(v) => Some(v as u32),
514                mysql_async::Value::Float(v) => Some(v as u32),
515                mysql_async::Value::Double(v) => Some(v as u32),
516                other => Err(format!("ValueConv Error: {:?}", other))?,
517            };
518            Ok(v)
519        }
520    }
521
522    impl ValueConv<usize> for mysql_async::Value {
523        fn conv(&self) -> OrmR<usize> {
524            let v = match self.clone() {
525                mysql_async::Value::NULL => 0,
526                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
527                mysql_async::Value::Int(v) => v as usize,
528                mysql_async::Value::UInt(v) => v as usize,
529                mysql_async::Value::Float(v) => v as usize,
530                mysql_async::Value::Double(v) => v as usize,
531                other => Err(format!("ValueConv Error: {:?}", other))?,
532            };
533            Ok(v)
534        }
535    }
536    impl ValueConv<Option<usize>> for mysql_async::Value {
537        fn conv(&self) -> OrmR<Option<usize>> {
538            let v = match self.clone() {
539                mysql_async::Value::NULL => None,
540                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
541                mysql_async::Value::Int(v) => Some(v as usize),
542                mysql_async::Value::UInt(v) => Some(v as usize),
543                mysql_async::Value::Float(v) => Some(v as usize),
544                mysql_async::Value::Double(v) => Some(v as usize),
545                other => Err(format!("ValueConv Error: {:?}", other))?,
546            };
547            Ok(v)
548        }
549    }
550
551    impl ValueConv<u8> for mysql_async::Value {
552        fn conv(&self) -> OrmR<u8> {
553            let v = match self.clone() {
554                mysql_async::Value::NULL => 0,
555                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
556                mysql_async::Value::Int(v) => v as u8,
557                mysql_async::Value::UInt(v) => v as u8,
558                mysql_async::Value::Float(v) => v as u8,
559                mysql_async::Value::Double(v) => v as u8,
560                other => Err(format!("ValueConv Error: {:?}", other))?,
561            };
562            Ok(v)
563        }
564    }
565
566    impl ValueConv<Option<u8>> for mysql_async::Value {
567        fn conv(&self) -> OrmR<Option<u8>> {
568            let v = match self.clone() {
569                mysql_async::Value::NULL => None,
570                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
571                mysql_async::Value::Int(v) => Some(v as u8),
572                mysql_async::Value::UInt(v) => Some(v as u8),
573                mysql_async::Value::Float(v) => Some(v as u8),
574                mysql_async::Value::Double(v) => Some(v as u8),
575                other => Err(format!("ValueConv Error: {:?}", other))?,
576            };
577            Ok(v)
578        }
579    }
580
581    impl ValueConv<i32> for mysql_async::Value {
582        fn conv(&self) -> OrmR<i32> {
583            let v = match self.clone() {
584                mysql_async::Value::NULL => 0,
585                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
586                mysql_async::Value::Int(v) => v as i32,
587                mysql_async::Value::UInt(v) => v as i32,
588                mysql_async::Value::Float(v) => v as i32,
589                mysql_async::Value::Double(v) => v as i32,
590                other => Err(format!("ValueConv Error: {:?}", other))?,
591            };
592            Ok(v)
593        }
594    }
595
596    impl ValueConv<Option<i32>> for mysql_async::Value {
597        fn conv(&self) -> OrmR<Option<i32>> {
598            let v = match self.clone() {
599                mysql_async::Value::NULL => None,
600                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
601                mysql_async::Value::Int(v) => Some(v as i32),
602                mysql_async::Value::UInt(v) => Some(v as i32),
603                mysql_async::Value::Float(v) => Some(v as i32),
604                mysql_async::Value::Double(v) => Some(v as i32),
605                other => Err(format!("ValueConv Error: {:?}", other))?,
606            };
607            Ok(v)
608        }
609    }
610
611    impl ValueConv<i64> for mysql_async::Value {
612        fn conv(&self) -> OrmR<i64> {
613            let v = match self.clone() {
614                mysql_async::Value::NULL => 0,
615                mysql_async::Value::Bytes(v) => String::from_utf8(v)?.parse()?,
616                mysql_async::Value::Int(v) => v,
617                mysql_async::Value::UInt(v) => v as i64,
618                mysql_async::Value::Float(v) => v as i64,
619                mysql_async::Value::Double(v) => v as i64,
620                other => Err(format!("ValueConv Error: {:?}", other))?,
621            };
622            Ok(v)
623        }
624    }
625    impl ValueConv<Option<i64>> for mysql_async::Value {
626        fn conv(&self) -> OrmR<Option<i64>> {
627            let v = match self.clone() {
628                mysql_async::Value::NULL => None,
629                mysql_async::Value::Bytes(v) => Some(String::from_utf8(v)?.parse()?),
630                mysql_async::Value::Int(v) => Some(v),
631                mysql_async::Value::UInt(v) => Some(v as i64),
632                mysql_async::Value::Float(v) => Some(v as i64),
633                mysql_async::Value::Double(v) => Some(v as i64),
634                other => Err(format!("ValueConv Error: {:?}", other))?,
635            };
636            Ok(v)
637        }
638    }
639
640    impl ValueConv<String> for mysql_async::Value {
641        fn conv(&self) -> OrmR<String> {
642            let v = match self.clone() {
643                mysql_async::Value::NULL => "".into(),
644                mysql_async::Value::Bytes(v) => String::from_utf8(v)?,
645                mysql_async::Value::Int(v) => v.to_string(),
646                mysql_async::Value::UInt(v) => v.to_string(),
647                mysql_async::Value::Float(v) => v.to_string(),
648                mysql_async::Value::Double(v) => v.to_string(),
649                other => format!("{:?}", other),
650            };
651            Ok(v)
652        }
653    }
654
655    impl ValueConv<Option<String>> for mysql_async::Value {
656        fn conv(&self) -> OrmR<Option<String>> {
657            let v = match self.clone() {
658                mysql_async::Value::NULL => return Ok(None),
659                mysql_async::Value::Bytes(v) => String::from_utf8(v)?,
660                mysql_async::Value::Int(v) => v.to_string(),
661                mysql_async::Value::UInt(v) => v.to_string(),
662                mysql_async::Value::Float(v) => v.to_string(),
663                mysql_async::Value::Double(v) => v.to_string(),
664                other => format!("{:?}", other),
665            };
666            Ok(Some(v))
667        }
668    }
669}