use super::{r2d2, ConnPool, WrConn};
use threadpool::ThreadPool;
use ump_ng::ReplyContext;
pub use sqlsrv::rusqlite;
use rusqlite::Connection;
pub enum Error<E> {
R2D2(r2d2::Error),
App(E)
}
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 conn = cpool.writer();
let res = match f(&mut conn) {
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}");
}
});
}