rbatis_core/db/
bind_sqlite.rs

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