Struct kuzu::Connection
source · pub struct Connection<'a> { /* private fields */ }
Expand description
Connections are used to interact with a Database instance.
§Concurrency
Each connection is thread-safe, and multiple connections can connect to the same Database instance in a multithreaded environment.
Note that since connections require a reference to the Database, creating or using connections in multiple threads cannot be done from a regular std::thread since the threads (and connections) could outlive the database. This can be worked around by using a scoped thread (Note: Introduced in rust 1.63. For compatibility with older versions of rust, crosssbeam_utils::thread::scope can be used instead).
Also note that write queries can only be done one at a time; the query command will return an error if another write query is in progress.
let conn = Connection::new(&db)?;
conn.query("CREATE NODE TABLE Person(name STRING, age INT32, PRIMARY KEY(name));")?;
// Write queries must be done sequentially
conn.query("CREATE (:Person {name: 'Alice', age: 25});")?;
conn.query("CREATE (:Person {name: 'Bob', age: 30});")?;
let (alice, bob) = std::thread::scope(|s| -> Result<(Vec<Value>, Vec<Value>), Error> {
let alice_thread = s.spawn(|| -> Result<Vec<Value>, Error> {
let conn = Connection::new(&db)?;
let mut result = conn.query("MATCH (a:Person) WHERE a.name = \"Alice\" RETURN a.name AS NAME, a.age AS AGE;")?;
Ok(result.next().unwrap())
});
let bob_thread = s.spawn(|| -> Result<Vec<Value>, Error> {
let conn = Connection::new(&db)?;
let mut result = conn.query(
"MATCH (a:Person) WHERE a.name = \"Bob\" RETURN a.name AS NAME, a.age AS AGE;",
)?;
Ok(result.next().unwrap())
});
Ok((alice_thread.join().unwrap()?, bob_thread.join().unwrap()?))
})?;
assert_eq!(alice, vec!["Alice".into(), 25.into()]);
assert_eq!(bob, vec!["Bob".into(), 30.into()]);
temp_dir.close()?;
Ok(())
Implementations§
source§impl<'a> Connection<'a>
impl<'a> Connection<'a>
sourcepub fn new(database: &'a Database) -> Result<Self, Error>
pub fn new(database: &'a Database) -> Result<Self, Error>
Creates a connection to the database.
§Arguments
database
: A reference to the database instance to which this connection will be connected.
sourcepub fn set_max_num_threads_for_exec(&mut self, num_threads: u64)
pub fn set_max_num_threads_for_exec(&mut self, num_threads: u64)
Sets the maximum number of threads to use for execution in the current connection
§Arguments
num_threads
: The maximum number of threads to use for execution in the current connection
sourcepub fn get_max_num_threads_for_exec(&self) -> u64
pub fn get_max_num_threads_for_exec(&self) -> u64
Returns the maximum number of threads used for execution in the current connection
sourcepub fn prepare(&self, query: &str) -> Result<PreparedStatement, Error>
pub fn prepare(&self, query: &str) -> Result<PreparedStatement, Error>
Prepares the given query and returns the prepared statement.
§Arguments
query
: The query to prepare. See https://kuzudb.com/docs/cypher for details on the query format
sourcepub fn query(&self, query: &str) -> Result<QueryResult, Error>
pub fn query(&self, query: &str) -> Result<QueryResult, Error>
Executes the given query and returns the result.
§Arguments
query
: The query to execute. See https://kuzudb.com/docs/cypher for details on the query format
sourcepub fn execute(
&self,
prepared_statement: &mut PreparedStatement,
params: Vec<(&str, Value)>
) -> Result<QueryResult, Error>
pub fn execute( &self, prepared_statement: &mut PreparedStatement, params: Vec<(&str, Value)> ) -> Result<QueryResult, Error>
Executes the given prepared statement with args and returns the result.
§Arguments
prepared_statement
: The prepared statement to execute
sourcepub fn interrupt(&self) -> Result<(), Error>
pub fn interrupt(&self) -> Result<(), Error>
Interrupts all queries currently executing within this connection
sourcepub fn set_query_timeout(&self, timeout_ms: u64)
pub fn set_query_timeout(&self, timeout_ms: u64)
Sets the query timeout value of the current connection
A value of zero (the default) disables the timeout.