mybatis_core/db/
bind_sqlite.rs

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