mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 19:41:37 +08:00
Change profile() to take a Rust fn instead of an extern "C" fn.
This commit is contained in:
parent
ace5b1ebdc
commit
e6fef5107d
51
src/trace.rs
51
src/trace.rs
@ -5,6 +5,7 @@ use std::ffi::{CStr, CString};
|
|||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use super::ffi;
|
use super::ffi;
|
||||||
use {SqliteError, SqliteResult, SqliteConnection};
|
use {SqliteError, SqliteResult, SqliteConnection};
|
||||||
@ -56,10 +57,6 @@ pub fn log(err_code: c_int, msg: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The profile callback function signature.
|
|
||||||
pub type ProfileCallback =
|
|
||||||
Option<extern "C" fn (p_arg: *mut c_void, z_sql: *const c_char, nanoseconds: u64)>;
|
|
||||||
|
|
||||||
impl SqliteConnection {
|
impl SqliteConnection {
|
||||||
/// Register or clear a callback function that can be used for tracing the execution of SQL statements.
|
/// Register or clear a callback function that can be used for tracing the execution of SQL statements.
|
||||||
///
|
///
|
||||||
@ -86,16 +83,31 @@ impl SqliteConnection {
|
|||||||
///
|
///
|
||||||
/// There can only be a single profiler defined for each database connection.
|
/// There can only be a single profiler defined for each database connection.
|
||||||
/// Setting a new profiler clears the old one.
|
/// Setting a new profiler clears the old one.
|
||||||
pub fn profile(&mut self, x_profile: ProfileCallback) {
|
pub fn profile(&mut self, profile_fn: Option<fn(&str, Duration)>) {
|
||||||
|
extern "C" fn profile_callback(p_arg: *mut c_void, z_sql: *const c_char, nanoseconds: u64) {
|
||||||
|
let profile_fn: fn(&str, Duration) = unsafe { mem::transmute(p_arg) };
|
||||||
|
let c_slice = unsafe { CStr::from_ptr(z_sql).to_bytes() };
|
||||||
|
if let Ok(s) = str::from_utf8(c_slice) {
|
||||||
|
const NANOS_PER_SEC: u64 = 1_000_000_000;
|
||||||
|
|
||||||
|
let duration = Duration::new(nanoseconds / NANOS_PER_SEC,
|
||||||
|
(nanoseconds % NANOS_PER_SEC) as u32);
|
||||||
|
profile_fn(s, duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let c = self.db.borrow_mut();
|
let c = self.db.borrow_mut();
|
||||||
unsafe { ffi::sqlite3_profile(c.db(), x_profile, ptr::null_mut()); }
|
match profile_fn {
|
||||||
|
Some(f) => unsafe { ffi::sqlite3_profile(c.db(), Some(profile_callback), mem::transmute(f)) },
|
||||||
|
None => unsafe { ffi::sqlite3_profile(c.db(), None, ptr::null_mut()) },
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::io::Write;
|
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use SqliteConnection;
|
use SqliteConnection;
|
||||||
|
|
||||||
@ -127,19 +139,24 @@ mod test {
|
|||||||
assert_eq!(traced_stmts[1], "SELECT 'hello'");
|
assert_eq!(traced_stmts[1], "SELECT 'hello'");
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn profile_callback(_: *mut ::libc::c_void, sql: *const ::libc::c_char, nanoseconds: u64) {
|
|
||||||
use std::time::Duration;
|
|
||||||
unsafe {
|
|
||||||
let c_slice = ::std::ffi::CStr::from_ptr(sql).to_bytes();
|
|
||||||
let d = Duration::from_millis(nanoseconds / 1_000_000);
|
|
||||||
let _ = writeln!(::std::io::stderr(), "PROFILE: {:?} ({:?})", ::std::str::from_utf8(c_slice), d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_profile() {
|
fn test_profile() {
|
||||||
|
lazy_static! {
|
||||||
|
static ref PROFILED: Mutex<Vec<(String, Duration)>> = Mutex::new(Vec::new());
|
||||||
|
}
|
||||||
|
fn profiler(s: &str, d: Duration) {
|
||||||
|
let mut profiled = PROFILED.lock().unwrap();
|
||||||
|
profiled.push((s.to_owned(), d));
|
||||||
|
}
|
||||||
|
|
||||||
let mut db = SqliteConnection::open_in_memory().unwrap();
|
let mut db = SqliteConnection::open_in_memory().unwrap();
|
||||||
db.profile(Some(profile_callback));
|
db.profile(Some(profiler));
|
||||||
db.execute_batch("PRAGMA application_id = 1").unwrap();
|
db.execute_batch("PRAGMA application_id = 1").unwrap();
|
||||||
|
db.profile(None);
|
||||||
|
db.execute_batch("PRAGMA application_id = 2").unwrap();
|
||||||
|
|
||||||
|
let profiled = PROFILED.lock().unwrap();
|
||||||
|
assert_eq!(profiled.len(), 1);
|
||||||
|
assert_eq!(profiled[0].0, "PRAGMA application_id = 1");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user