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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
use { crate::{Error, RefInto}, std::{ convert::{TryFrom, TryInto}, path::Path, }, }; #[derive(Clone, Debug)] pub enum Pool { PostgreSQL(r2d2::Pool<r2d2_postgres::PostgresConnectionManager<postgres::NoTls>>), SQLite(r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>), } impl Pool { pub fn postgres(config: postgres::Config) -> Result<Self, Error> { let conn = Pool::PostgreSQL(r2d2::Pool::new( r2d2_postgres::PostgresConnectionManager::new(config, postgres::NoTls), )?); Ok(conn) } pub fn sqlite(path: impl AsRef<Path>) -> Result<Self, Error> { let conn = Pool::SQLite(r2d2::Pool::new( r2d2_sqlite::SqliteConnectionManager::file(path), )?); Ok(conn) } pub fn as_kind(&self) -> PoolKind { match self { Pool::PostgreSQL(_) => PoolKind::PostgreSQL, Pool::SQLite(_) => PoolKind::SQLite, } } pub fn batch_execute(&self, exec: impl AsRef<str>) -> Result<(), Error> { match self { Pool::PostgreSQL(pool) => { let mut conn = pool.get()?; conn.batch_execute(exec.as_ref())?; } Pool::SQLite(pool) => { let conn = pool.get()?; conn.execute_batch(exec.as_ref())?; } } Ok(()) } pub fn transaction( &self, run: impl FnOnce(Transaction<'_>) -> Result<(), Error>, ) -> Result<(), Error> { match self { Pool::PostgreSQL(pool) => { let mut conn = pool.get()?; let trans = conn.transaction()?; let inner = Transaction::PostgreSQL(trans); run(inner)?; Ok(()) } Pool::SQLite(pool) => { let mut conn = pool.get()?; let trans = conn.transaction()?; let inner = Transaction::SQLite(trans); run(inner)?; Ok(()) } } } } #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum PoolKind { PostgreSQL, SQLite, } impl RefInto<PoolKind> for Pool { fn ref_into(&self) -> PoolKind { self.as_kind() } } pub enum ConnTrans<'r> { Conn(Connection), Trans(Transaction<'r>), } impl<'r> TryFrom<Pool> for ConnTrans<'r> { type Error = Error; fn try_from(pool: Pool) -> Result<ConnTrans<'r>, Error> { let conn = pool.try_into()?; Ok(ConnTrans::Conn(conn)) } } impl<'r> TryFrom<&'r Pool> for ConnTrans<'r> { type Error = Error; fn try_from(pool: &'r Pool) -> Result<ConnTrans<'r>, Error> { let conn = pool.try_into()?; Ok(ConnTrans::Conn(conn)) } } impl<'r> From<Connection> for ConnTrans<'r> { fn from(conn: Connection) -> ConnTrans<'r> { ConnTrans::Conn(conn) } } impl<'r> From<Transaction<'r>> for ConnTrans<'r> { fn from(trans: Transaction<'r>) -> ConnTrans<'r> { ConnTrans::Trans(trans) } } pub enum Connection { PostgreSQL(r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager<postgres::NoTls>>), SQLite(r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>), } impl TryFrom<Pool> for Connection { type Error = Error; fn try_from(pool: Pool) -> Result<Connection, Self::Error> { match pool { Pool::PostgreSQL(pool) => { let conn = pool.get()?; Ok(Connection::PostgreSQL(conn)) } Pool::SQLite(pool) => { let conn = pool.get()?; Ok(Connection::SQLite(conn)) } } } } impl<'r> TryFrom<&'r Pool> for Connection { type Error = Error; fn try_from(pool: &'r Pool) -> Result<Connection, Self::Error> { match pool { Pool::PostgreSQL(pool) => { let conn = pool.get()?; Ok(Connection::PostgreSQL(conn)) } Pool::SQLite(pool) => { let conn = pool.get()?; Ok(Connection::SQLite(conn)) } } } } pub enum Transaction<'r> { PostgreSQL(postgres::Transaction<'r>), SQLite(rusqlite::Transaction<'r>), } pub enum Row<'r> { PostgreSQL(postgres::Row), SQLite(&'r rusqlite::Row<'r>), }