derive_sql/proxy/mysql/
log.rs1use super::*;
2
3pub struct Log<C>
4where C: MysqlTrait
5{
6 conn: Option<C>,
7}
8
9impl<C> std::default::Default for Log<C>
10where C: MysqlTrait
11{
12 fn default() -> Self { Log { conn: None } }
13}
14
15impl<C> Log<C>
16where C: MysqlTrait
17{
18 pub fn with(mut self, conn: C) -> Self { self.conn = Some(conn); self }
19
20 fn conn_mut(&mut self) -> DeriveSqlResult<&mut C> {
21 self.conn.as_mut()
22 .ok_or(Error::MySqlProxyNoConnectionProvided)
23 }
24
25 fn log(&self, sql: &str) { ::log::info!("{sql}"); }
26}
27
28impl<C> MysqlTrait for Log<C>
29where C: MysqlTrait
30{
31 fn query_drop<Q>(&mut self, query: Q) -> DeriveSqlResult<()>
32 where Q: AsRef<str>
33 {
34 self.log(query.as_ref());
35 self.conn_mut()?.query_drop(query)
36 }
37
38 fn query_first<T, Q>(&mut self, query: Q) -> DeriveSqlResult<Option<T>>
39 where Q: AsRef<str>,
40 T: ::mysql::prelude::FromRow
41 {
42 self.log(query.as_ref());
43 self.conn_mut()?.query_first(query)
44 }
45
46 fn query_map<T, F, Q, U>(&mut self, query: Q, f: F) -> DeriveSqlResult<Vec<U>>
47 where Q: AsRef<str>,
48 T: ::mysql::prelude::FromRow,
49 F: FnMut(T) -> U
50 {
51 self.log(query.as_ref());
52 self.conn_mut()?.query_map(query, f)
53 }
54
55 fn exec_drop<Q, P>(&mut self, query: Q, params: P) -> DeriveSqlResult<()>
56 where Q: AsRef<str>,
57 P: Into<::mysql::Params>
58 {
59 self.log(query.as_ref());
60 self.conn_mut()?.exec_drop(query, params)
61 }
62}
63