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

source

pub fn init()

Initializes underlying mgclient.

Should be called at the beginning of each process using the client.

source

pub fn finalize()

Finalizes underlying mgclient.

Should be called at the end of each process using the client.

source

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.

source

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.

source

pub fn arraysize(&self) -> u32

Getter for arraysize field.

Default amount of rows to get fetched when calling fetchmany. Initial value is 1.

source

pub fn status(&self) -> ConnectionStatus

Returns current connection status.

source

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.

source

pub fn set_lazy(&mut self, lazy: bool)

Setter for lazy field.

Panics

Panics if connection is not in a Ready status.

source

pub fn set_autocommit(&mut self, autocommit: bool)

Setter for autocommit field.

Panics

Panics if connection is not in a Ready status.

source

pub fn set_arraysize(&mut self, arraysize: u32)

Setter for arraysize field.

source

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)?;
source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn rollback(&mut self) -> Result<(), MgError>

Rollback 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.

source

pub fn close(&mut self)

Closes the connection.

The connection will be unusable from this point forward. Any operation on connection will return error.

Trait Implementations§

source§

impl Drop for Connection

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.