2016-05-17 23:06:43 +08:00
|
|
|
use super::ffi;
|
2017-09-22 01:27:07 +08:00
|
|
|
use super::unlock_notify;
|
2019-01-25 13:43:39 +08:00
|
|
|
use super::StatementStatus;
|
2018-08-11 18:48:21 +08:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::os::raw::c_int;
|
|
|
|
use std::ptr;
|
2016-05-17 23:06:43 +08:00
|
|
|
|
|
|
|
// Private newtype for raw sqlite3_stmts that finalize themselves when dropped.
|
|
|
|
#[derive(Debug)]
|
2020-04-14 14:47:12 +08:00
|
|
|
pub struct RawStatement(*mut ffi::sqlite3_stmt, bool, crate::util::ParamIndexCache);
|
2016-05-17 23:06:43 +08:00
|
|
|
|
|
|
|
impl RawStatement {
|
2020-04-07 10:38:33 +08:00
|
|
|
pub unsafe fn new(stmt: *mut ffi::sqlite3_stmt, tail: bool) -> RawStatement {
|
2020-04-14 14:47:12 +08:00
|
|
|
RawStatement(stmt, tail, Default::default())
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 02:24:18 +08:00
|
|
|
pub fn is_null(&self) -> bool {
|
|
|
|
self.0.is_null()
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:06:43 +08:00
|
|
|
pub unsafe fn ptr(&self) -> *mut ffi::sqlite3_stmt {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
2018-05-24 03:04:13 +08:00
|
|
|
pub fn column_count(&self) -> usize {
|
|
|
|
unsafe { ffi::sqlite3_column_count(self.0) as usize }
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2018-05-24 03:04:13 +08:00
|
|
|
pub fn column_type(&self, idx: usize) -> c_int {
|
|
|
|
unsafe { ffi::sqlite3_column_type(self.0, idx as c_int) }
|
2016-05-24 09:46:51 +08:00
|
|
|
}
|
|
|
|
|
2019-03-20 03:33:36 +08:00
|
|
|
pub fn column_decltype(&self, idx: usize) -> Option<&CStr> {
|
|
|
|
unsafe {
|
|
|
|
let decltype = ffi::sqlite3_column_decltype(self.0, idx as c_int);
|
|
|
|
if decltype.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(CStr::from_ptr(decltype))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:09:44 +08:00
|
|
|
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 {
|
|
|
|
let ptr = ffi::sqlite3_column_name(self.0, 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?"
|
|
|
|
);
|
2019-06-26 03:09:44 +08:00
|
|
|
Some(CStr::from_ptr(ptr))
|
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(&self) -> c_int {
|
2017-09-22 01:27:07 +08:00
|
|
|
if cfg!(feature = "unlock_notify") {
|
2018-04-04 02:18:51 +08:00
|
|
|
let db = unsafe { ffi::sqlite3_db_handle(self.0) };
|
2017-09-22 01:27:07 +08:00
|
|
|
let mut rc;
|
|
|
|
loop {
|
|
|
|
rc = unsafe { ffi::sqlite3_step(self.0) };
|
2020-04-07 10:38:33 +08:00
|
|
|
if unsafe { !unlock_notify::is_locked(db, rc) } {
|
2018-03-31 16:22:19 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-07 10:38:33 +08:00
|
|
|
rc = unsafe { unlock_notify::wait_for_unlock_notify(db) };
|
2017-09-22 01:27:07 +08:00
|
|
|
if rc != ffi::SQLITE_OK {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.reset();
|
|
|
|
}
|
|
|
|
rc
|
|
|
|
} else {
|
|
|
|
unsafe { ffi::sqlite3_step(self.0) }
|
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&self) -> c_int {
|
|
|
|
unsafe { ffi::sqlite3_reset(self.0) }
|
|
|
|
}
|
|
|
|
|
2018-05-24 03:04:13 +08:00
|
|
|
pub fn bind_parameter_count(&self) -> usize {
|
|
|
|
unsafe { ffi::sqlite3_bind_parameter_count(self.0) as usize }
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:47:12 +08:00
|
|
|
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()) };
|
|
|
|
match r {
|
|
|
|
0 => None,
|
|
|
|
i => Some(i as usize),
|
|
|
|
}
|
|
|
|
})
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_bindings(&self) -> c_int {
|
|
|
|
unsafe { ffi::sqlite3_clear_bindings(self.0) }
|
|
|
|
}
|
|
|
|
|
2019-10-30 02:24:18 +08:00
|
|
|
pub fn sql(&self) -> Option<&CStr> {
|
|
|
|
if self.0.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(unsafe { CStr::from_ptr(ffi::sqlite3_sql(self.0)) })
|
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finalize(mut self) -> c_int {
|
|
|
|
self.finalize_()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finalize_(&mut self) -> c_int {
|
|
|
|
let r = unsafe { ffi::sqlite3_finalize(self.0) };
|
|
|
|
self.0 = ptr::null_mut();
|
|
|
|
r
|
|
|
|
}
|
2018-08-11 02:48:13 +08:00
|
|
|
|
2020-01-15 00:11:36 +08:00
|
|
|
#[cfg(feature = "modern_sqlite")] // 3.7.4
|
2018-08-11 02:48:13 +08:00
|
|
|
pub fn readonly(&self) -> bool {
|
|
|
|
unsafe { ffi::sqlite3_stmt_readonly(self.0) != 0 }
|
|
|
|
}
|
2018-08-11 02:52:11 +08:00
|
|
|
|
2019-07-28 14:53:26 +08:00
|
|
|
/// `CStr` must be freed
|
2020-01-15 00:11:36 +08:00
|
|
|
#[cfg(feature = "modern_sqlite")] // 3.14.0
|
2019-07-28 14:53:26 +08:00
|
|
|
pub unsafe fn expanded_sql(&self) -> Option<&CStr> {
|
|
|
|
let ptr = ffi::sqlite3_expanded_sql(self.0);
|
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(CStr::from_ptr(ptr))
|
2018-08-11 02:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 17:28:19 +08:00
|
|
|
|
2019-01-25 13:43:39 +08:00
|
|
|
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) }
|
|
|
|
}
|
2019-02-02 20:22:40 +08:00
|
|
|
|
2018-10-28 17:28:19 +08:00
|
|
|
pub fn has_tail(&self) -> bool {
|
2019-08-27 02:43:39 +08:00
|
|
|
self.1
|
2018-10-28 17:28:19 +08:00
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for RawStatement {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.finalize_();
|
|
|
|
}
|
|
|
|
}
|