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
89
90
91
92
93
use bson::Bson;
use bson::spec::BinarySubtype;
use sqlx_core::sqlite::{Sqlite, SqliteArguments};
use sqlx_core::query::Query;
use crate::error::Error;
use crate::types::{DateNative, DateTimeNative, DateTimeUtc, DateUtc, Decimal, TimeNative, TimeUtc};

#[inline]
pub fn bind<'a>(t: Bson, mut q: Query<'a, Sqlite, SqliteArguments<'a>>) -> crate::Result<Query<'a, Sqlite, SqliteArguments<'a>>> {
    match t {
        Bson::String(s) => {
            q = q.bind(Some(s));
        }
        Bson::Null => {
            q = q.bind(Option::<String>::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::Decimal128(d) => {
            q = q.bind(d.to_string());
        }
        Bson::Binary(d) => {
            match d.subtype {
                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(d.bytes);
                        }
                        crate::types::BINARY_SUBTYPE_DECIMAL => {
                            let data: Decimal = bson::from_slice(&d.bytes)?;
                            q = q.bind(data.inner.to_string());
                        }
                        crate::types::BINARY_SUBTYPE_DATETIME_UTC => {
                            let data: DateTimeUtc = bson::from_slice(&d.bytes)?;
                            q = q.bind(data.inner);
                        }
                        crate::types::BINARY_SUBTYPE_DATE_LOCAL => {
                            let data: DateNative = bson::from_slice(&d.bytes)?;
                            q = q.bind(data.inner);
                        }
                        crate::types::BINARY_SUBTYPE_DATE_UTC => {
                            let data: DateUtc = bson::from_slice(&d.bytes)?;
                            q = q.bind(data.inner);
                        }
                        crate::types::BINARY_SUBTYPE_TIME_UTC => {
                            let data: TimeUtc = bson::from_slice(&d.bytes)?;
                            q = q.bind(data.inner);
                        }
                        crate::types::BINARY_SUBTYPE_TIME_LOCAL => {
                            let data: TimeNative = bson::from_slice(&d.bytes)?;
                            q = q.bind(data.inner);
                        }
                        _ => {
                            return Err(Error::from("un supported bind type!"));
                        }
                    }
                }
                _ => {
                    return Err(Error::from("un supported bind type!"));
                }
            }
        }
        Bson::DateTime(d) => {
            q = q.bind(DateTimeNative::from(d).inner);
        }
        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);
}