Skip to content

Commit f18c3e8

Browse files
authored
Change query entrypoints to use Into<Query> instead of just Query (#213)
* Change query entrypoints to use `Into<Query>` instead of just `Query` * Change examples/tests to remove explicit calls to `query`
1 parent 345e2cc commit f18c3e8

File tree

5 files changed

+38
-43
lines changed

5 files changed

+38
-43
lines changed

lib/examples/concurrent_writes.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use futures::stream::{self, StreamExt, TryStreamExt};
2-
use neo4rs::{query, ConfigBuilder, Graph};
2+
use neo4rs::{ConfigBuilder, Graph};
33

44
#[tokio::main]
55
async fn main() {
@@ -35,7 +35,7 @@ async fn main() {
3535

3636
async fn work(i: u64, graph: Graph) -> (u64, u64, u64) {
3737
graph
38-
.run(query(
38+
.run(
3939
"
4040
CREATE
4141
(dan:Person {name: 'Dan'}),
@@ -77,12 +77,12 @@ CREATE
7777
(elsa)-[:BUYS {amount: 3}]->(chocolate),
7878
(elsa)-[:BUYS {amount: 3}]->(milk)
7979
",
80-
))
80+
)
8181
.await
8282
.unwrap();
8383

8484
let node_count = graph
85-
.execute(query("MATCH (n) RETURN count(n) AS count"))
85+
.execute("MATCH (n) RETURN count(n) AS count")
8686
.await
8787
.unwrap()
8888
.column_into_stream::<u64>("count")
@@ -91,7 +91,7 @@ CREATE
9191
.unwrap();
9292

9393
let rel_count = graph
94-
.execute(query("MATCH ()-[r]->() RETURN count(r) AS count"))
94+
.execute("MATCH ()-[r]->() RETURN count(r) AS count")
9595
.await
9696
.unwrap()
9797
.column_into_stream::<u64>("count")

lib/src/graph.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ impl Graph {
158158
/// Use [`Graph::run`] for cases where you just want a write operation
159159
///
160160
/// use [`Graph::execute`] when you are interested in the result stream
161-
pub async fn run(&self, q: Query) -> Result<()> {
162-
self.impl_run_on(self.config.db.clone(), q, Operation::Write)
161+
pub async fn run(&self, q: impl Into<Query>) -> Result<()> {
162+
self.impl_run_on(self.config.db.clone(), q.into(), Operation::Write)
163163
.await
164164
}
165165

@@ -178,15 +178,16 @@ impl Graph {
178178
pub async fn run_on(
179179
&self,
180180
db: impl Into<Database>,
181-
q: Query,
181+
q: impl Into<Query>,
182182
operation: Operation,
183183
) -> Result<()> {
184-
self.impl_run_on(Some(db.into()), q, operation).await
184+
self.impl_run_on(Some(db.into()), q.into(), operation).await
185185
}
186186

187187
#[cfg(not(feature = "unstable-bolt-protocol-impl-v2"))]
188-
pub async fn run_on(&self, db: impl Into<Database>, q: Query) -> Result<()> {
189-
self.impl_run_on(Some(db.into()), q, Operation::Write).await
188+
pub async fn run_on(&self, db: impl Into<Database>, q: impl Into<Query>) -> Result<()> {
189+
self.impl_run_on(Some(db.into()), q.into(), Operation::Write)
190+
.await
190191
}
191192

192193
#[allow(unused_variables)]
@@ -229,8 +230,8 @@ impl Graph {
229230
/// All errors with the `Transient` error class as well as a few other error classes are considered retryable.
230231
/// This includes errors during a leader election or when the transaction resources on the server (memory, handles, ...) are exhausted.
231232
/// Retries happen with an exponential backoff until a retry delay exceeds 60s, at which point the query fails with the last error as it would without any retry.
232-
pub async fn execute(&self, q: Query) -> Result<DetachedRowStream> {
233-
self.impl_execute_on(self.config.db.clone(), q, Operation::Write)
233+
pub async fn execute(&self, q: impl Into<Query>) -> Result<DetachedRowStream> {
234+
self.impl_execute_on(self.config.db.clone(), q.into(), Operation::Write)
234235
.await
235236
}
236237

@@ -240,8 +241,8 @@ impl Graph {
240241
/// All errors with the `Transient` error class as well as a few other error classes are considered retryable.
241242
/// This includes errors during a leader election or when the transaction resources on the server (memory, handles, ...) are exhausted.
242243
/// Retries happen with an exponential backoff until a retry delay exceeds 60s, at which point the query fails with the last error as it would without any retry.
243-
pub async fn execute_read(&self, q: Query) -> Result<DetachedRowStream> {
244-
self.impl_execute_on(self.config.db.clone(), q, Operation::Read)
244+
pub async fn execute_read(&self, q: impl Into<Query>) -> Result<DetachedRowStream> {
245+
self.impl_execute_on(self.config.db.clone(), q.into(), Operation::Read)
245246
.await
246247
}
247248

@@ -255,10 +256,11 @@ impl Graph {
255256
pub async fn execute_on(
256257
&self,
257258
db: impl Into<Database>,
258-
q: Query,
259+
q: impl Into<Query>,
259260
operation: Operation,
260261
) -> Result<DetachedRowStream> {
261-
self.impl_execute_on(Some(db.into()), q, operation).await
262+
self.impl_execute_on(Some(db.into()), q.into(), operation)
263+
.await
262264
}
263265

264266
/// Executes a query on the provided database and returns a [`DetachedRowStream`]
@@ -267,8 +269,12 @@ impl Graph {
267269
/// All errors with the `Transient` error class as well as a few other error classes are considered retryable.
268270
/// This includes errors during a leader election or when the transaction resources on the server (memory, handles, ...) are exhausted.
269271
#[cfg(not(feature = "unstable-bolt-protocol-impl-v2"))]
270-
pub async fn execute_on(&self, db: impl Into<Database>, q: Query) -> Result<DetachedRowStream> {
271-
self.impl_execute_on(Some(db.into()), q, Operation::Write)
272+
pub async fn execute_on(
273+
&self,
274+
db: impl Into<Database>,
275+
q: impl Into<Query>,
276+
) -> Result<DetachedRowStream> {
277+
self.impl_execute_on(Some(db.into()), q.into(), Operation::Write)
272278
.await
273279
}
274280

lib/src/txn.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ impl Txn {
5252
}
5353

5454
/// Runs a single query and discards the stream.
55-
pub async fn run(&mut self, q: Query) -> Result<()> {
56-
let mut query = q.clone();
55+
pub async fn run(&mut self, q: impl Into<Query>) -> Result<()> {
56+
let mut query = q.into();
5757
if let Some(db) = self.db.as_ref() {
5858
query = query.extra("db", db.to_string());
5959
}
@@ -68,8 +68,8 @@ impl Txn {
6868
}
6969

7070
/// Executes a query and returns a [`RowStream`]
71-
pub async fn execute(&mut self, q: Query) -> Result<RowStream> {
72-
let mut query = q.clone();
71+
pub async fn execute(&mut self, q: impl Into<Query>) -> Result<RowStream> {
72+
let mut query = q.into();
7373
if let Some(db) = self.db.as_ref() {
7474
query = query.extra("db", db.to_string());
7575
}

lib/tests/node_property_parsing.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use chrono::{DateTime, FixedOffset};
2-
use neo4rs::{query, Node, Point2D, Point3D};
2+
use neo4rs::{Node, Point2D, Point3D};
33

44
mod container;
55

@@ -9,31 +9,25 @@ async fn node_property_parsing() {
99
let graph = neo4j.graph();
1010

1111
graph
12-
.run(query(
12+
.run(
1313
"CREATE
1414
(:Datetime {p1:DATETIME('2024-12-31T08:10:35')}),
1515
(:Point2D {a:Point ({x:2,y:3})}),
1616
(:Point3D {a:Point ({x:3,y:4,z:5})})
1717
",
18-
))
18+
)
1919
.await
2020
.unwrap();
2121

22-
let mut result = graph
23-
.execute(query("MATCH (p:DateTime) RETURN p"))
24-
.await
25-
.unwrap();
22+
let mut result = graph.execute("MATCH (p:DateTime) RETURN p").await.unwrap();
2623

2724
while let Ok(Some(row)) = result.next().await {
2825
let node: Node = row.get("p").unwrap();
2926
let p1 = node.get::<DateTime<FixedOffset>>("p1").unwrap();
3027
assert_eq!(p1.timestamp(), 1735632635);
3128
}
3229

33-
let mut result = graph
34-
.execute(query("MATCH (p:Point2D) RETURN p"))
35-
.await
36-
.unwrap();
30+
let mut result = graph.execute("MATCH (p:Point2D) RETURN p").await.unwrap();
3731

3832
while let Ok(Some(row)) = result.next().await {
3933
let node: Node = row.get("p").unwrap();
@@ -42,10 +36,7 @@ async fn node_property_parsing() {
4236
assert_eq!(p1.y(), 3.0);
4337
}
4438

45-
let mut result = graph
46-
.execute(query("MATCH (p:Point3D) RETURN p"))
47-
.await
48-
.unwrap();
39+
let mut result = graph.execute("MATCH (p:Point3D) RETURN p").await.unwrap();
4940

5041
while let Ok(Some(row)) = result.next().await {
5142
let node: Node = row.get("p").unwrap();

lib/tests/use_default_db.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use futures::TryStreamExt;
2-
use neo4rs::*;
2+
use neo4rs::{query, Operation};
33

44
mod container;
55

@@ -27,13 +27,11 @@ async fn use_default_db() {
2727

2828
#[cfg(feature = "unstable-bolt-protocol-impl-v2")]
2929
let query_stream = graph
30-
.execute_on("system", query("SHOW DEFAULT DATABASE"), Operation::Read)
30+
.execute_on("system", "SHOW DEFAULT DATABASE", Operation::Read)
3131
.await;
3232

3333
#[cfg(not(feature = "unstable-bolt-protocol-impl-v2"))]
34-
let query_stream = graph
35-
.execute_on("system", query("SHOW DEFAULT DATABASE"))
36-
.await;
34+
let query_stream = graph.execute_on("system", "SHOW DEFAULT DATABASE").await;
3735

3836
let default_db = query_stream
3937
.unwrap()

0 commit comments

Comments
 (0)