1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use bson::Bson;
use bson::spec::BinarySubtype;
use sqlx_core::mssql::{Mssql, MssqlArguments};
use sqlx_core::query::Query;
use crate::error::Error;
use crate::types::{DateNative, DateTimeUtc, DateUtc, Decimal, TimeNative, TimeUtc};

#[inline]
pub fn bind(t: Bson, mut q: Query<Mssql, MssqlArguments>) -> crate::Result<Query<Mssql, MssqlArguments>> {
    match t {
        Bson::String(s) => {
            q = q.bind(Some(s));
        }
        Bson::Null => {
            q = q.bind(Option::<i32>::None);
        }
        Bson::Int32(n) => {
            q = q.bind(n);
        }
        Bson::Int64(n) => {
            q = q.bind(n);
        }
        Bson::Double(n) => {
            q = q.bind(n);
        }
        Bson::Boolean(b) => {
            q = q.bind(b);
        }
        Bson::Binary(d) => {
            match d.subtype {
                // todo unsupported
                // BinarySubtype::Generic => {
                //     q = q.bind(d.bytes);
                // }
                BinarySubtype::Uuid => {
                    q = q.bind(crate::types::Uuid::from(d).to_string());
                }
                BinarySubtype::UserDefined(type_id) => {
                    match type_id {
                        crate::types::BINARY_SUBTYPE_JSON => {
                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
                        }
                        crate::types::BINARY_SUBTYPE_DECIMAL => {
                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
                        }
                        crate::types::BINARY_SUBTYPE_DATETIME_UTC => {
                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
                        }
                        crate::types::BINARY_SUBTYPE_DATE_LOCAL => {
                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
                        }
                        crate::types::BINARY_SUBTYPE_DATE_UTC => {
                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
                        }
                        crate::types::BINARY_SUBTYPE_TIME_UTC => {
                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
                        }
                        crate::types::BINARY_SUBTYPE_TIME_LOCAL => {
                            q = q.bind(String::from_utf8(d.bytes).unwrap_or_default());
                        }
                        _ => {
                            return Err(Error::from("un supported bind type!"));
                        }
                    }
                }
                _ => {
                    return Err(Error::from("un supported bind type!"));
                }
            }
        }
        Bson::Decimal128(d) => {
            q = q.bind(d.to_string());
        }
        Bson::DateTime(d) => {
            q = q.bind(d.to_string());
        }
        Bson::Timestamp(d) => {
            q = q.bind(crate::types::Timestamp::from(d).inner.to_string());
        }
        Bson::ObjectId(d) => {
            q = q.bind(d.to_string());
        }
        _ => {
            return crate::Result::Err(crate::Error::from("unsupported type!"));
        }
    }
    return Ok(q);
}