Add Error::last

This commit is contained in:
Ivan Ukhov 2015-05-29 14:15:01 -04:00
parent 72ff84e686
commit 4baf4517f8
2 changed files with 29 additions and 3 deletions

View File

@ -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 {

View File

@ -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<String>,
}
impl Error {
/// Return the last occurred error if any.
pub fn last(database: &mut Database) -> Option<Error> {
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<T> From<T> for Error where T: Into<String> {
#[inline]
fn from(message: T) -> Error {