Merge pull request #39 from jgallagher/add-get-checked

Add get_checked to SqliteRow.
This commit is contained in:
John Gallagher 2015-05-11 20:17:18 -04:00
commit ad3e805357
5 changed files with 132 additions and 9 deletions

View File

@ -25,4 +25,4 @@ tempdir = "~0.3.4"
[dependencies.libsqlite3-sys] [dependencies.libsqlite3-sys]
path = "libsqlite3-sys" path = "libsqlite3-sys"
version = "0.0.13" version = "0.1.0"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "libsqlite3-sys" name = "libsqlite3-sys"
version = "0.0.13" version = "0.1.0"
authors = ["John Gallagher <jgallagher@bignerdranch.com>"] authors = ["John Gallagher <jgallagher@bignerdranch.com>"]
repository = "https://github.com/jgallagher/rusqlite" repository = "https://github.com/jgallagher/rusqlite"
description = "Native bindings to the libsqlite3 library" description = "Native bindings to the libsqlite3 library"

View File

@ -42,6 +42,10 @@ pub const SQLITE_ROW : c_int = 100;
pub const SQLITE_DONE : c_int = 101; pub const SQLITE_DONE : c_int = 101;
// SQLite datatype constants. // SQLite datatype constants.
pub const SQLITE_INTEGER : c_int = 1;
pub const SQLITE_FLOAT : c_int = 2;
pub const SQLITE_TEXT : c_int = 3;
pub const SQLITE_BLOB : c_int = 4;
pub const SQLITE_NULL : c_int = 5; pub const SQLITE_NULL : c_int = 5;
pub type SqliteDestructor = extern "C" fn(*mut c_void); pub type SqliteDestructor = extern "C" fn(*mut c_void);

View File

@ -835,6 +835,30 @@ impl<'stmt> SqliteRow<'stmt> {
self.get_opt(idx).unwrap() self.get_opt(idx).unwrap()
} }
/// Get the value of a particular column of the result row.
///
/// ## Failure
///
/// Returns a `SQLITE_MISMATCH`-coded `SqliteError` if the underlying SQLite column
/// type is not a valid type as a source for `T`.
///
/// Panics if `idx` is outside the range of columns in the returned query or if this row
/// is stale.
pub fn get_checked<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> {
let valid_column_type = unsafe {
T::column_has_valid_sqlite_type(self.stmt.stmt, idx)
};
if valid_column_type {
Ok(self.get(idx))
} else {
Err(SqliteError{
code: ffi::SQLITE_MISMATCH,
message: "Invalid column type".to_string(),
})
}
}
/// Attempt to get the value of a particular column of the result row. /// Attempt to get the value of a particular column of the result row.
/// ///
/// ## Failure /// ## Failure

View File

