1
use crate::crud::Transaction;
2
use crate::mapper::RowMapper;
3
use std::marker::PhantomData;
4

            
5
/// Lightweight wrapper over the collection of results of the different crates
6
/// supported by Canyon-SQL.
7
///
8
/// Even tho the wrapping seems meaningless, this allows us to provide internal
9
/// operations that are too difficult or to ugly to implement in the macros that
10
/// will call the query method of Crud.
11
pub enum CanyonRows<T> {
12
    #[cfg(feature = "postgres")]
13
    Postgres(Vec<tokio_postgres::Row>),
14
    #[cfg(feature = "mssql")]
15
    Tiberius(Vec<tiberius::Row>),
16
    #[cfg(feature = "mysql")]
17
    MySQL(Vec<mysql_async::Row>),
18

            
19
    UnusableTypeMarker(PhantomData<T>),
20
}
21

            
22
impl<T> CanyonRows<T> {
23
    #[cfg(feature = "postgres")]
24
1
    pub fn get_postgres_rows(&self) -> &Vec<tokio_postgres::Row> {
25
1
        match self {
26
1
            Self::Postgres(v) => v,
27
            _ => panic!("This branch will never ever should be reachable"),
28
        }
29
1
    }
30

            
31
    #[cfg(feature = "mssql")]
32
    pub fn get_tiberius_rows(&self) -> &Vec<tiberius::Row> {
33
        match self {
34
            Self::Tiberius(v) => v,
35
            _ => panic!("This branch will never ever should be reachable"),
36
        }
37
    }
38

            
39
    #[cfg(feature = "mysql")]
40
    pub fn get_mysql_rows(&self) -> &Vec<mysql_async::Row> {
41
        match self {
42
            Self::MySQL(v) => v,
43
            _ => panic!("This branch will never ever should be reachable"),
44
        }
45
    }
46

            
47
    /// Consumes `self` and returns the wrapped [`std::vec::Vec`] with the instances of T
48
60
    pub fn into_results<Z: RowMapper<T>>(self) -> Vec<T>
49
    where
50
        T: Transaction<T>,
51
    {
52
60
        match self {
53
            #[cfg(feature = "postgres")]
54
277
            Self::Postgres(v) => v.iter().map(|row| Z::deserialize_postgresql(row)).collect(),
55
            #[cfg(feature = "mssql")]
56
227
            Self::Tiberius(v) => v.iter().map(|row| Z::deserialize_sqlserver(row)).collect(),
57
            #[cfg(feature = "mysql")]
58
179
            Self::MySQL(v) => v.iter().map(|row| Z::deserialize_mysql(row)).collect(),
59
            _ => panic!("This branch will never ever should be reachable"),
60
        }
61
60
    }
62

            
63
    /// Returns the number of elements present on the wrapped collection
64
37
    pub fn len(&self) -> usize {
65
37
        match self {
66
            #[cfg(feature = "postgres")]
67
13
            Self::Postgres(v) => v.len(),
68
            #[cfg(feature = "mssql")]
69
12
            Self::Tiberius(v) => v.len(),
70
            #[cfg(feature = "mysql")]
71
12
            Self::MySQL(v) => v.len(),
72
            _ => panic!("This branch will never ever should be reachable"),
73
        }
74
37
    }
75

            
76
    /// Returns true whenever the wrapped collection of Rows does not contains any elements
77
    pub fn is_empty(&self) -> bool {
78
        match self {
79
            #[cfg(feature = "postgres")]
80
            Self::Postgres(v) => v.is_empty(),
81
            #[cfg(feature = "mssql")]
82
            Self::Tiberius(v) => v.is_empty(),
83
            #[cfg(feature = "mysql")]
84
            Self::MySQL(v) => v.is_empty(),
85
            _ => panic!("This branch will never ever should be reachable"),
86
        }
87
    }
88
}