toql_core/sql_arg/
from.rs

1//! Implementation of the [From](std::convert::From) for [SqlArg].
2//! Used to convert basic datatypes into [SqlArg].
3use super::SqlArg;
4use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
5
6macro_rules! from_float {
7       ($($type:ty),+) => {
8        $(
9             impl From<$type> for SqlArg {
10                fn from(t: $type) -> Self {
11                    SqlArg::F64(t.into())
12                }
13             }
14            impl From<&$type> for SqlArg {
15                fn from(t: &$type) -> Self {
16                    SqlArg::F64(t.to_owned().into())
17                }
18        }
19         impl From<&Option<$type>> for SqlArg {
20                fn from(t: &Option<$type>) -> Self {
21                    match t {
22                        Some(v) =>  SqlArg::F64(v.to_owned().into()),
23                        None => SqlArg::Null
24                    }
25
26                }
27             }
28        )+
29        };
30
31
32    }
33
34macro_rules! from_unsigned {
35       ($($type:ty),+) => {
36        $(
37             impl From<$type> for SqlArg {
38                fn from(t: $type) -> Self {
39                    SqlArg::U64(t.into())
40                }
41             }
42              impl From<&$type> for SqlArg {
43                fn from(t: &$type) -> Self {
44                    SqlArg::U64(t.to_owned().into())
45                }
46            }
47             impl From<&Option<$type>> for SqlArg {
48                fn from(t: &Option<$type>) -> Self {
49                    match t {
50                        Some(v) =>  SqlArg::U64(v.to_owned().into()),
51                        None => SqlArg::Null
52                    }
53
54                }
55             }
56
57        )+
58        };
59    }
60macro_rules! from_signed {
61        ($($type:ty),+) => {
62            $(
63                impl From<$type> for SqlArg {
64                fn from(t: $type) -> Self {
65                    SqlArg::I64(t.into())
66                }
67            }
68             impl From<&$type> for SqlArg {
69                fn from(t: &$type) -> Self {
70                    SqlArg::I64(t.to_owned().into())
71                }
72             }
73             impl From<&Option<$type>> for SqlArg {
74                fn from(t: &Option<$type>) -> Self {
75                    match t {
76                        Some(v) =>  SqlArg::I64(v.to_owned().into()),
77                        None => SqlArg::Null
78                    }
79
80                }
81             }
82            )+
83        };
84        }
85macro_rules! from_string {
86        ($($type:ty),+) => {
87            $(
88                impl From<$type> for SqlArg {
89                fn from(t: $type) -> Self {
90                    SqlArg::Str(t.to_string())
91                }
92            }
93             impl From<&$type> for SqlArg {
94                fn from(t: &$type) -> Self {
95                    SqlArg::Str(t.to_string())
96                }
97             }
98             impl From<&Option<$type>> for SqlArg {
99                fn from(t: &Option<$type>) -> Self {
100                    match t {
101                        Some(v) =>  SqlArg::Str(v.to_string()),
102                        None => SqlArg::Null
103                    }
104
105                }
106             }
107            )+
108        };
109        }
110
111from_unsigned!(u8, u16, u32, u64);
112from_signed!(i8, i16, i32, i64);
113from_float!(f32, f64);
114from_string!(String, NaiveDateTime, NaiveDate, NaiveTime);
115
116impl From<bool> for SqlArg {
117    fn from(t: bool) -> Self {
118        SqlArg::Bool(t)
119    }
120}
121impl From<&bool> for SqlArg {
122    fn from(t: &bool) -> Self {
123        SqlArg::Bool(t.to_owned())
124    }
125}
126impl From<&Option<bool>> for SqlArg {
127    fn from(t: &Option<bool>) -> Self {
128        match t {
129            Some(v) => SqlArg::Bool(v.to_owned()),
130            None => SqlArg::Null,
131        }
132    }
133}
134
135impl From<&str> for SqlArg {
136    fn from(t: &str) -> Self {
137        SqlArg::Str(t.to_owned())
138    }
139}
140
141impl From<&&str> for SqlArg {
142    fn from(t: &&str) -> Self {
143        SqlArg::Str((*t).to_owned())
144    }
145}
146
147impl From<&SqlArg> for SqlArg {
148    fn from(t: &SqlArg) -> Self {
149        t.to_owned()
150    }
151}
152
153impl<T> From<Option<T>> for SqlArg
154where
155    T: Into<SqlArg>,
156{
157    fn from(t: Option<T>) -> Self {
158        match t {
159            Some(t) => t.into(),
160            None => SqlArg::Null,
161        }
162    }
163}