Merge pull request #464 from thomcc/stmt-status

Add support for querying sqlite_stmt_status
This commit is contained in:
gwenn 2019-01-25 18:21:45 +01:00 committed by GitHub
commit a1654e7ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 1 deletions

View File

@ -90,7 +90,7 @@ use crate::error::{error_from_handle, error_from_sqlite_code};
use crate::raw_statement::RawStatement;
use crate::types::{ToSql, ValueRef};
pub use crate::statement::Statement;
pub use crate::statement::{Statement, StatementStatus};
pub use crate::row::{AndThenRows, MappedRows, Row, RowIndex, Rows};

View File

@ -1,5 +1,6 @@
use super::ffi;
use super::unlock_notify;
use super::StatementStatus;
use std::ffi::CStr;
use std::os::raw::c_int;
use std::ptr;
@ -100,6 +101,11 @@ impl RawStatement {
}
}
}
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) }
}
}
impl Drop for RawStatement {

View File

@ -569,6 +569,17 @@ impl<'conn> Statement<'conn> {
.map(|s| str::from_utf8_unchecked(s.to_bytes()))
}
}
/// Get the value for one of the status counters for this statement.
pub fn get_status(&self, status: StatementStatus) -> i32 {
self.stmt.get_status(status, false)
}
/// Reset the value of one of the status counters for this statement,
/// returning the value it had before resetting.
pub fn reset_status(&self, status: StatementStatus) -> i32 {
self.stmt.get_status(status, true)
}
}
impl<'conn> Into<RawStatement> for Statement<'conn> {
@ -670,6 +681,32 @@ impl<'conn> Statement<'conn> {
}
}
/// Prepared statement status counters.
///
/// See https://www.sqlite.org/c3ref/c_stmtstatus_counter.html
/// for explanations of each.
///
/// Note that depending on your version of SQLite, all of these
/// may not be available.
#[repr(i32)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum StatementStatus {
/// Equivalent to SQLITE_STMTSTATUS_FULLSCAN_STEP
FullscanStep = 1,
/// Equivalent to SQLITE_STMTSTATUS_SORT
Sort = 2,
/// Equivalent to SQLITE_STMTSTATUS_AUTOINDEX
AutoIndex = 3,
/// Equivalent to SQLITE_STMTSTATUS_VM_STEP
VmStep = 4,
/// Equivalent to SQLITE_STMTSTATUS_REPREPARE
RePrepare = 5,
/// Equivalent to SQLITE_STMTSTATUS_RUN
Run = 6,
/// Equivalent to SQLITE_STMTSTATUS_MEMUSED
MemUsed = 99,
}
#[cfg(test)]
mod test {
use crate::{Connection, Error, Result, NO_PARAMS};