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
use std::collections::HashMap;
pub trait Entity {
fn from(tuple: &crate::Tuple<'_>) -> Self;
fn get(&self, field: &str) -> Option<&dyn crate::ToSql>;
}
impl<T: crate::ToSql + crate::FromSql> Entity for T {
fn from(tuple: &crate::Tuple<'_>) -> T {
tuple.nth(0)
}
fn get(&self, _: &str) -> Option<&dyn crate::ToSql> {
Some(self)
}
}
impl<T: crate::FromSql + crate::ToSql, S: std::hash::BuildHasher + Default>
Entity for HashMap<String, T, S>
{
fn from(tuple: &crate::Tuple<'_>) -> Self {
let mut hashmap = HashMap::default();
for x in 0..tuple.len() {
let name = tuple.field_name(x).unwrap();
let value = tuple.nth(x);
hashmap.insert(name, value);
}
hashmap
}
fn get(&self, field: &str) -> Option<&dyn crate::ToSql> {
match self.get(field) {
Some(value) => Some(value),
None => None,
}
}
}
impl<T: crate::FromSql + crate::ToSql, S: std::hash::BuildHasher + Default>
Entity for HashMap<usize, T, S>
{
fn from(tuple: &crate::Tuple<'_>) -> Self {
let mut hashmap = HashMap::default();
for x in 0..tuple.len() {
let value = tuple.nth(x);
hashmap.insert(x, value);
}
hashmap
}
fn get(&self, field: &str) -> Option<&dyn crate::ToSql> {
match self.get(&field.parse::<usize>().unwrap()) {
Some(value) => Some(value),
None => None,
}
}
}
#[cfg(test)]
mod test {
use std::collections::HashMap;
#[test]
fn hashmap_str_from_sql() -> crate::Result<()> {
let elephantry = crate::test::new_conn()?;
let results: Vec<HashMap<String, i32>> =
elephantry.query("SELECT 1 as n", &[])?.collect();
assert_eq!(results[0].get("n"), Some(&1));
Ok(())
}
#[test]
fn hashmap_usize_from_sql() -> crate::Result<()> {
let elephantry = crate::test::new_conn()?;
let results: Vec<HashMap<usize, i32>> =
elephantry.query("SELECT 1 as n", &[])?.collect();
assert_eq!(results[0].get(&0), Some(&1));
Ok(())
}
}