@ -62,6 +62,9 @@ use super::ffi;
use super::{SqliteResult, SqliteError, str_to_cstring}; use super::{SqliteResult, SqliteError, str_to_cstring};
pub use ffi::sqlite3_stmt as sqlite3_stmt; pub use ffi::sqlite3_stmt as sqlite3_stmt;
pub use ffi::sqlite3_column_type as sqlite3_column_type;
pub use ffi::{SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL};
const SQLITE_DATETIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S"; const SQLITE_DATETIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S";
@ -73,6 +76,14 @@ pub trait ToSql {
/// A trait for types that can be created from a SQLite value. /// A trait for types that can be created from a SQLite value.
pub trait FromSql { pub trait FromSql {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<Self>; unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<Self>;
/// FromSql types can implement this method and use sqlite3_column_type to check that
/// the type reported by SQLite matches a type suitable for Self. This method is used
/// by `SqliteRow::get_checked` to confirm that the column contains a valid type before
/// attempting to retrieve the value.
unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool {
true
}
} }
macro_rules! raw_to_impl( macro_rules! raw_to_impl(
@ -161,18 +172,22 @@ impl ToSql for Null {
} }
macro_rules! raw_from_impl( macro_rules! raw_from_impl(
($t:ty, $f:ident) => ( ($t:ty, $f:ident, $c:expr) => (
impl FromSql for $t { impl FromSql for $t {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<$t> { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<$t> {
Ok(ffi::$f(stmt, col)) Ok(ffi::$f(stmt, col))
} }
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
sqlite3_column_type(stmt, col) == $c
}
} }
) )
); );
raw_from_impl!(c_int, sqlite3_column_int); raw_from_impl!(c_int, sqlite3_column_int, ffi::SQLITE_INTEGER);
raw_from_impl!(i64, sqlite3_column_int64); raw_from_impl!(i64, sqlite3_column_int64, ffi::SQLITE_INTEGER);
raw_from_impl!(c_double, sqlite3_column_double); raw_from_impl!(c_double, sqlite3_column_double, ffi::SQLITE_FLOAT);
impl FromSql for String { impl FromSql for String {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<String> { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<String> {
@ -187,6 +202,10 @@ impl FromSql for String {
.map_err(|e| { SqliteError{code: 0, message: e.to_string()} }) .map_err(|e| { SqliteError{code: 0, message: e.to_string()} })
} }
} }
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
sqlite3_column_type(stmt, col) == ffi::SQLITE_TEXT
}
} }
impl FromSql for Vec<u8> { impl FromSql for Vec<u8> {
@ -202,6 +221,10 @@ impl FromSql for Vec<u8> {
Ok(from_raw_parts(mem::transmute(c_blob), len).to_vec()) Ok(from_raw_parts(mem::transmute(c_blob), len).to_vec())
} }
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
sqlite3_column_type(stmt, col) == ffi::SQLITE_BLOB
}
} }
impl FromSql for time::Timespec { impl FromSql for time::Timespec {
@ -216,26 +239,37 @@ impl FromSql for time::Timespec {
}) })
}) })
} }
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
String::column_has_valid_sqlite_type(stmt, col)
}
} }
impl<T: FromSql> FromSql for Option<T> { impl<T: FromSql> FromSql for Option<T> {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<Option<T>> { unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> SqliteResult<Option<T>> {
if ffi::sqlite3_column_type(stmt, col) == ffi::SQLITE_NULL { if sqlite3_column_type(stmt, col) == ffi::SQLITE_NULL {
Ok(None) Ok(None)
} else { } else {
FromSql::column_result(stmt, col).map(|t| Some(t)) FromSql::column_result(stmt, col).map(|t| Some(t))
} }
} }
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
sqlite3_column_type(stmt, col) == ffi::SQLITE_NULL ||
T::column_has_valid_sqlite_type(stmt, col)
}
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use SqliteConnection; use SqliteConnection;
use ffi;
use super::time; use super::time;
use libc::{c_int, c_double};
fn checked_memory_handle() -> SqliteConnection { fn checked_memory_handle() -> SqliteConnection {
let db = SqliteConnection::open_in_memory().unwrap(); let db = SqliteConnection::open_in_memory().unwrap();
db.execute_batch("CREATE TABLE foo (b BLOB, t TEXT)").unwrap(); db.execute_batch("CREATE TABLE foo (b BLOB, t TEXT, i INTEGER, f FLOAT, n)").unwrap();
db db
} }
@ -297,4 +331,65 @@ mod test {
assert!(s2.is_none()); assert!(s2.is_none());
assert_eq!(b, b2); assert_eq!(b, b2);
} }
#[test]
fn test_mismatched_types() {
let db = checked_memory_handle();
db.execute("INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)", &[]).unwrap();
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
let mut rows = stmt.query(&[]).unwrap();
let row = rows.next().unwrap().unwrap();
// check the correct types come back as expected
assert_eq!(vec![1,2], row.get_checked::<Vec<u8>>(0).unwrap());
assert_eq!("text", row.get_checked::<String>(1).unwrap());
assert_eq!(1, row.get_checked::<c_int>(2).unwrap());
assert_eq!(1.5, row.get_checked::<c_double>(3).unwrap());
assert!(row.get_checked::<Option<c_int>>(4).unwrap().is_none());
assert!(row.get_checked::<Option<c_double>>(4).unwrap().is_none());
assert!(row.get_checked::<Option<String>>(4).unwrap().is_none());
// check some invalid types
// 0 is actually a blob (Vec<u8>)
assert_eq!(row.get_checked::<c_int>(0).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<i64>(0).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<c_double>(0).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<String>(0).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<time::Timespec>(0).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Option<c_int>>(0).err().unwrap().code, ffi::SQLITE_MISMATCH);
// 1 is actually a text (String)
assert_eq!(row.get_checked::<c_int>(1).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<i64>(1).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<c_double>(1).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Vec<u8>>(1).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Option<c_int>>(1).err().unwrap().code, ffi::SQLITE_MISMATCH);
// 2 is actually an integer
assert_eq!(row.get_checked::<c_double>(2).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<String>(2).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Vec<u8>>(2).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<time::Timespec>(2).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Option<c_double>>(2).err().unwrap().code, ffi::SQLITE_MISMATCH);
// 3 is actually a float (c_double)
assert_eq!(row.get_checked::<c_int>(3).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<i64>(3).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<String>(3).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Vec<u8>>(3).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<time::Timespec>(3).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Option<c_int>>(3).err().unwrap().code, ffi::SQLITE_MISMATCH);
// 4 is actually NULL
assert_eq!(row.get_checked::<c_int>(4).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<i64>(4).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<c_double>(4).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<String>(4).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<Vec<u8>>(4).err().unwrap().code, ffi::SQLITE_MISMATCH);
assert_eq!(row.get_checked::<time::Timespec>(4).err().unwrap().code, ffi::SQLITE_MISMATCH);
}
} }