Skip to main content

dapper_guru/
connection.rs

1use serde_json::Value;
2use std::collections::BTreeMap;
3
4pub type Parameters = BTreeMap<String, Value>;
5
6#[derive(Debug, Clone, Default, PartialEq)]
7pub struct ParametersBuilder {
8    entries: Parameters,
9}
10
11impl ParametersBuilder {
12    pub fn new() -> Self {
13        ParametersBuilder::default()
14    }
15
16    pub fn set(mut self, name: impl Into<String>, value: Value) -> Self {
17        self.entries.insert(name.into(), value);
18        self
19    }
20
21    pub fn build(&self) -> Parameters {
22        self.entries.clone()
23    }
24}
25
26pub fn parameters(pairs: &[(&str, Value)]) -> Parameters {
27    pairs
28        .iter()
29        .map(|(name, value)| ((*name).to_string(), value.clone()))
30        .collect()
31}
32
33#[derive(Debug, Clone, PartialEq)]
34pub enum DbError {
35    QueryFailed(String),
36}
37
38impl std::fmt::Display for DbError {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match self {
41            DbError::QueryFailed(message) => write!(f, "query failed: {message}"),
42        }
43    }
44}
45
46impl std::error::Error for DbError {}
47
48pub trait Connection {
49    fn query_grids(&self, sql: &str, params: &Parameters) -> Result<Vec<Vec<crate::Row>>, DbError>;
50
51    fn execute(&mut self, sql: &str, params: &Parameters) -> Result<usize, DbError>;
52
53    fn execute_scalar_value(&self, sql: &str, params: &Parameters) -> Result<Value, DbError>;
54}
55
56#[derive(Debug, Clone)]
57pub struct ExecutedCommand {
58    pub sql: String,
59    pub params: Parameters,
60}
61
62#[derive(Debug, Clone)]
63pub struct InMemoryConnection {
64    grids: Vec<Vec<crate::Row>>,
65    affected: usize,
66    scalar: Value,
67    pub executed: std::cell::RefCell<Vec<ExecutedCommand>>,
68}
69
70impl InMemoryConnection {
71    pub fn with_grids(grids: Vec<Vec<crate::Row>>) -> Self {
72        InMemoryConnection {
73            grids,
74            affected: 0,
75            scalar: Value::Null,
76            executed: std::cell::RefCell::new(Vec::new()),
77        }
78    }
79
80    pub fn with_affected(mut self, affected: usize) -> Self {
81        self.affected = affected;
82        self
83    }
84
85    pub fn with_scalar(mut self, scalar: Value) -> Self {
86        self.scalar = scalar;
87        self
88    }
89}
90
91impl Connection for InMemoryConnection {
92    fn query_grids(&self, sql: &str, params: &Parameters) -> Result<Vec<Vec<crate::Row>>, DbError> {
93        self.executed.borrow_mut().push(ExecutedCommand {
94            sql: sql.to_string(),
95            params: params.clone(),
96        });
97        Ok(self.grids.clone())
98    }
99
100    fn execute(&mut self, sql: &str, params: &Parameters) -> Result<usize, DbError> {
101        self.executed.borrow_mut().push(ExecutedCommand {
102            sql: sql.to_string(),
103            params: params.clone(),
104        });
105        Ok(self.affected)
106    }
107
108    fn execute_scalar_value(&self, sql: &str, params: &Parameters) -> Result<Value, DbError> {
109        self.executed.borrow_mut().push(ExecutedCommand {
110            sql: sql.to_string(),
111            params: params.clone(),
112        });
113        Ok(self.scalar.clone())
114    }
115}