db_derive/
conn.rs

1use {
2    crate::{table::Schema, Error, Pool},
3    std::convert::{TryFrom, TryInto},
4};
5
6pub enum Connection {
7    #[cfg(feature = "postgresql")]
8    PostgreSQL(
9        r2d2::PooledConnection<crate::pool::postgres::PostgresConnectionManager<postgres::NoTls>>,
10    ),
11    #[cfg(feature = "sqlite")]
12    SQLite(r2d2::PooledConnection<crate::pool::sqlite::SqliteConnectionManager>),
13}
14
15impl TryFrom<Pool> for Connection {
16    type Error = Error;
17
18    fn try_from(pool: Pool) -> Result<Connection, Self::Error> {
19        match pool {
20            #[cfg(feature = "postgresql")]
21            Pool::PostgreSQL(pool) => {
22                let conn = pool.get()?;
23
24                Ok(Connection::PostgreSQL(conn))
25            }
26            #[cfg(feature = "sqlite")]
27            Pool::SQLite(pool) => {
28                let conn = pool.get()?;
29
30                Ok(Connection::SQLite(conn))
31            }
32        }
33    }
34}
35
36impl<'r> TryFrom<&'r Pool> for Connection {
37    type Error = Error;
38
39    fn try_from(pool: &'r Pool) -> Result<Connection, Self::Error> {
40        match pool {
41            #[cfg(feature = "postgresql")]
42            Pool::PostgreSQL(pool) => {
43                let conn = pool.get()?;
44
45                Ok(Connection::PostgreSQL(conn))
46            }
47            #[cfg(feature = "sqlite")]
48            Pool::SQLite(pool) => {
49                let conn = pool.get()?;
50
51                Ok(Connection::SQLite(conn))
52            }
53        }
54    }
55}
56
57pub enum Transaction<'r> {
58    #[cfg(feature = "postgresql")]
59    PostgreSQL(postgres::Transaction<'r>),
60    #[cfg(feature = "sqlite")]
61    SQLite(rusqlite::Transaction<'r>),
62}
63
64impl<'r> Transaction<'r> {
65    pub fn schema<T: Schema>(&mut self) -> Result<(), Error> {
66        match self {
67            #[cfg(feature = "postgresql")]
68            Transaction::PostgreSQL(ref mut trans) => {
69                trans.batch_execute(T::schema_postgres())?;
70            }
71            #[cfg(feature = "sqlite")]
72            Transaction::SQLite(trans) => {
73                trans.execute_batch(T::schema_sqlite())?;
74            }
75        }
76
77        Ok(())
78    }
79}
80
81pub enum Row<'r> {
82    #[cfg(feature = "postgresql")]
83    PostgreSQL(postgres::Row),
84    #[cfg(feature = "sqlite")]
85    SQLite(&'r rusqlite::Row<'r>),
86}
87
88// TODO: fix clippy warning
89// Clippy says the SQLite variant is too large and to box it
90// However it would be created every time a connection is made
91#[allow(clippy::large_enum_variant)]
92pub enum ConnTrans<'r> {
93    Conn(Connection),
94    Trans(Transaction<'r>),
95}
96
97impl<'r> TryFrom<Pool> for ConnTrans<'r> {
98    type Error = Error;
99
100    fn try_from(pool: Pool) -> Result<ConnTrans<'r>, Error> {
101        let conn = pool.try_into()?;
102
103        Ok(ConnTrans::Conn(conn))
104    }
105}
106
107impl<'r> TryFrom<&'r Pool> for ConnTrans<'r> {
108    type Error = Error;
109
110    fn try_from(pool: &'r Pool) -> Result<ConnTrans<'r>, Error> {
111        let conn = pool.try_into()?;
112
113        Ok(ConnTrans::Conn(conn))
114    }
115}
116
117impl<'r> From<Connection> for ConnTrans<'r> {
118    fn from(conn: Connection) -> ConnTrans<'r> {
119        ConnTrans::Conn(conn)
120    }
121}
122
123impl<'r> From<Transaction<'r>> for ConnTrans<'r> {
124    fn from(trans: Transaction<'r>) -> ConnTrans<'r> {
125        ConnTrans::Trans(trans)
126    }
127}