mybatis_core/db/
bind_mysql.rs1use 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::mysql::{MySql, MySqlArguments};
9use sqlx_core::query::Query;
10
11#[inline]
12pub fn bind(
13 t: Bson,
14 mut q: Query<MySql, MySqlArguments>,
15) -> crate::Result<Query<MySql, MySqlArguments>> {
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);
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).inner);
90 }
91 BinarySubtype::UserDefined(type_id) => match type_id {
92 crate::types::BINARY_SUBTYPE_JSON => {
93 q = q.bind(
94 serde_json::from_slice::<serde_json::Value>(&d.bytes).unwrap_or_default(),
95 );
96 }
97 _ => {
98 return Err(Error::from("un supported bind type!"));
99 }
100 },
101 _ => {
102 return Err(Error::from("un supported bind type!"));
103 }
104 },
105 Bson::DateTime(d) => {
106 q = q.bind(DateTimeNative::from(d).inner);
107 }
108 Bson::Timestamp(d) => {
109 q = q.bind(crate::types::Timestamp::from(d).inner);
110 }
111 _ => {
112 return crate::Result::Err(crate::Error::from("unsupported type!"));
113 }
114 }
115 return Ok(q);
116}