diff --git a/src/database.rs b/src/database.rs index a5dff69..8dde348 100644 --- a/src/database.rs +++ b/src/database.rs @@ -60,10 +60,15 @@ impl<'l> Database<'l> { impl<'l> Drop for Database<'l> { #[inline] fn drop(&mut self) { - unsafe { ::raw::sqlite3_close(self.raw) }; + unsafe { raw::sqlite3_close(self.raw) }; } } +#[inline] +pub fn as_raw(database: &mut Database) -> *mut raw::sqlite3 { + database.raw +} + extern fn execute_callback(callback: *mut c_void, count: c_int, values: *mut *mut c_char, columns: *mut *mut c_char) -> c_int { diff --git a/src/error.rs b/src/error.rs index a2afcc5..2cd0958 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,8 @@ -use ResultCode; - +use raw; use std::convert::{From, Into}; +use {Database, ResultCode}; + /// An error. #[derive(Debug)] pub struct Error { @@ -9,6 +10,26 @@ pub struct Error { pub message: Option, } +impl Error { + /// Return the last occurred error if any. + pub fn last(database: &mut Database) -> Option { + unsafe { + let code = raw::sqlite3_errcode(::database::as_raw(database)); + if code == raw::SQLITE_OK { + return None; + } + let message = raw::sqlite3_errmsg(::database::as_raw(database)); + if message.is_null() { + return None; + } + Some(Error { + code: ::result::code_from_raw(code), + message: Some(c_str_to_string!(message)), + }) + } + } +} + impl From for Error where T: Into { #[inline] fn from(message: T) -> Error {