rbatis_core/db/
bind_mssql.rs

1use rbson::Bson;
2use rbson::spec::BinarySubtype;
3use sqlx_core::mssql::{Mssql, MssqlArguments};
4use sqlx_core::query::Query;
5use crate::{DateTimeNative, Uuid};
6use crate::error::Error;
7use crate::types::{DateNative, DateTimeUtc, DateUtc, Decimal, TimeNative, TimeUtc};
8
9#[inline]
10pub fn bind(t: Bson, mut q: Query<Mssql, MssqlArguments>) -> crate::Result<Query<Mssql, MssqlArguments>> {
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.to_string());
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.to_string());
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.to_string());
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.to_string());
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.to_string());
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.to_string());
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 as i32);
66        }
67        Bson::UInt64(n) => {
68            q = q.bind(n as i64);
69        }
70        Bson::Double(n) => {
71            q = q.bind(n);
72        }
73        Bson::Boolean(b) => {
74            q = q.bind(b);
75        }
76        Bson::Binary(d) => {
77            match d.subtype {
78                BinarySubtype::Uuid => {
79                    q = q.bind(crate::types::Uuid::from(d).to_string());
80                }
81                BinarySubtype::UserDefined(type_id) => {
82                    match type_id {
83                        crate::types::BINARY_SUBTYPE_JSON => {
84                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
85                        }
86                        _ => {
87                            return Err(Error::from("un supported bind type!"));
88                        }
89                    }
90                }
91                _ => {
92                    return Err(Error::from("un supported bind type!"));
93                }
94            }
95        }
96        Bson::Decimal128(d) => {
97            q = q.bind(d.to_string());
98        }
99        Bson::DateTime(d) => {
100            q = q.bind(d.to_string());
101        }
102        Bson::Timestamp(d) => {
103            q = q.bind(crate::types::Timestamp::from(d).inner.to_string());
104        }
105        _ => {
106            return crate::Result::Err(crate::Error::from("unsupported type!"));
107        }
108    }
109    return Ok(q);
110}