Struct rsmgclient::Connection
source · pub struct Connection { /* private fields */ }
Expand description
Encapsulates a database connection.
Examples
use rsmgclient::{ConnectParams, Connection};
let connect_params = ConnectParams {
host: Some(String::from("localhost")),
..Default::default()
};
let mut connection = Connection::connect(&connect_params)?;
let query = "CREATE (u:User {name: 'Alice'})-[l:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, l, m";
connection.execute(query, None)?;
let records = connection.fetchall()?;
for value in &records[0].values {
println!("{}", value);
}
connection.commit()?;
Implementations§
source§impl Connection
impl Connection
sourcepub fn init()
pub fn init()
Initializes underlying mgclient.
Should be called at the beginning of each process using the client.
sourcepub fn finalize()
pub fn finalize()
Finalizes underlying mgclient.
Should be called at the end of each process using the client.
sourcepub fn lazy(&self) -> bool
pub fn lazy(&self) -> bool
Returns whether connection is executing lazily.
If false, queries are not executed lazily. After running execute
, records are immediately
pulled.
If true queries are executed lazily. After running execute
, records will only get pulled
until fetch functions are called.
sourcepub fn autocommit(&self) -> bool
pub fn autocommit(&self) -> bool
Getter for autocommit
field.
If true all queries are automatically committed.
If false queries are executed inside a transaction. Before executing first query, execute
runs begin
on database. After that user needs to commit or roll back manually, using
commit
and rollback
functions.
sourcepub fn arraysize(&self) -> u32
pub fn arraysize(&self) -> u32
Getter for arraysize
field.
Default amount of rows to get fetched when calling fetchmany
.
Initial value is 1
.
sourcepub fn status(&self) -> ConnectionStatus
pub fn status(&self) -> ConnectionStatus
Returns current connection status.
sourcepub fn summary(&self) -> Option<HashMap<String, Value>>
pub fn summary(&self) -> Option<HashMap<String, Value>>
Returns query summary if it is present.
Query summary is present after query has completed execution( all records have been fetched). Executing new query will remove previous query summary.
sourcepub fn set_autocommit(&mut self, autocommit: bool)
pub fn set_autocommit(&mut self, autocommit: bool)
sourcepub fn set_arraysize(&mut self, arraysize: u32)
pub fn set_arraysize(&mut self, arraysize: u32)
Setter for arraysize
field.
sourcepub fn connect(param_struct: &ConnectParams) -> Result<Connection, MgError>
pub fn connect(param_struct: &ConnectParams) -> Result<Connection, MgError>
Creates a connection to database using provided connection parameters.
Returns Connection
if connection to database is successfully established, otherwise
returns error with explanation what went wrong.
Examples
use rsmgclient::{ConnectParams, Connection};
let connect_params = ConnectParams {
host: Some(String::from("localhost")),
..Default::default()
};
let mut connection = Connection::connect(&connect_params)?;
sourcepub fn execute_without_results(&mut self, query: &str) -> Result<(), MgError>
pub fn execute_without_results(&mut self, query: &str) -> Result<(), MgError>
Fully Executes provided query but doesn’t return any results even if they exist.
sourcepub fn execute(
&mut self,
query: &str,
params: Option<&HashMap<String, QueryParam>>
) -> Result<Vec<String>, MgError>
pub fn execute( &mut self, query: &str, params: Option<&HashMap<String, QueryParam>> ) -> Result<Vec<String>, MgError>
Executes provided query using parameters (if provided) and returns names of columns.
After execution records need to get fetched using fetch methods. Connection needs to be in
status Ready
or InTransaction
. Error is returned if connection is not ready, query is
invalid or there was an error in communication with server.
If connection is not lazy will also fetch and store all records. If connection has autocommit set to false and is not in a transaction will also start a transaction.
sourcepub fn fetchone(&mut self) -> Result<Option<Record>, MgError>
pub fn fetchone(&mut self) -> Result<Option<Record>, MgError>
Returns next row of query results or None if there is no more data available.
Returns error if connection is not in Executing
status or if there was an error while
pulling record from database.
sourcepub fn fetchmany(&mut self, size: Option<u32>) -> Result<Vec<Record>, MgError>
pub fn fetchmany(&mut self, size: Option<u32>) -> Result<Vec<Record>, MgError>
Returns next rows of query results.
The number of rows to fetch is specified either by size
or arraysize
attribute,
size
(if provided) overrides arraysize
.
Returns error if connection is not in Executing
status or if there was an error while
pulling record from database.
sourcepub fn fetchall(&mut self) -> Result<Vec<Record>, MgError>
pub fn fetchall(&mut self) -> Result<Vec<Record>, MgError>
Returns all(remaining) rows of query results.
Returns error if connection is not in Executing
status or if there was an error while
pulling record from database.
sourcepub fn commit(&mut self) -> Result<(), MgError>
pub fn commit(&mut self) -> Result<(), MgError>
Commit any pending transaction to the database.
Returns error if there are queries that didn’t finish executing.
If autocommit
is set to true or there is no pending transaction this method does nothing.