mybatis_core/db/
bind_mssql.rs

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