use std::mem;
use std::path::Path;
use std::sync::Arc;
use rusqlite::{Connection, Row, ToSql};
use tokio::sync::{Mutex, MutexGuard};
use crate::entities::errors::{DbError, DbResult};
use crate::impls::async_executor::AExecutor;
use crate::impls::executor::Executor;
use crate::traits::repo::{
DynFut, DynFutDbRes, IAsyncConnection, IAsyncExecutor, IConnection, IExecutor,
};
#[derive(Clone)]
pub struct AMSQLConnection {
inner: Arc<Mutex<Connection>>,
}
unsafe impl Send for AMSQLConnection {}
unsafe impl Sync for AMSQLConnection {}
#[derive(Clone)]
pub struct Lock {
mtx: Arc<Mutex<Connection>>,
guard: Option<Arc<MutexGuard<'static, Connection>>>,
}
unsafe impl Send for Lock {}
unsafe impl Sync for Lock {}
impl Drop for Lock {
fn drop(&mut self) {
let _ = mem::replace(&mut self.guard, None);
}
}
impl AMSQLConnection {
pub fn new<P: AsRef<Path>>(path: P) -> DbResult<Self> {
let cnct = Connection::open(path).map_err(|err| DbError::CanNotConnect(err.to_string()))?;
Ok(Self {
inner: Arc::new(Mutex::new(cnct)),
})
}
}
impl IAsyncConnection for AMSQLConnection {
type Locked = Lock;
fn lock_sync(&self) -> DynFut<Lock> {
let mtx = self.inner.clone();
Box::pin(async move {
let mtx_to_lock = mtx.clone();
let guard = mtx_to_lock.lock().await;
let guard: MutexGuard<'static, Connection> = unsafe { mem::transmute(guard) };
Lock {
mtx,
guard: Some(Arc::new(guard)),
}
})
}
fn with<
T: Send + Sync + 'static,
F: FnOnce(&Connection) -> DbResult<T> + Send + Sync + 'static,
>(
&self,
fun: F,
) -> DynFutDbRes<T> {
let mtx = self.inner.clone();
Box::pin(async move {
let guard = mtx.lock().await;
fun(&guard)
})
}
}
impl IAsyncExecutor for AMSQLConnection {
type Locked = Lock;
fn lock(&self) -> DynFut<Self::Locked> {
self.lock_sync()
}
fn get_one<
T: Send + Sync + 'static,
F: FnMut(&Row<'_>) -> DbResult<T> + Send + Sync + 'static,
>(
&self,
query: &str,
params: &[&dyn ToSql],
serializer: F,
) -> DynFutDbRes<T> {
AExecutor::new(self).get_one(query, params, serializer)
}
fn get_many<
T: Send + Sync + 'static,
F: FnMut(&Row<'_>) -> DbResult<T> + Send + Sync + 'static,
>(
&self,
query: &str,
params: &[&dyn ToSql],
serializer: F,
) -> DynFutDbRes<Vec<T>> {
AExecutor::new(self).get_many(query, params, serializer)
}
fn execute(&self, query: &str, params: &[&dyn ToSql]) -> DynFutDbRes<()> {
AExecutor::new(self).execute(query, params)
}
}
impl IConnection for Lock {
type Locked = Self;
fn lock_sync(&self) -> DbResult<Self::Locked> {
Ok(Self {
mtx: self.mtx.clone(),
guard: self.guard.clone(),
})
}
fn with<T, F: FnOnce(&Connection) -> DbResult<T>>(&self, fun: F) -> DbResult<T> {
if let Some(ref grd) = self.guard {
fun(grd)
} else {
unreachable!()
}
}
}
impl IExecutor for Lock {
type Locked = Self;
fn lock(&self) -> DbResult<Self::Locked> {
self.lock_sync()
}
fn get_one<T, F: FnMut(&Row<'_>) -> DbResult<T>>(
&self,
query: &str,
params: &[&dyn ToSql],
serializer: F,
) -> DbResult<T> {
Executor::new(self).get_one(query, params, serializer)
}
fn get_many<T, F: FnMut(&Row<'_>) -> DbResult<T>>(
&self,
query: &str,
params: &[&dyn ToSql],
serializer: F,
) -> DbResult<Vec<T>> {
Executor::new(self).get_many(query, params, serializer)
}
fn execute(&self, query: &str, params: &[&dyn ToSql]) -> DbResult<()> {
Executor::new(self).execute(query, params)
}
fn execute_return_id(&self, query: &str, params: &[&dyn ToSql]) -> DbResult<i64> {
Executor::new(self).execute_return_id(query, params)
}
}