ump_ngx/server/sqlsrv/
utils.rs1use super::{r2d2, ConnPool, WrConn};
4
5
6use threadpool::ThreadPool;
7
8use ump_ng::ReplyContext;
9
10pub use sqlsrv::rusqlite;
12
13use rusqlite::Connection;
14
15pub enum Error<E> {
16 R2D2(r2d2::Error),
17 App(E)
18}
19
20
21pub fn proc_rodb_req<R, E, F>(
31 cpool: &ConnPool,
32 rctx: ReplyContext<R, E>,
33 f: F
34) -> Result<(), r2d2::Error>
35where
36 R: Send + 'static,
37 E: std::error::Error + Send + 'static,
38 F: FnOnce(&Connection) -> Result<R, E> + Send + 'static
39{
40 let roconn = cpool.reader()?;
41
42 let res = match f(&roconn) {
43 Ok(reply) => rctx.reply(reply),
44 Err(e) => rctx.fail(e)
45 };
46 if let Err(e) = res {
47 eprintln!("Reply message pass failed; {e}");
48 }
49
50 Ok(())
51}
52
53pub fn proc_rodb_req_thrd<R, E, F>(
66 cpool: &ConnPool,
67 tpool: &ThreadPool,
68 rctx: ReplyContext<R, E>,
69 f: F
70) -> Result<(), r2d2::Error>
71where
72 R: Send + 'static,
73 E: std::error::Error + Send + 'static,
74 F: FnOnce(&Connection) -> Result<R, E> + Send + 'static
75{
76 let roconn = cpool.reader()?;
77
78 tpool.execute(move || {
79 let res = match f(&roconn) {
80 Ok(reply) => rctx.reply(reply),
81 Err(e) => rctx.fail(e)
82 };
83 if let Err(e) = res {
84 eprintln!("Reply message pass failed; {e}");
85 }
86 });
87
88 Ok(())
89}
90
91pub fn proc_rwdb_req<R, E, F>(cpool: &ConnPool, rctx: ReplyContext<R, E>, f: F)
93where
94 R: Send + 'static,
95 E: std::error::Error + Send + 'static,
96 F: FnOnce(&mut WrConn) -> Result<R, E> + Send + 'static
97{
98 let mut conn = cpool.writer();
99
100 let res = match f(&mut conn) {
101 Ok(reply) => rctx.reply(reply),
102 Err(e) => rctx.fail(e)
103 };
104 if let Err(e) = res {
105 eprintln!("Reply message pass failed; {e}");
106 }
107}
108
109pub fn proc_rwdb_req_thrd<R, E, F>(
118 cpool: &ConnPool,
119 tpool: &ThreadPool,
120 rctx: ReplyContext<R, E>,
121 f: F
122) where
123 R: Send + 'static,
124 E: std::error::Error + Send + 'static,
125 F: FnOnce(&mut WrConn) -> Result<R, E> + Send + 'static
126{
127 let mut rwconn = cpool.writer();
128
129 tpool.execute(move || {
130 let res = match f(&mut rwconn) {
131 Ok(reply) => rctx.reply(reply),
132 Err(e) => rctx.fail(e)
133 };
134 if let Err(e) = res {
135 eprintln!("Reply message pass failed; {e}");
136 }
137 });
138}
139
140