#![forbid(missing_docs)]
use crate::{
bindings::{sqlite3_finalize, sqlite3_step, sqlite3_stmt},
ehandle::MinSqliteWrapperError,
operations::ColumnCapabilities,
prelude::*,
};
#[non_exhaustive]
#[repr(i8)]
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum PreparedStatementStatus {
Other(i32) = -1,
FoundRow,
Done,
}
pub struct SqlStatement(*mut sqlite3_stmt);
unsafe impl Send for SqlStatement {}
unsafe impl Sync for SqlStatement {}
impl Drop for SqlStatement {
fn drop(&mut self) {
self.kill();
}
}
impl<'a> SqlStatement {
#[inline]
pub(crate) fn new(statement: *mut sqlite3_stmt) -> Self {
Self(statement)
}
#[inline]
pub fn execute_prepared(&mut self) -> PreparedStatementStatus {
match unsafe { sqlite3_step(self.0) } {
100 => PreparedStatementStatus::FoundRow,
101 => PreparedStatementStatus::Done,
other_id => PreparedStatementStatus::Other(other_id),
}
}
#[inline]
pub fn get_data<T: ColumnCapabilities<'a>>(
&'a self,
i: usize,
) -> Result<T, MinSqliteWrapperError> {
ColumnCapabilities::get_data(self.0, i)
}
#[inline]
pub fn bind_val<T: ColumnCapabilities<'a>>(&'a self, i: usize, val: T) -> SqlitePrimaryResult {
if i == 0 {
return SqlitePrimaryResult::Range;
}
ColumnCapabilities::bind_val(val, self.0, i)
}
#[inline]
pub fn kill(&self) -> SqlitePrimaryResult {
unsafe { SqlitePrimaryResult::from(sqlite3_finalize(self.0)) }
}
}