Skip to content

Commit e16b744

Browse files
0xDjoleknutwalker
andauthored
Pass multiple params as a hashmap (#68)
* pass as hashmap * Make signature of params more generic --------- Co-authored-by: Paul Horn <dev@knutwalker.engineer>
1 parent 85b7d42 commit e16b744

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

lib/src/query.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@ impl Query {
2222
}
2323
}
2424

25-
pub fn param<T: std::convert::Into<BoltType>>(mut self, key: &str, value: T) -> Self {
25+
pub fn param<T: Into<BoltType>>(mut self, key: &str, value: T) -> Self {
2626
self.params.put(key.into(), value.into());
2727
self
2828
}
2929

30+
pub fn params<K, V>(mut self, input_params: impl IntoIterator<Item = (K, V)>) -> Self
31+
where
32+
K: Into<BoltString>,
33+
V: Into<BoltType>,
34+
{
35+
for (key, value) in input_params {
36+
self.params.put(key.into(), value.into());
37+
}
38+
39+
self
40+
}
41+
3042
pub(crate) async fn run(
3143
self,
3244
config: &Config,
@@ -64,3 +76,23 @@ impl Query {
6476
}
6577
}
6678
}
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
#[test]
85+
fn add_params() {
86+
let q = Query::new("MATCH (n) WHERE n.name = $name AND n.age > $age RETURN n".to_owned());
87+
let q = q.params([
88+
("name", BoltType::from("Frobniscante")),
89+
("age", BoltType::from(42)),
90+
]);
91+
92+
assert_eq!(
93+
q.params.get::<String>("name").unwrap(),
94+
String::from("Frobniscante")
95+
);
96+
assert_eq!(q.params.get::<i64>("age").unwrap(), 42);
97+
}
98+
}

0 commit comments

Comments
 (0)