Merge pull request #69 from jgallagher/gwenn-debug_verbose

Include database path and SQL when debugging SqliteConnection and SqliteStatement
This commit is contained in:
John Gallagher 2015-09-20 20:51:17 -04:00
commit 69f68b1347

View File

@ -58,7 +58,7 @@ use std::default::Default;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use std::fmt; use std::fmt;
use std::path::{Path}; use std::path::{Path,PathBuf};
use std::error; use std::error;
use std::rc::{Rc}; use std::rc::{Rc};
use std::cell::{RefCell, Cell}; use std::cell::{RefCell, Cell};
@ -151,6 +151,7 @@ fn path_to_cstring(p: &Path) -> SqliteResult<CString> {
/// prepare multiple statements at the same time). /// prepare multiple statements at the same time).
pub struct SqliteConnection { pub struct SqliteConnection {
db: RefCell<InnerSqliteConnection>, db: RefCell<InnerSqliteConnection>,
path: Option<PathBuf>,
} }
unsafe impl Send for SqliteConnection {} unsafe impl Send for SqliteConnection {}
@ -179,7 +180,7 @@ impl SqliteConnection {
-> SqliteResult<SqliteConnection> { -> SqliteResult<SqliteConnection> {
let c_path = try!(path_to_cstring(path.as_ref())); let c_path = try!(path_to_cstring(path.as_ref()));
InnerSqliteConnection::open_with_flags(&c_path, flags).map(|db| { InnerSqliteConnection::open_with_flags(&c_path, flags).map(|db| {
SqliteConnection{ db: RefCell::new(db) } SqliteConnection{ db: RefCell::new(db), path: Some(path.as_ref().to_path_buf()) }
}) })
} }
@ -190,7 +191,7 @@ impl SqliteConnection {
pub fn open_in_memory_with_flags(flags: SqliteOpenFlags) -> SqliteResult<SqliteConnection> { pub fn open_in_memory_with_flags(flags: SqliteOpenFlags) -> SqliteResult<SqliteConnection> {
let c_memory = try!(str_to_cstring(":memory:")); let c_memory = try!(str_to_cstring(":memory:"));
InnerSqliteConnection::open_with_flags(&c_memory, flags).map(|db| { InnerSqliteConnection::open_with_flags(&c_memory, flags).map(|db| {
SqliteConnection{ db: RefCell::new(db) } SqliteConnection{ db: RefCell::new(db), path: None }
}) })
} }
@ -411,7 +412,7 @@ impl SqliteConnection {
impl fmt::Debug for SqliteConnection { impl fmt::Debug for SqliteConnection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SqliteConnection()") write!(f, "SqliteConnection( path: {:?} )", &self.path)
} }
} }
@ -728,7 +729,11 @@ impl<'conn> SqliteStatement<'conn> {
impl<'conn> fmt::Debug for SqliteStatement<'conn> { impl<'conn> fmt::Debug for SqliteStatement<'conn> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Statement( conn: {:?}, stmt: {:?} )", self.conn, self.stmt) let sql = unsafe {
let c_slice = CStr::from_ptr(ffi::sqlite3_sql(self.stmt)).to_bytes();
str::from_utf8(c_slice)
};
write!(f, "SqliteStatement( conn: {:?}, stmt: {:?}, sql: {:?} )", self.conn, self.stmt, sql)
} }
} }
@ -1156,4 +1161,13 @@ mod test {
} }
assert_eq!(db.last_insert_rowid(), 10); assert_eq!(db.last_insert_rowid(), 10);
} }
#[test]
fn test_statement_debugging() {
let db = checked_memory_handle();
let query = "SELECT 12345";
let stmt = db.prepare(query).unwrap();
assert!(format!("{:?}", stmt).contains(query));
}
} }