derive_sql/proxy/sqlite/
log.rs

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
use super::*;

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

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

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

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

impl<C> SqliteTrait for Log<C>
where C: SqliteTrait
{
  fn execute<P>(&self, sql: &str, params: P) -> DeriveSqlResult<usize>
  where P: rusqlite::Params 
  {
    self.log(sql);
    self.conn.as_ref().ok_or(Error::SqliteProxyNoConnectionProvided)?
    .execute(sql, params)
  }

  fn query_first<T, P, F>(&self, sql: &str, params: P, f: F) -> DeriveSqlResult<T>
  where P: rusqlite::Params,
        F: FnOnce(&rusqlite::Row<'_>) -> rusqlite::Result<T>
  {
    self.log(sql);
    self.conn.as_ref().ok_or(Error::SqliteProxyNoConnectionProvided)?
    .query_first(sql, params, f)
  }

  fn query_map<T, P, F>(&self, sql: &str, params: P, f: F) -> DeriveSqlResult<Vec<T>>
  where P: rusqlite::Params,
        F: FnMut(&rusqlite::Row<'_>) -> rusqlite::Result<T>
  {
    self.log(sql);
    self.conn.as_ref().ok_or(Error::SqliteProxyNoConnectionProvided)?
    .query_map(sql, params, f)
  }
}