Struct mimerrust::Transaction

source ·
pub struct Transaction<'a> { /* private fields */ }
Expand description

Represents a transaction on a database connection. A Transaction will roll back by default if the object is dropped. Use the commit method to commit the changes made in the transaction.

Implementations§

source§

impl Transaction<'_>

source

pub fn commit(self) -> Result<i32, i32>

Commits a Transaction into the database, returns 0 if successful and a negative number if unsuccessful. This function consumes the transaction, meaning that the transaction object will be dropped after being called.

§Errors

Returns Err when a transaction can’t be commited.

§Examples
let mut conn = Connection::open(db, ident, pass).unwrap();
let trans_option = TransactionMode::ReadWrite;
let trans = conn.begin_transaction(trans_option).unwrap();

// Do some actions on the database like inserting or removing values in table(s)

trans.commit().unwrap();
source

pub fn rollback(self) -> Result<i32, i32>

Rolls back a Transaction to the state of the database before transaction was created, returns 0 if successful and a negative number if unsuccessful. This function consumes the transaction, meaning that the transaction object will be dropped after call.

§Errors

Returns Err when a transaction can’t be rolled back.

§Examples
let mut conn = Connection::open(db, ident, pass).unwrap();
let trans_option = TransactionMode::ReadWrite;
let trans = conn.begin_transaction(trans_option).unwrap();

// Do some actions on the database like inserting or removing values in table(s)

trans.rollback().unwrap();

Methods from Deref<Target = Connection>§

source

pub fn get_error(&self, error_code: i32) -> MimerError

Returns a MimerError given a Connection and a return code. This can be errors from the Mimer database itself, or errors from the Mimer Rust API.

§Errors

Returns Err when this method fails. It will still return a MimerError explaining what failed in this method.

§Examples
let conn = Connection::open(db, ident, pass).unwrap();

let err = match conn.execute_statement(&format!("DROP TABLE {}", "non_existing_table")) {
    Ok(_) => panic!("Execute statement succeded when it should have failed."),
    Err(ec) => conn.get_error(ec),
};

println!("{}", err);
source

pub fn execute_statement(&self, sqlstatement: &str) -> Result<i32, i32>

Executes an SQL statement on the database. Mainly used for DDL statements. The query needs to be defined with parameter values inline, and can’t contain named parameters.

§Errors

Returns Err when a statement can’t be executed, e.g. if the query contained a syntax error or if the database server is stopped.

§Examples
let conn = Connection::open(db, ident, pass).unwrap();

conn.execute_statement("INSERT INTO test_table VALUES('the number one',1)").unwrap();
source

pub fn prepare( &mut self, sqlstatement: &str, option: CursorMode, ) -> Result<Statement, i32>

Prepares a SQL statement and creates a Statement.

§Errors

Returns Err when a statement can’t be prepared, e.g. if the query contained invalid syntax.

§Examples
let mut conn = Connection::open(db, ident, pass).unwrap();

let stmnt = conn.prepare("INSERT INTO test_table VALUES(:column_1,:column_2)", CursorMode::Forward).unwrap();
source

pub fn begin_transaction( &mut self, trans_option: TransactionMode, ) -> Result<Transaction<'_>, i32>

Initiates a database transaction. This method only needs to be called if two or more database operations should participate in the transaction.

§Errors

Returns Err when a transaction can’t be started on the connection.

§Examples
let mut conn = Connection::open(db, ident, pass).unwrap();
let trans_option = TransactionMode::ReadWrite;
let trans = conn.begin_transaction(trans_option).unwrap();

// Do some actions on the database like inserting or removing values in table(s)

trans.commit().unwrap();
source

pub fn get_statistics(&self, counters: &mut Vec<i32>) -> Result<i32, i32>

Obtains server statistics information. Statistics is returned in the form of counters. Counters may either be an absolute value representing the current status or a monotonically increasing value representing the number of occurred events since the server started. An example of the former is current number of users and an example of the latter is number of server page requests. The available counter values are:

  • BSI_4K : The number of 4K pages available in the system.
  • BSI_32K : The number of 32K pages available in the system.
  • BSI_128K : The number of 128K pages available in the system.
  • BSI_PAGES_USED : The total number of pages in use.
  • BSI_4K_USED : The number of 4K pages in use.
  • BSI_32K_USED : The number of 32K pages in use.
  • BSI_128K_USED : The number of 128K pages in use.
§Errors

Returns Err on invalid counter parameter in “counters” argument, or if failed to connect to server.

§Examples
let conn = Connection::open(db, ident, pass).unwrap();

// Specify counters
let mut counters = vec![BSI_4K,BSI_4K_USED,BSI_32K,BSI_32K_USED,BSI_128K,BSI_128K_USED,BSI_PAGES_USED];

// Get statistics
conn.get_statistics(&mut counters).unwrap();

// counters now holds statistics from the database
counters.iter().for_each(|c| assert!(c >= MIMER_SUCCESS));

Trait Implementations§

source§

impl DerefMut for Transaction<'_>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<'a> Drop for Transaction<'a>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Deref for Transaction<'_>

source§

type Target = Connection

The resulting type after dereferencing.
source§

fn deref(&self) -> &Connection

Dereferences the value.

Auto Trait Implementations§

§

impl<'a> Freeze for Transaction<'a>

§

impl<'a> !RefUnwindSafe for Transaction<'a>

§

impl<'a> Send for Transaction<'a>

§

impl<'a> Sync for Transaction<'a>

§

impl<'a> Unpin for Transaction<'a>

§

impl<'a> !UnwindSafe for Transaction<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

source§

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 T
where U: TryFrom<T>,

source§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

§

fn is_within(&self, b: &G2) -> bool