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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::collections::BTreeMap;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::{self, Value};
use serde_json::error::Error as JsonError;
#[macro_export]
macro_rules! cypher_stmt {
( $s:expr ) => {{
Ok($crate::Statement::new($s)) as Result<$crate::Statement, $crate::serde_json::error::Error>
}};
( $s:expr, { $( $k:expr => $v:expr ),+ } ) => {{
let mut stmt = $crate::Statement::new($s);
let mut error: Option<$crate::serde_json::error::Error> = None;
$(
if error.is_none() {
match stmt.add_param($k, $v) {
Err(e) => error = Some(e),
_ => {}
}
}
)*
if let Some(error) = error {
Err($crate::error::GraphError::Serde(error))
} else {
Ok(stmt)
}
}}
}
#[derive(Clone, Debug, Serialize)]
pub struct Statement {
statement: String,
parameters: BTreeMap<String, Value>,
}
impl Statement {
pub fn new<T: Into<String>>(statement: T) -> Self {
Statement {
statement: statement.into(),
parameters: BTreeMap::new(),
}
}
pub fn statement(&self) -> &str {
&self.statement
}
pub fn add_param<K, V>(&mut self, key: K, value: V) -> Result<(), JsonError>
where K: Into<String>, V: Serialize + Copy
{
self.parameters.insert(key.into(), serde_json::value::to_value(&value)?);
Ok(())
}
pub fn with_param<K, V>(mut self, key: K, value: V) -> Result<Self, JsonError>
where K: Into<String>, V: Serialize + Copy
{
self.add_param(key, value)?;
Ok(self)
}
pub fn param<T: DeserializeOwned>(&self, key: &str) -> Option<Result<T, serde_json::error::Error>> {
self.parameters.get(key).map(|v| serde_json::value::from_value(v.clone()))
}
pub fn parameters(&self) -> &BTreeMap<String, Value> {
&self.parameters
}
pub fn set_parameters<T: Serialize>(&mut self, params: &BTreeMap<String, T>)
-> Result<(), JsonError>
{
let mut parameters = BTreeMap::new();
for (k, v) in params {
parameters.insert(k.to_owned(), serde_json::value::to_value(v)?);
}
self.parameters = parameters;
Ok(())
}
pub fn remove_param(&mut self, key: &str) {
self.parameters.remove(key);
}
}
impl<T: Into<String>> From<T> for Statement {
fn from(stmt: T) -> Self {
Statement::new(stmt)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[allow(unused_variables)]
fn from_str() {
let stmt = Statement::new("MATCH n RETURN n");
}
#[test]
fn with_param() {
let statement = Statement::new("MATCH n RETURN n")
.with_param("param1", "value1").unwrap()
.with_param("param2", 2).unwrap()
.with_param("param3", 3.0).unwrap()
.with_param("param4", [0; 4]).unwrap();
assert_eq!(statement.parameters().len(), 4);
}
#[test]
fn add_param() {
let mut statement = Statement::new("MATCH n RETURN n");
statement.add_param("param1", "value1").unwrap();
statement.add_param("param2", 2).unwrap();
statement.add_param("param3", 3.0).unwrap();
statement.add_param("param4", [0; 4]).unwrap();
assert_eq!(statement.parameters().len(), 4);
}
#[test]
fn remove_param() {
let mut statement = Statement::new("MATCH n RETURN n")
.with_param("param1", "value1").unwrap()
.with_param("param2", 2).unwrap()
.with_param("param3", 3.0).unwrap()
.with_param("param4", [0; 4]).unwrap();
statement.remove_param("param1");
assert_eq!(statement.parameters().len(), 3);
}
#[test]
#[allow(unused_variables)]
fn macro_without_params() {
let stmt = cypher_stmt!("MATCH n RETURN n").unwrap();
}
#[test]
fn macro_single_param() {
let statement1 = cypher_stmt!("MATCH n RETURN n", {
"name" => "test"
}).unwrap();
let param = 1;
let statement2 = cypher_stmt!("MATCH n RETURN n", {
"value" => param
}).unwrap();
assert_eq!("test", statement1.param::<String>("name").unwrap().unwrap());
assert_eq!(param, statement2.param::<i32>("value").unwrap().unwrap());
}
#[test]
fn macro_multiple_params() {
let param = 3f32;
let statement = cypher_stmt!("MATCH n RETURN n", {
"param1" => "one",
"param2" => 2,
"param3" => param
}).unwrap();
assert_eq!("one", statement.param::<String>("param1").unwrap().unwrap());
assert_eq!(2, statement.param::<i32>("param2").unwrap().unwrap());
assert_eq!(param, statement.param::<f32>("param3").unwrap().unwrap());
}
}