SqlStatement

Struct SqlStatement 

Source
pub struct SqlStatement(/* private fields */);
Expand description

Binded instance of the sqlite3_stmt.

Implementations§

Source§

impl<'a> SqlStatement

Provides prepared statement functionality.

Source

pub fn execute_prepared(&mut self) -> PreparedStatementStatus

Executes the prepared statement and returns PreparedStatementStatus for data and error handling.

§Usage

let db_path = Path::new(“./example.db”); let db = Database::open(db_path).unwrap();

let statement = String::from( “SELECT * FROM example_table WHERE ID = ‘15’;” );

let mut sql = db.prepare(statement, None::<Box<dyn FnOnce(SqlitePrimaryResult, String)>>).unwrap();

while let PreparedStatementStatus::FoundRow = sql.execute_prepared() { … }

sql.kill(); db.close();

Source

pub fn get_data<T: ColumnCapabilities<'a>>( &'a self, i: usize, ) -> Result<T, MinSqliteWrapperError<'_>>

Reads the column data of the rows that returns from the SQL query.

§Panics
  • If the data type is incorrectly specified.
  • If the column index doesn’t match.
§Usage
#[derive(Debug)]
struct Item {
    id: i64,
    name: String,
    tag: String,
}

let db_path = Path::new("./example.db");
let db = Database::open(db_path).unwrap();

let statement = String::from(
    "SELECT * FROM example_table WHERE ID = '15';"
);

let mut sql = db.prepare(statement, None::<Box<dyn FnOnce(SqlitePrimaryResult, String)>>).unwrap();

while let PreparedStatementStatus::FoundRow = sql.execute_prepared() {
    println!(
        "id = {}, name = {}, tag = {}",
        sql.get_data::<i64>(0).unwrap(),
        sql.get_data::<String>(1).unwrap(),
        sql.get_data::<String>(2).unwrap(),
    );

    // OR

    println!(
        "{:?}",
        Item {
            id: sql.get_data(0).unwrap(),
            name: sql.get_data(1).unwrap(),
            tag: sql.get_data(2).unwrap(),
        }
    );
}

sql.kill();
db.close();
Source

pub fn bind_val<T: ColumnCapabilities<'a>>( &'a self, i: usize, val: T, ) -> SqlitePrimaryResult

Binds the value of a parameter to a prepared statement indicator.

Supported indicator patterns:

  • ?
  • ?NNN
  • :VVV
  • @VVV
  • $VVV

Returns SqlitePrimaryResult:Ok on success or an error code if anything goes wrong. SqlitePrimaryResult::Range is returned if the parameter index is out of range.

§IMPORTANT

The first argument isn’t index of the column. It’s simply index of the indicator and always starts at 1. If the first argument is given zero, the function will return SqlitePrimaryResult::Range.

§Usage
let db_path = Path::new("./example.db");
let db = Database::open(db_path).unwrap();

let statement = String::from(
    "SELECT * FROM example_table WHERE ID = ;"
);

let mut sql = db.prepare(statement, None::<Box<dyn FnOnce(SqlitePrimaryResult, String)>>).unwrap();

let status = sql.bind_val(1, 5);
// You can do some checks by
assert_eq!(status, SqlitePrimaryResult::Ok);
// or
if status == SqlitePrimaryResult::Range {
    panic!("Out of index on sql.bind_val!");
}

sql.kill();
db.close();
Source

pub fn kill(&self) -> SqlitePrimaryResult

Called to destroy prepared statement. This function must be called for each prepared statement. Otherwise some resource leaks might happen.

§Usage

let db_path = Path::new(“./example.db”); let db = Database::open(db_path).unwrap();

let statement = String::from( “SELECT * FROM example_table WHERE ID = ‘15’;” );

let mut sql = db.prepare(statement, None::<Box<dyn FnOnce(SqlitePrimaryResult, String)>>).unwrap();

sql.kill(); db.close();

Trait Implementations§

Source§

impl Drop for SqlStatement

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for SqlStatement

Source§

impl Sync for SqlStatement

Auto Trait Implementations§

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