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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::str::FromStr;
use bson::Bson;
use log::{debug, error, trace};
use crate::configuration::subgraph::data_sources::sql::DialectEnum;
use super::{FromBson, SqlValue};
impl FromBson for Bson {
fn to_sql_value(
&self,
dialect: Option<&DialectEnum>,
) -> Result<SqlValue, async_graphql::Error> {
debug!("Converting Bson To SqlValue");
let sql_value = match self {
Bson::String(s) => {
let mut value = SqlValue::String(s.clone());
let is_uuid = uuid::Uuid::parse_str(s);
if is_uuid.is_ok() {
value = match dialect {
Some(DialectEnum::POSTGRES) => SqlValue::UUID(is_uuid.unwrap()),
_ => SqlValue::String(s.clone()),
};
}
let is_date = chrono::DateTime::<chrono::Utc>::from_str(s);
if is_date.is_ok() {
value = SqlValue::DateTime(is_date.unwrap());
}
value
}
Bson::Int32(i) => SqlValue::Int(*i),
Bson::Int64(i) => SqlValue::Int(*i as i32),
Bson::Boolean(b) => SqlValue::Bool(*b),
Bson::ObjectId(o) => SqlValue::ObjectID(o.to_string()),
Bson::DateTime(d) => SqlValue::DateTime(chrono::DateTime::<chrono::Utc>::from(*d)),
Bson::Array(a) => {
let mut sql_value = Vec::new();
for v in a {
sql_value.push(v.to_sql_value(dialect)?);
}
// Match the type to get the correct SqlValue
match sql_value[0] {
SqlValue::String(_) => SqlValue::StringList(
sql_value
.iter()
.map(|v| match v {
SqlValue::String(s) => s.clone(),
_ => panic!("Bson::to_sql_value: not supported"),
})
.collect(),
),
SqlValue::Int(_) => SqlValue::IntList(
sql_value
.iter()
.map(|v| match v {
SqlValue::Int(i) => *i,
_ => panic!("Bson::to_sql_value: not supported"),
})
.collect(),
),
SqlValue::Bool(_) => SqlValue::BoolList(
sql_value
.iter()
.map(|v| match v {
SqlValue::Bool(b) => *b,
_ => panic!("Bson::to_sql_value: not supported"),
})
.collect(),
),
SqlValue::UUID(_) => SqlValue::UUIDList(
sql_value
.iter()
.map(|v| match v {
SqlValue::UUID(u) => u.clone(),
_ => panic!("Bson::to_sql_value: not supported"),
})
.collect(),
),
SqlValue::DateTime(_) => SqlValue::DateTimeList(
sql_value
.iter()
.map(|v| match v {
SqlValue::DateTime(d) => d.clone(),
_ => panic!("Bson::to_sql_value: not supported"),
})
.collect(),
),
SqlValue::ObjectID(_) => SqlValue::ObjectIDList(
sql_value
.iter()
.map(|v| match v {
SqlValue::ObjectID(o) => o.clone(),
_ => panic!("Bson::to_sql_value: not supported"),
})
.collect(),
),
_ => {
error!("Bson::to_sql_value: not supported");
return Err(async_graphql::Error::new(
"Bson::to_sql_value: not supported",
));
}
}
}
_ => {
error!("Bson::to_sql_value: not supported");
return Err(async_graphql::Error::new(
"Bson::to_sql_value: not supported",
));
}
};
trace!("Bson::to_sql_value: {:?}", sql_value);
Ok(sql_value)
}
}