Skip to content

Commit

Permalink
Name fields of RawStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom Chiovoloni authored and thomcc committed Apr 16, 2020
1 parent 1207a7c commit 6485b32
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions src/raw_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,40 @@ use std::ptr;

// Private newtype for raw sqlite3_stmts that finalize themselves when dropped.
#[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 {
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 {
self.0.is_null()
self.ptr.is_null()
}

pub unsafe fn ptr(&self) -> *mut ffi::sqlite3_stmt {
self.0
self.ptr
}

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 {
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> {
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() {
None
} else {
Expand All @@ -49,7 +57,7 @@ impl RawStatement {
return None;
}
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
// meaningful we can do. Just assert instead of returning None.
assert!(
Expand All @@ -62,10 +70,10 @@ impl RawStatement {

pub fn step(&self) -> c_int {
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;
loop {
rc = unsafe { ffi::sqlite3_step(self.0) };
rc = unsafe { ffi::sqlite3_step(self.ptr) };
if unsafe { !unlock_notify::is_locked(db, rc) } {
break;
}
Expand All @@ -77,21 +85,21 @@ impl RawStatement {
}
rc
} else {
unsafe { ffi::sqlite3_step(self.0) }
unsafe { ffi::sqlite3_step(self.ptr) }
}
}

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 {
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> {
self.2.get_or_insert_with(name, |param_cstr| {
let r = unsafe { ffi::sqlite3_bind_parameter_index(self.0, param_cstr.as_ptr()) };
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),
Expand All @@ -100,14 +108,14 @@ impl RawStatement {
}

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> {
if self.0.is_null() {
if self.ptr.is_null() {
None
} else {
Some(unsafe { CStr::from_ptr(ffi::sqlite3_sql(self.0)) })
Some(unsafe { CStr::from_ptr(ffi::sqlite3_sql(self.ptr)) })
}
}

Expand All @@ -116,28 +124,28 @@ impl RawStatement {
}

fn finalize_(&mut self) -> c_int {
let r = unsafe { ffi::sqlite3_finalize(self.0) };
self.0 = ptr::null_mut();
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 {
unsafe { ffi::sqlite3_stmt_readonly(self.0) != 0 }
unsafe { ffi::sqlite3_stmt_readonly(self.ptr) != 0 }
}

#[cfg(feature = "modern_sqlite")] // 3.14.0
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 {
assert!(!self.0.is_null());
unsafe { ffi::sqlite3_stmt_status(self.0, status as i32, reset as i32) }
assert!(!self.ptr.is_null());
unsafe { ffi::sqlite3_stmt_status(self.ptr, status as i32, reset as i32) }
}

pub fn has_tail(&self) -> bool {
self.1
self.tail
}
}

Expand Down

0 comments on commit 6485b32

Please sign in to comment.