2018-12-14 23:16:18 +01:00
|
|
|
use diesel::{dsl::sql_query, r2d2::{ConnectionManager, CustomizeConnection, Error as ConnError, Pool, PooledConnection}, ConnectionError, RunQueryDsl};
|
2018-11-24 12:44:17 +01:00
|
|
|
use rocket::{
|
|
|
|
http::Status,
|
|
|
|
request::{self, FromRequest},
|
|
|
|
Outcome, Request, State,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-04-24 11:21:39 +02:00
|
|
|
use std::ops::Deref;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
use Connection;
|
|
|
|
|
|
|
|
pub type DbPool = Pool<ConnectionManager<Connection>>;
|
2018-06-20 20:22:34 +02:00
|
|
|
|
2018-04-22 15:35:37 +02:00
|
|
|
// From rocket documentation
|
|
|
|
|
|
|
|
// Connection request guard type: a wrapper around an r2d2 pooled connection.
|
2018-09-27 23:06:40 +02:00
|
|
|
pub struct DbConn(pub PooledConnection<ConnectionManager<Connection>>);
|
2018-04-22 15:35:37 +02:00
|
|
|
|
|
|
|
/// Attempts to retrieve a single connection from the managed database pool. If
|
|
|
|
/// no pool is currently managed, fails with an `InternalServerError` status. If
|
|
|
|
/// no connections are available, fails with a `ServiceUnavailable` status.
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
2018-09-27 23:06:40 +02:00
|
|
|
let pool = request.guard::<State<DbPool>>()?;
|
2018-04-22 15:35:37 +02:00
|
|
|
match pool.get() {
|
|
|
|
Ok(conn) => Outcome::Success(DbConn(conn)),
|
2018-11-24 12:44:17 +01:00
|
|
|
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
// For the convenience of using an &DbConn as an &Connection.
|
2018-04-22 15:35:37 +02:00
|
|
|
impl Deref for DbConn {
|
2018-09-27 23:06:40 +02:00
|
|
|
type Target = Connection;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 23:16:18 +01:00
|
|
|
|
|
|
|
// Execute a pragma for every new sqlite connection
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct PragmaForeignKey;
|
|
|
|
impl CustomizeConnection<Connection, ConnError> for PragmaForeignKey {
|
|
|
|
#[cfg(feature = "sqlite")] // will default to an empty function for postgres
|
|
|
|
fn on_acquire(&self, conn: &mut Connection) -> Result<(), ConnError> {
|
|
|
|
sql_query("PRAGMA foreign_keys = on;").execute(conn)
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(|_| ConnError::ConnectionError(ConnectionError::BadConnection(String::from("PRAGMA foreign_keys = on failed"))))
|
|
|
|
}
|
|
|
|
}
|