Struct mimerrust::Connection
source · pub struct Connection { /* private fields */ }Expand description
Represents a connection to a MimerSQL database.
Implementations§
source§impl Connection
impl Connection
sourcepub fn open(
database: &str,
ident: &str,
password: &str,
) -> Result<Connection, MimerError>
pub fn open( database: &str, ident: &str, password: &str, ) -> Result<Connection, MimerError>
Opens a connection to a MimerSQL database.
§Errors
Returns Err holding a MimerError when a connection failed to open.
§Examples
let conn = Connection::open(db, ident, pass).unwrap();sourcepub fn get_error(&self, error_code: i32) -> MimerError
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);sourcepub fn execute_statement(&self, sqlstatement: &str) -> Result<i32, i32>
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();sourcepub fn prepare(
&mut self,
sqlstatement: &str,
option: CursorMode,
) -> Result<Statement, i32>
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();sourcepub fn begin_transaction(
&mut self,
trans_option: TransactionMode,
) -> Result<Transaction<'_>, i32>
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();sourcepub fn get_statistics(&self, counters: &mut Vec<i32>) -> Result<i32, i32>
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));Auto Trait Implementations§
impl Freeze for Connection
impl !RefUnwindSafe for Connection
impl Send for Connection
impl Sync for Connection
impl Unpin for Connection
impl !UnwindSafe for Connection
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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