2021-07-19 08:15:20 +00:00
|
|
|
use crate::errors::*;
|
2021-09-13 07:51:13 +00:00
|
|
|
use diesel::{dsl::sql, expression::SqlLiteral, query_dsl::LoadQuery, Connection, RunQueryDsl, SqliteConnection};
|
2021-07-19 08:15:20 +00:00
|
|
|
|
|
|
|
pub trait ConnectionExtension: Connection {
|
|
|
|
fn query<ST, T>(&self, query: &str) -> Result<T>
|
|
|
|
where
|
|
|
|
SqlLiteral<ST>: LoadQuery<SqliteConnection, T>;
|
|
|
|
|
|
|
|
fn exec(&self, query: impl AsRef<str>) -> Result<usize>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConnectionExtension for SqliteConnection {
|
|
|
|
fn query<ST, T>(&self, query: &str) -> Result<T>
|
|
|
|
where
|
|
|
|
SqlLiteral<ST>: LoadQuery<SqliteConnection, T>,
|
|
|
|
{
|
|
|
|
Ok(sql::<ST>(query).get_result(self)?)
|
|
|
|
}
|
|
|
|
|
2021-09-13 07:51:13 +00:00
|
|
|
fn exec(&self, query: impl AsRef<str>) -> Result<usize> { Ok(SqliteConnection::execute(self, query.as_ref())?) }
|
2021-07-19 08:15:20 +00:00
|
|
|
}
|