1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::*;

pub struct Log<C>
where C: MysqlTrait
{
  conn: Option<C>,
}

impl<C> std::default::Default for Log<C>
where C: MysqlTrait
{
  fn default() -> Self { Log { conn: None } }
}

impl<C> Log<C>
where C: MysqlTrait
{
  pub fn with(mut self, conn: C) -> Self { self.conn = Some(conn); self }

  fn conn_mut(&mut self) -> DeriveSqlResult<&mut C> { 
    self.conn.as_mut()
    .ok_or(Error::MySqlProxyNoConnectionProvided)
  }

  fn log(&self, sql: &str) { ::log::info!("{sql}"); }
}

impl<C> MysqlTrait for Log<C>
where C: MysqlTrait
{
  fn query_drop<Q>(&mut self, query: Q) -> DeriveSqlResult<()>
  where Q: AsRef<str>
  {
    self.log(query.as_ref());
    self.conn_mut()?.query_drop(query)
  }

  fn query_first<T, Q>(&mut self, query: Q) -> DeriveSqlResult<Option<T>>
  where Q: AsRef<str>,
        T: ::mysql::prelude::FromRow
  {
    self.log(query.as_ref());
    self.conn_mut()?.query_first(query)
  }

  fn query_map<T, F, Q, U>(&mut self, query: Q, f: F) -> DeriveSqlResult<Vec<U>>
  where Q: AsRef<str>,
        T: ::mysql::prelude::FromRow,
        F: FnMut(T) -> U
  {
    self.log(query.as_ref());
    self.conn_mut()?.query_map(query, f)
  }

  fn exec_drop<Q, P>(&mut self, query: Q, params: P) -> DeriveSqlResult<()>
  where Q: AsRef<str>,
        P: Into<::mysql::Params>
  {
    self.log(query.as_ref());
    self.conn_mut()?.exec_drop(query, params)
  }
}