aboutsummaryrefslogtreecommitdiff
path: root/src/db/backend.rs
blob: 5950747f2a9febede31b0d72a2e55a5ba2c7946a (plain)
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
use barrel::backend::SqlGenerator;
pub use diesel::connection::Connection;

pub trait UsableBackend: diesel::backend::Backend where *const str: diesel::deserialize::FromSql<diesel::sql_types::Text, Self> {}
impl UsableBackend for diesel::sqlite::Sqlite {}

pub trait UsableConnection: Connection where <Self as Connection>::Backend: UsableBackend {}
impl UsableConnection for diesel::sqlite::SqliteConnection {}

pub trait Connectable {
    type Connection: UsableConnection;
    type SqlGenerator: SqlGenerator;
    fn connect(&self) -> diesel::ConnectionResult<Self::Connection>;
}

pub struct Sqlite {
    pub db_file: &'static str,
}

impl Connectable for Sqlite {
    type Connection = diesel::sqlite::SqliteConnection;
    type SqlGenerator = barrel::backend::Sqlite;

    fn connect(&self) -> diesel::ConnectionResult<Self::Connection> {
        Self::Connection::establish(self.db_file)
    }
}