rbatis_core/db/
bind_mysql.rs

1use rbson::Bson;
2use rbson::spec::BinarySubtype;
3use sqlx_core::mysql::{MySql, MySqlArguments};
4use sqlx_core::query::Query;
5use crate::error::Error;
6use crate::types::{DateNative, DateTimeNative, DateTimeUtc, DateUtc, Decimal, TimeNative, TimeUtc};
7use crate::Uuid;
8
9#[inline]
10pub fn bind(t: Bson, mut q: Query<MySql, MySqlArguments>) -> crate::Result<Query<MySql, MySqlArguments>> {
11    match t {
12        Bson::String(s) => {
13            if s.starts_with("DateTimeUtc(")  {
14                let data: DateTimeUtc = rbson::from_bson(Bson::String(s))?;
15                q = q.bind(data.inner);
16                return Ok(q);
17            }
18            if s.starts_with("DateTimeNative(")  {
19                let data: DateTimeNative = rbson::from_bson(Bson::String(s))?;
20                q = q.bind(data.inner);
21                return Ok(q);
22            }
23            if s.starts_with("DateNative(") {
24                let data: DateNative = rbson::from_bson(Bson::String(s))?;
25                q = q.bind(data.inner);
26                return Ok(q);
27            }
28            if s.starts_with("DateUtc(") {
29                let data: DateUtc = rbson::from_bson(Bson::String(s))?;
30                q = q.bind(data.inner);
31                return Ok(q);
32            }
33            if s.starts_with("TimeUtc(") {
34                let data: TimeUtc = rbson::from_bson(Bson::String(s))?;
35                q = q.bind(data.inner);
36                return Ok(q);
37            }
38            if s.starts_with("TimeNative(") {
39                let data: TimeNative = rbson::from_bson(Bson::String(s))?;
40                q = q.bind(data.inner);
41                return Ok(q);
42            }
43            if s.starts_with("Decimal(") {
44                let data: Decimal = rbson::from_bson(Bson::String(s))?;
45                q = q.bind(data.inner.to_string());
46                return Ok(q);
47            }
48            if s.starts_with("Uuid(") {
49                let data: Uuid = rbson::from_bson(Bson::String(s))?;
50                q = q.bind(data.inner.to_string());
51                return Ok(q);
52            }
53            q = q.bind(Some(s));
54        }
55        Bson::Null => {
56            q = q.bind(Option::<String>::None);
57        }
58        Bson::Int32(n) => {
59            q = q.bind(n);
60        }
61        Bson::Int64(n) => {
62            q = q.bind(n);
63        }
64        Bson::UInt32(n) => {
65            q = q.bind(n);
66        }
67        Bson::UInt64(n) => {
68            q = q.bind(n);
69        }
70        Bson::Double(n) => {
71            q = q.bind(n);
72        }
73        Bson::Boolean(b) => {
74            q = q.bind(b);
75        }
76        Bson::Decimal128(d) => {
77            q = q.bind(d.to_string());
78        }
79        Bson::Binary(d) => {
80            match d.subtype {
81                BinarySubtype::Generic => {
82                    q = q.bind(d.bytes);
83                }
84                BinarySubtype::Uuid => {
85                    q = q.bind(crate::types::Uuid::from(d).inner);
86                }
87                BinarySubtype::UserDefined(type_id) => {
88                    match type_id {
89                        crate::types::BINARY_SUBTYPE_JSON => {
90                            q = q.bind(serde_json::from_slice::<serde_json::Value>(&d.bytes).unwrap_or_default());
91                        }
92                        _ => {
93                            return Err(Error::from("un supported bind type!"));
94                        }
95                    }
96                }
97                _ => {
98                    return Err(Error::from("un supported bind type!"));
99                }
100            }
101        }
102        Bson::DateTime(d) => {
103            q = q.bind(DateTimeNative::from(d).inner);
104        }
105        Bson::Timestamp(d) => {
106            q = q.bind(crate::types::Timestamp::from(d).inner);
107        }
108        _ => {
109            return crate::Result::Err(crate::Error::from("unsupported type!"));
110        }
111    }
112    return Ok(q);
113}