2016-05-17 23:06:43 +08:00
|
|
|
use super::ffi;
|
2019-01-25 13:43:39 +08:00
|
|
|
use super::StatementStatus;
|
2022-01-06 02:34:29 +08:00
|
|
|
use crate::util::ParamIndexCache;
|
2020-04-16 22:24:03 +08:00
|
|
|
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;
|
2020-04-16 23:22:47 +08:00
|
|
|
use std::sync::Arc;
|
2016-05-17 23:06:43 +08:00
|
|
|
|
|
|
|
// 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,
|
2020-06-27 01:35:14 +08:00
|
|
|
tail: usize,
|
2020-04-16 23:22:47 +08:00
|
|
|
// Cached indices of named parameters, computed on the fly.
|
2022-01-06 02:34:29 +08:00
|
|
|
cache: ParamIndexCache,
|
2020-04-16 23:22:47 +08:00
|
|
|
// Cached SQL (trimmed) that we use as the key when we're in the statement
|
|
|
|
// cache. This is None for statements which didn't come from the statement
|
|
|
|
// cache.
|
|
|
|
//
|
|
|
|
// This is probably the same as `self.sql()` in most cases, but we don't
|
|
|
|
// care either way -- It's a better cache key as it is anyway since it's the
|
|
|
|
// actual source we got from rust.
|
|
|
|
//
|
|
|
|
// One example of a case where the result of `sqlite_sql` and the value in
|
|
|
|
// `statement_cache_key` might differ is if the statement has a `tail`.
|
|
|
|
statement_cache_key: Option<Arc<str>>,
|
2020-04-16 23:02:39 +08:00
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
|
|
|
|
impl RawStatement {
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-06-27 01:35:14 +08:00
|
|
|
pub unsafe fn new(stmt: *mut ffi::sqlite3_stmt, tail: usize) -> RawStatement {
|
2020-04-16 23:02:39 +08:00
|
|
|
RawStatement {
|
|
|
|
ptr: stmt,
|
|
|
|
tail,
|
2022-01-06 02:34:29 +08:00
|
|
|
cache: ParamIndexCache::default(),
|
2020-04-16 23:22:47 +08:00
|
|
|
statement_cache_key: None,
|
2020-04-16 23:02:39 +08:00
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2019-10-30 02:24:18 +08:00
|
|
|
pub fn is_null(&self) -> bool {
|
2020-04-16 23:02:39 +08:00
|
|
|
self.ptr.is_null()
|
2019-10-30 02:24:18 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-16 23:22:47 +08:00
|
|
|
pub(crate) fn set_statement_cache_key(&mut self, p: impl Into<Arc<str>>) {
|
|
|
|
self.statement_cache_key = Some(p.into());
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-16 23:22:47 +08:00
|
|
|
pub(crate) fn statement_cache_key(&self) -> Option<Arc<str>> {
|
|
|
|
self.statement_cache_key.clone()
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2016-05-17 23:06:43 +08:00
|
|
|
pub unsafe fn ptr(&self) -> *mut ffi::sqlite3_stmt {
|
2020-04-16 23:02:39 +08:00
|
|
|
self.ptr
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2018-05-24 03:04:13 +08:00
|
|
|
pub fn column_count(&self) -> usize {
|
2020-04-16 23:22:47 +08:00
|
|
|
// Note: Can't cache this as it changes if the schema is altered.
|
2020-04-16 23:02:39 +08:00
|
|
|
unsafe { ffi::sqlite3_column_count(self.ptr) as usize }
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2018-05-24 03:04:13 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-06-01 05:02:18 +08:00
|
|
|
#[cfg(feature = "column_decltype")]
|
2019-03-20 03:33:36 +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);
|
2019-03-20 03:33:36 +08:00
|
|
|
if decltype.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(CStr::from_ptr(decltype))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
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 {
|
2020-04-16 23:02:39 +08:00
|
|
|
let ptr = ffi::sqlite3_column_name(self.ptr, idx);
|
2019-06-26 03:09:44 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-01-05 12:23:32 +08:00
|
|
|
#[inline]
|
|
|
|
#[cfg(not(feature = "unlock_notify"))]
|
|
|
|
pub fn step(&self) -> c_int {
|
|
|
|
unsafe { ffi::sqlite3_step(self.ptr) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "unlock_notify")]
|
2016-05-17 23:06:43 +08:00
|
|
|
pub fn step(&self) -> c_int {
|
2022-01-05 12:23:32 +08:00
|
|
|
use crate::unlock_notify;
|
2022-05-29 19:33:51 +08:00
|
|
|
let mut db = ptr::null_mut::<ffi::sqlite3>();
|
2022-01-05 12:23:32 +08:00
|
|
|
loop {
|
|
|
|
unsafe {
|
|
|
|
let mut rc = ffi::sqlite3_step(self.ptr);
|
|
|
|
// Bail out early for success and errors unrelated to locking. We
|
|
|
|
// still need check `is_locked` after this, but checking now lets us
|
|
|
|
// avoid one or two (admittedly cheap) calls into SQLite that we
|
|
|
|
// don't need to make.
|
|
|
|
if (rc & 0xff) != ffi::SQLITE_LOCKED {
|
|
|
|
break rc;
|
|
|
|
}
|
|
|
|
if db.is_null() {
|
|
|
|
db = ffi::sqlite3_db_handle(self.ptr);
|
|
|
|
}
|
|
|
|
if !unlock_notify::is_locked(db, rc) {
|
|
|
|
break rc;
|
2018-03-31 16:22:19 +08:00
|
|
|
}
|
2022-01-05 12:23:32 +08:00
|
|
|
rc = unlock_notify::wait_for_unlock_notify(db);
|
2017-09-22 01:27:07 +08:00
|
|
|
if rc != ffi::SQLITE_OK {
|
2022-01-05 12:23:32 +08:00
|
|
|
break rc;
|
2017-09-22 01:27:07 +08:00
|
|
|
}
|
|
|
|
self.reset();
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2016-05-17 23:06:43 +08:00
|
|
|
pub fn reset(&self) -> c_int {
|
2020-04-16 23:02:39 +08:00
|
|
|
unsafe { ffi::sqlite3_reset(self.ptr) }
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2018-05-24 03:04:13 +08:00
|
|
|
pub fn bind_parameter_count(&self) -> usize {
|
2020-04-17 02:08:36 +08:00
|
|
|
unsafe { ffi::sqlite3_bind_parameter_count(self.ptr) as usize }
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-14 14:47:12 +08:00
|
|
|
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()) };
|
2020-04-14 14:47:12 +08:00
|
|
|
match r {
|
|
|
|
0 => None,
|
|
|
|
i => Some(i as usize),
|
|
|
|
}
|
|
|
|
})
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 07:53:37 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn bind_parameter_name(&self, index: i32) -> Option<&CStr> {
|
|
|
|
unsafe {
|
|
|
|
let name = ffi::sqlite3_bind_parameter_name(self.ptr, index);
|
|
|
|
if name.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(CStr::from_ptr(name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2022-12-11 17:26:13 +08:00
|
|
|
pub fn clear_bindings(&self) {
|
|
|
|
unsafe {
|
|
|
|
ffi::sqlite3_clear_bindings(self.ptr);
|
|
|
|
} // rc is always SQLITE_OK
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2019-10-30 02:24:18 +08:00
|
|
|
pub fn sql(&self) -> Option<&CStr> {
|
2020-04-16 23:02:39 +08:00
|
|
|
if self.ptr.is_null() {
|
2019-10-30 02:24:18 +08:00
|
|
|
None
|
|
|
|
} else {
|
2020-04-16 23:02:39 +08:00
|
|
|
Some(unsafe { CStr::from_ptr(ffi::sqlite3_sql(self.ptr)) })
|
2019-10-30 02:24:18 +08:00
|
|
|
}
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2016-05-17 23:06:43 +08:00
|
|
|
pub fn finalize(mut self) -> c_int {
|
|
|
|
self.finalize_()
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2016-05-17 23:06:43 +08:00
|
|
|
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();
|
2016-05-17 23:06:43 +08:00
|
|
|
r
|
|
|
|
}
|
2018-08-11 02:48:13 +08:00
|
|
|
|
2021-02-28 20:19:58 +08:00
|
|
|
// does not work for PRAGMA
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2018-08-11 02:48:13 +08:00
|
|
|
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:48:13 +08:00
|
|
|
}
|
2018-08-11 02:52:11 +08:00
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-16 22:24:03 +08:00
|
|
|
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
|
|
|
}
|
2018-10-28 17:28:19 +08:00
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2019-01-25 13:43:39 +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) }
|
2019-01-25 13:43:39 +08:00
|
|
|
}
|
2019-02-02 20:22:40 +08:00
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-06-07 08:46:38 +08:00
|
|
|
#[cfg(feature = "extra_check")]
|
2018-10-28 17:28:19 +08:00
|
|
|
pub fn has_tail(&self) -> bool {
|
2020-06-27 01:35:14 +08:00
|
|
|
self.tail != 0
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-06-27 01:35:14 +08:00
|
|
|
pub fn tail(&self) -> usize {
|
2020-04-16 23:02:39 +08:00
|
|
|
self.tail
|
2018-10-28 17:28:19 +08:00
|
|
|
}
|
2022-03-18 02:58:02 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
#[cfg(feature = "modern_sqlite")] // 3.28.0
|
|
|
|
pub fn is_explain(&self) -> i32 {
|
|
|
|
unsafe { ffi::sqlite3_stmt_isexplain(self.ptr) }
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO sqlite3_normalized_sql (https://sqlite.org/c3ref/expanded_sql.html) // 3.27.0 + SQLITE_ENABLE_NORMALIZE
|
2016-05-17 23:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for RawStatement {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.finalize_();
|
|
|
|
}
|
|
|
|
}
|