use super::{
r2d2,
rusqlite::{params, Connection},
ConnPool, WrConn
};
use threadpool::ThreadPool;
use ump::ReplyContext;
pub use sqlsrv::rusqlite;
pub fn proc_rodb_req<R, E, F>(
cpool: &ConnPool,
rctx: ReplyContext<R, E>,
f: F
) -> Result<(), r2d2::Error>
where
R: Send + 'static,
E: std::error::Error + Send + 'static,
F: FnOnce(&Connection) -> Result<R, E> + Send + 'static
{
let roconn = cpool.reader()?;
let res = match f(&roconn) {
Ok(reply) => rctx.reply(reply),
Err(e) => rctx.fail(e)
};
if let Err(e) = res {
eprintln!("Reply message pass failed; {e}");
}
Ok(())
}
pub fn proc_rodb_req_thrd<R, E, F>(
cpool: &ConnPool,
tpool: &ThreadPool,
rctx: ReplyContext<R, E>,
f: F
) -> Result<(), r2d2::Error>
where
R: Send + 'static,
E: std::error::Error + Send + 'static,
F: FnOnce(&Connection) -> Result<R, E> + Send + 'static
{
let roconn = cpool.reader()?;
tpool.execute(move || {
let res = match f(&roconn) {
Ok(reply) => rctx.reply(reply),
Err(e) => rctx.fail(e)
};
if let Err(e) = res {
eprintln!("Reply message pass failed; {e}");
}
});
Ok(())
}
pub fn proc_rwdb_req<R, E, F>(cpool: &ConnPool, rctx: ReplyContext<R, E>, f: F)
where
R: Send + 'static,
E: std::error::Error + Send + 'static,
F: FnOnce(&mut WrConn) -> Result<R, E> + Send + 'static
{
let mut rwconn = cpool.writer();
let res = match f(&mut rwconn) {
Ok(reply) => rctx.reply(reply),
Err(e) => rctx.fail(e)
};
if let Err(e) = res {
eprintln!("Reply message pass failed; {e}");
}
}
pub fn proc_rwdb_req_thrd<R, E, F>(
cpool: &ConnPool,
tpool: &ThreadPool,
rctx: ReplyContext<R, E>,
f: F
) where
R: Send + 'static,
E: std::error::Error + Send + 'static,
F: FnOnce(&mut WrConn) -> Result<R, E> + Send + 'static
{
let mut rwconn = cpool.writer();
tpool.execute(move || {
let res = match f(&mut rwconn) {
Ok(reply) => rctx.reply(reply),
Err(e) => rctx.fail(e)
};
if let Err(e) = res {
eprintln!("Reply message pass failed; {e}");
}
});
}
#[must_use]
pub fn incremental_vacuum(
cpool: &ConnPool,
tpool: &ThreadPool,
n: Option<usize>
) -> swctx::WaitCtx<(), (), rusqlite::Error> {
let (sctx, wctx) = swctx::mkpair();
let conn = cpool.writer();
tpool.execute(move || {
#[allow(clippy::option_if_let_else)]
let res = if let Some(n) = n {
conn.execute("PRAGMA incremental_vacuum(?);", params![n])
} else {
conn.execute("PRAGMA incremental_vacuum;", params![])
};
let res = match res {
Ok(_) => sctx.set(()),
Err(e) => sctx.fail(e)
};
if let Err(e) = res {
eprintln!("Reply message pass failed; {e}");
}
});
wctx
}