0

I’m using Rust driver to connect to Memgraph instance and execute multiple MATCH / CREATE queries.

Is there any way I can execute multiple queries with method:

connection.execute_without_results ?

In other words - I dont want a sequence of queries like:

connection.execute_without_results(query1) connection.execute_without_results(query2) connection.execute_without_results(query3)

But I want them like this: connection.execute_without_results(query1;query2;query3)

I have also noticed it works for MERGE queries like:

MERGE (:Node) MERGE (:Node1) MERGE (:Node2) MERGE (:Node3)

But I also need other MATCH queries ( like create relations ).

TalyaDan
  • 63
  • 3

1 Answers1

0

The execute_without_resultsmethod in the Rust driver for Memgraph executes a single query without returning any results. To execute multiple queries in a single call, you can concatenate them into a single string and pass it as a parameter to execute_without_results

use memgraph::Connection;

pub async fn execute_multiple_queries(connection: &mut Connection, queries: &[&str]) {
    let combined_query = queries.join("; ");
    connection.execute_without_results(&combined_query).await.unwrap();
}

The execute_multiple_queries function takes a mutable reference to a Connection and a slice of query strings (queries). It joins all the queries using a semicolon as the separator and then passes the combined query to execute_without_results.

You can call the execute_multiple_queries function with your list of queries like this:

let mut connection = Connection::connect("memgraph://localhost").await.unwrap();

let queries = [
    "MATCH (n:Node) RETURN n",
    "MATCH (n:Node1) RETURN n",
    "MATCH (n:Node2) RETURN n",
    "MATCH (n:Node3) RETURN n",
];

execute_multiple_queries(&mut connection, &queries).await;

This will execute all the queries in the queries array as a single batch using the execute_without_results method. Note that the queries should be separated by a semicolon (;) in the combined query string.