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
94
use rbson::{bson, Bson};
use sqlx_core::column::Column;
use sqlx_core::decode::Decode;
use sqlx_core::error::BoxDynError;
use sqlx_core::mssql::{Mssql, MssqlRow, MssqlValue, MssqlValueRef};
use sqlx_core::row::Row;
use sqlx_core::type_info::TypeInfo;
use sqlx_core::types::BigDecimal;
use sqlx_core::value::ValueRef;
use crate::convert::{JsonCodec, RefJsonCodec, ResultCodec};
use crate::{to_bson_macro};
use crate::db::db_adapter::DataDecoder;
use crate::types::Decimal;
impl<'r> JsonCodec for sqlx_core::mssql::MssqlValueRef<'r> {
fn try_to_bson(self) -> crate::Result<Bson> {
match self.type_info().name() {
"NULL" => {
return Ok(Bson::Null);
}
"TINYINT" => {
let r: Option<i8> = Decode::<'_, Mssql>::decode(self)?;
if let Some(r)=r{
return Ok(bson!(r as i32));
}
return Ok(Bson::Null);
}
"SMALLINT" => {
let r: Option<i16> = Decode::<'_, Mssql>::decode(self)?;
if let Some(r)=r{
return Ok(bson!(r as i32));
}
return Ok(Bson::Null);
}
"INT" => {
let r: Option<i32> = Decode::<'_, Mssql>::decode(self)?;
return Ok(to_bson_macro!(r));
}
"BIGINT" => {
let r: Option<i64> = Decode::<'_, Mssql>::decode(self)?;
return Ok(to_bson_macro!(r));
}
"REAL" => {
let r: Option<f32> = Decode::<'_, Mssql>::decode(self)?;
return Ok(to_bson_macro!(r));
}
"FLOAT" => {
let r: Option<f64> = Decode::<'_, Mssql>::decode(self)?;
return Ok(to_bson_macro!(r));
}
"VARCHAR" | "NVARCHAR" | "BIGVARCHAR" | "CHAR" | "BIGCHAR" | "NCHAR" => {
let r: Option<String> = Decode::<'_, Mssql>::decode(self)?;
return Ok(to_bson_macro!(r));
}
"NEWDECIMAL" => {
let r: Option<String> = Decode::<'_, Mssql>::decode(self)?;
return Ok(to_bson_macro!(r));
}
_ => {
return Err(crate::Error::from(format!(
"un support database type for:{:?}!",
self.type_info().name()
)));
}
}
}
}
impl RefJsonCodec for Vec<MssqlRow> {
fn try_to_bson(&self,decoder: &dyn DataDecoder) -> crate::Result<Bson> {
let mut arr = Vec::with_capacity(self.len());
for row in self {
let mut m = rbson::Document::new();
let columns = row.columns();
for x in columns {
let key = x.name();
let v: MssqlValueRef = row.try_get_raw(key)?;
let mut bson=v.try_to_bson()?;
decoder.decode(key,&mut bson)?;
m.insert(key.to_owned(), bson);
}
arr.push(Bson::Document(m));
}
Ok(Bson::from(arr))
}
}