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
use {Config}; use errors::*; #[cfg(feature="postgresql")] use postgres; #[cfg(feature="sqlite")] use rusqlite; #[allow(dead_code)] pub mod markers { pub struct PostgresqlFeatureRequired; pub struct SqliteFeatureRequired; } #[allow(unused_imports)] use self::markers::*; #[allow(dead_code)] pub struct DbConn<'a> { config: &'a Config, } impl<'a> DbConn<'a> { pub fn new(config: &'a Config) -> Self { Self { config } } #[cfg(not(feature="postgresql"))] pub fn pg_connection(&self) -> Result<PostgresqlFeatureRequired> { unimplemented!() } #[cfg(feature="postgresql")] pub fn pg_connection(&self) -> Result<postgres::Connection> { let conn_str = self.config.connect_string()?; Ok(postgres::Connection::connect(conn_str, postgres::TlsMode::None)?) } #[cfg(not(feature="sqlite"))] pub fn sqlite_connection(&self) -> Result<SqliteFeatureRequired> { unimplemented!() } #[cfg(feature="sqlite")] pub fn sqlite_connection(&self) -> Result<rusqlite::Connection> { let db_path = self.config.database_path()?; Ok(rusqlite::Connection::open(db_path)?) } }