rusqlite/src/raw_statement.rs

157 lines
4.3 KiB
Rust
Raw Normal View History

use super::ffi;
use super::unlock_notify;
use super::StatementStatus;
#[cfg(feature = "modern_sqlite")]
use crate::util::SqliteMallocString;
2018-08-11 18:48:21 +08:00
use std::ffi::CStr;
use std::os::raw::c_int;
use std::ptr;
// Private newtype for raw sqlite3_stmts that finalize themselves when dropped.
#[derive(Debug)]
2020-04-16 23:02:39 +08:00
pub struct RawStatement {
ptr: *mut ffi::sqlite3_stmt,
tail: bool,
cache: crate::util::ParamIndexCache,
}
impl RawStatement {
pub unsafe fn new(stmt: *mut ffi::sqlite3_stmt, tail: bool) -> RawStatement {
2020-04-16 23:02:39 +08:00
RawStatement {
ptr: stmt,
tail,
cache: Default::default(),
}
}
pub fn is_null(&self) -> bool {
2020-04-16 23:02:39 +08:00
self.ptr.is_null()
}
pub unsafe fn ptr(&self) -> *mut ffi::sqlite3_stmt {
2020-04-16 23:02:39 +08:00
self.ptr
}
pub fn column_count(&self) -> usize {
2020-04-16 23:02:39 +08:00
unsafe { ffi::sqlite3_column_count(self.ptr) as usize }
}
pub fn column_type(&self, idx: usize) -> c_int {
2020-04-16 23:02:39 +08:00
unsafe { ffi::sqlite3_column_type(self.ptr, idx as c_int) }
2016-05-24 09:46:51 +08:00
}
pub fn column_decltype(&self, idx: usize) -> Option<&CStr> {
unsafe {
2020-04-16 23:02:39 +08:00
let decltype = ffi::sqlite3_column_decltype(self.ptr, idx as c_int);
if decltype.is_null() {
None
} else {
Some(CStr::from_ptr(decltype))
}
}
}
pub fn column_name(&self, idx: usize) -> Option<&CStr> {
let idx = idx as c_int;
if idx < 0 || idx >= self.column_count() as c_int {
return None;
}
unsafe {
2020-04-16 23:02:39 +08:00
let ptr = ffi::sqlite3_column_name(self.ptr, idx);
// If ptr is null here, it's an OOM, so there's probably nothing
// meaningful we can do. Just assert instead of returning None.
2019-07-11 03:10:12 +08:00
assert!(
!ptr.is_null(),
"Null pointer from sqlite3_column_name: Out of memory?"
);
Some(CStr::from_ptr(ptr))
}
}
pub fn step(&self) -> c_int {
if cfg!(feature = "unlock_notify") {
2020-04-16 23:02:39 +08:00
let db = unsafe { ffi::sqlite3_db_handle(self.ptr) };
let mut rc;
loop {
2020-04-16 23:02:39 +08:00
rc = unsafe { ffi::sqlite3_step(self.ptr) };
if unsafe { !unlock_notify::is_locked(db, rc) } {
2018-03-31 16:22:19 +08:00
break;
}
rc = unsafe { unlock_notify::wait_for_unlock_notify(db) };
if rc != ffi::SQLITE_OK {
break;
}
self.reset();
}
rc
} else {
2020-04-16 23:02:39 +08:00
unsafe { ffi::sqlite3_step(self.ptr) }
}
}
pub fn reset(&self) -> c_int {
2020-04-16 23:02:39 +08:00
unsafe { ffi::sqlite3_reset(self.ptr) }
}
pub fn bind_parameter_count(&self) -> usize {
2020-04-16 23:02:39 +08:00
unsafe { ffi::sqlite3_bind_parameter_count(self.ptr) as usize }
}
pub fn bind_parameter_index(&self, name: &str) -> Option<usize> {
2020-04-16 23:02:39 +08:00
self.cache.get_or_insert_with(name, |param_cstr| {
let r = unsafe { ffi::sqlite3_bind_parameter_index(self.ptr, param_cstr.as_ptr()) };
match r {
0 => None,
i => Some(i as usize),
}
})
}
pub fn clear_bindings(&self) -> c_int {
2020-04-16 23:02:39 +08:00
unsafe { ffi::sqlite3_clear_bindings(self.ptr) }
}
pub fn sql(&self) -> Option<&CStr> {
2020-04-16 23:02:39 +08:00
if self.ptr.is_null() {
None
} else {
2020-04-16 23:02:39 +08:00
Some(unsafe { CStr::from_ptr(ffi::sqlite3_sql(self.ptr)) })
}
}
pub fn finalize(mut self) -> c_int {
self.finalize_()
}
fn finalize_(&mut self) -> c_int {
2020-04-16 23:02:39 +08:00
let r = unsafe { ffi::sqlite3_finalize(self.ptr) };
self.ptr = ptr::null_mut();
r
}
#[cfg(feature = "modern_sqlite")] // 3.7.4
pub fn readonly(&self) -> bool {
2020-04-16 23:02:39 +08:00
unsafe { ffi::sqlite3_stmt_readonly(self.ptr) != 0 }
}
2018-08-11 02:52:11 +08:00
#[cfg(feature = "modern_sqlite")] // 3.14.0
pub(crate) fn expanded_sql(&self) -> Option<SqliteMallocString> {
2020-04-16 23:02:39 +08:00
unsafe { SqliteMallocString::from_raw(ffi::sqlite3_expanded_sql(self.ptr)) }
2018-08-11 02:52:11 +08:00
}
pub fn get_status(&self, status: StatementStatus, reset: bool) -> i32 {
2020-04-16 23:02:39 +08:00
assert!(!self.ptr.is_null());
unsafe { ffi::sqlite3_stmt_status(self.ptr, status as i32, reset as i32) }
}
pub fn has_tail(&self) -> bool {
2020-04-16 23:02:39 +08:00
self.tail
}
}
impl Drop for RawStatement {
fn drop(&mut self) {
self.finalize_();
}
}