Name fields of RawStatement

This commit is contained in:
Thom Chiovoloni 2020-04-16 08:02:39 -07:00 committed by Thom Chiovoloni
parent 1207a7cc06
commit 6485b324d7

View File

@ -9,32 +9,40 @@ use std::ptr;
// Private newtype for raw sqlite3_stmts that finalize themselves when dropped. // Private newtype for raw sqlite3_stmts that finalize themselves when dropped.
#[derive(Debug)] #[derive(Debug)]
pub struct RawStatement(*mut ffi::sqlite3_stmt, bool, crate::util::ParamIndexCache); pub struct RawStatement {
ptr: *mut ffi::sqlite3_stmt,
tail: bool,
cache: crate::util::ParamIndexCache,
}
impl RawStatement { impl RawStatement {
pub unsafe fn new(stmt: *mut ffi::sqlite3_stmt, tail: bool) -> RawStatement { pub unsafe fn new(stmt: *mut ffi::sqlite3_stmt, tail: bool) -> RawStatement {
RawStatement(stmt, tail, Default::default()) RawStatement {
ptr: stmt,
tail,
cache: Default::default(),
}
} }
pub fn is_null(&self) -> bool { pub fn is_null(&self) -> bool {
self.0.is_null() self.ptr.is_null()
} }
pub unsafe fn ptr(&self) -> *mut ffi::sqlite3_stmt { pub unsafe fn ptr(&self) -> *mut ffi::sqlite3_stmt {
self.0 self.ptr
} }
pub fn column_count(&self) -> usize { pub fn column_count(&self) -> usize {
unsafe { ffi::sqlite3_column_count(self.0) as usize } unsafe { ffi::sqlite3_column_count(self.ptr) as usize }
} }
pub fn column_type(&self, idx: usize) -> c_int { pub fn column_type(&self, idx: usize) -> c_int {
unsafe { ffi::sqlite3_column_type(self.0, idx as c_int) } unsafe { ffi::sqlite3_column_type(self.ptr, idx as c_int) }
} }
pub fn column_decltype(&self, idx: usize) -> Option<&CStr> { pub fn column_decltype(&self, idx: usize) -> Option<&CStr> {
unsafe { unsafe {
let decltype = ffi::sqlite3_column_decltype(self.0, idx as c_int); let decltype = ffi::sqlite3_column_decltype(self.ptr, idx as c_int);
if decltype.is_null() { if decltype.is_null() {
None None
} else { } else {
@ -49,7 +57,7 @@ impl RawStatement {
return None; return None;
} }
unsafe { unsafe {
let ptr = ffi::sqlite3_column_name(self.0, idx); let ptr = ffi::sqlite3_column_name(self.ptr, idx);
// If ptr is null here, it's an OOM, so there's probably nothing // If ptr is null here, it's an OOM, so there's probably nothing
// meaningful we can do. Just assert instead of returning None. // meaningful we can do. Just assert instead of returning None.
assert!( assert!(
@ -62,10 +70,10 @@ impl RawStatement {
pub fn step(&self) -> c_int { pub fn step(&self) -> c_int {
if cfg!(feature = "unlock_notify") { if cfg!(feature = "unlock_notify") {
let db = unsafe { ffi::sqlite3_db_handle(self.0) }; let db = unsafe { ffi::sqlite3_db_handle(self.ptr) };
let mut rc; let mut rc;
loop { loop {
rc = unsafe { ffi::sqlite3_step(self.0) }; rc = unsafe { ffi::sqlite3_step(self.ptr) };
if unsafe { !unlock_notify::is_locked(db, rc) } { if unsafe { !unlock_notify::is_locked(db, rc) } {
break; break;
} }
@ -77,21 +85,21 @@ impl RawStatement {
} }
rc rc
} else { } else {
unsafe { ffi::sqlite3_step(self.0) } unsafe { ffi::sqlite3_step(self.ptr) }
} }
} }
pub fn reset(&self) -> c_int { pub fn reset(&self) -> c_int {
unsafe { ffi::sqlite3_reset(self.0) } unsafe { ffi::sqlite3_reset(self.ptr) }
} }
pub fn bind_parameter_count(&self) -> usize { pub fn bind_parameter_count(&self) -> usize {
unsafe { ffi::sqlite3_bind_parameter_count(self.0) as usize } unsafe { ffi::sqlite3_bind_parameter_count(self.ptr) as usize }
} }
pub fn bind_parameter_index(&self, name: &str) -> Option<usize> { pub fn bind_parameter_index(&self, name: &str) -> Option<usize> {
self.2.get_or_insert_with(name, |param_cstr| { self.cache.get_or_insert_with(name, |param_cstr| {
let r = unsafe { ffi::sqlite3_bind_parameter_index(self.0, param_cstr.as_ptr()) }; let r = unsafe { ffi::sqlite3_bind_parameter_index(self.ptr, param_cstr.as_ptr()) };
match r { match r {
0 => None, 0 => None,
i => Some(i as usize), i => Some(i as usize),
@ -100,14 +108,14 @@ impl RawStatement {
} }
pub fn clear_bindings(&self) -> c_int { pub fn clear_bindings(&self) -> c_int {
unsafe { ffi::sqlite3_clear_bindings(self.0) } unsafe { ffi::sqlite3_clear_bindings(self.ptr) }
} }
pub fn sql(&self) -> Option<&CStr> { pub fn sql(&self) -> Option<&CStr> {
if self.0.is_null() { if self.ptr.is_null() {
None None
} else { } else {
Some(unsafe { CStr::from_ptr(ffi::sqlite3_sql(self.0)) }) Some(unsafe { CStr::from_ptr(ffi::sqlite3_sql(self.ptr)) })
} }
} }
@ -116,28 +124,28 @@ impl RawStatement {
} }
fn finalize_(&mut self) -> c_int { fn finalize_(&mut self) -> c_int {
let r = unsafe { ffi::sqlite3_finalize(self.0) }; let r = unsafe { ffi::sqlite3_finalize(self.ptr) };
self.0 = ptr::null_mut(); self.ptr = ptr::null_mut();
r r
} }
#[cfg(feature = "modern_sqlite")] // 3.7.4 #[cfg(feature = "modern_sqlite")] // 3.7.4
pub fn readonly(&self) -> bool { pub fn readonly(&self) -> bool {
unsafe { ffi::sqlite3_stmt_readonly(self.0) != 0 } unsafe { ffi::sqlite3_stmt_readonly(self.ptr) != 0 }
} }
#[cfg(feature = "modern_sqlite")] // 3.14.0 #[cfg(feature = "modern_sqlite")] // 3.14.0
pub(crate) fn expanded_sql(&self) -> Option<SqliteMallocString> { pub(crate) fn expanded_sql(&self) -> Option<SqliteMallocString> {
unsafe { SqliteMallocString::from_raw(ffi::sqlite3_expanded_sql(self.0)) } unsafe { SqliteMallocString::from_raw(ffi::sqlite3_expanded_sql(self.ptr)) }
} }
pub fn get_status(&self, status: StatementStatus, reset: bool) -> i32 { pub fn get_status(&self, status: StatementStatus, reset: bool) -> i32 {
assert!(!self.0.is_null()); assert!(!self.ptr.is_null());
unsafe { ffi::sqlite3_stmt_status(self.0, status as i32, reset as i32) } unsafe { ffi::sqlite3_stmt_status(self.ptr, status as i32, reset as i32) }
} }
pub fn has_tail(&self) -> bool { pub fn has_tail(&self) -> bool {
self.1 self.tail
} }
} }