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
mod simple;

pub use simple::Simple;

use std::collections::HashMap;

/**
 * Trait to translate SQL row to struct and vice versa.
 *
 * You probably should use the [`Entity`] derive macro instead of writing the
 * impl by yourself.
 *
 * [`Entity`]: derive.Entity.html
 */
pub trait Entity {
    /** Create a new struct from SQL result. */
    fn from(tuple: &crate::Tuple<'_>) -> Self;
    /** Get the value of the field named `field`. */
    fn get(&self, field: &str) -> Option<&dyn crate::ToSql>;
}

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 = match tuple.field_name(x) {
                Ok(Some(name)) => name,
                _ => continue,
            };
            let value = tuple.nth(x);
            hashmap.insert(name, value);
        }

        hashmap
    }

    fn get(&self, field: &str) -> Option<&dyn crate::ToSql> {
        HashMap::get(self, field).map(|x| x as &dyn crate::ToSql)
    }
}

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> {
        let x = match field.parse::<usize>() {
            Ok(x) => x,
            Err(err) => {
                log::error!("Unable to retreive HashMap field: {}", err);
                return None;
            }
        };

        self.get(&x).map(|x| x as &dyn crate::ToSql)
    }
}

#[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(())
    }
}