Allow FromSql::<f64> to work on SQLite integer values.

This commit is contained in:
John Gallagher 2016-05-24 20:05:32 -04:00
parent 9d47d5109a
commit 4662b9b932
2 changed files with 7 additions and 3 deletions

View File

@ -1,5 +1,6 @@
use super::{BorrowedValue, Value}; use super::{BorrowedValue, Value};
use ::Result; use ::Result;
use ::error::Error;
/// 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: Sized { pub trait FromSql: Sized {
@ -20,7 +21,11 @@ impl FromSql for i64 {
impl FromSql for f64 { impl FromSql for f64 {
fn column_result(value: BorrowedValue) -> Result<Self> { fn column_result(value: BorrowedValue) -> Result<Self> {
value.as_f64() match value {
BorrowedValue::Integer(i) => Ok(i as f64),
BorrowedValue::Real(f) => Ok(f),
_ => Err(Error::InvalidColumnType),
}
} }
} }

View File

@ -223,10 +223,9 @@ mod test {
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_int>>(1).err().unwrap())); assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_int>>(1).err().unwrap()));
// 2 is actually an integer // 2 is actually an integer
assert!(is_invalid_column_type(row.get_checked::<i32, c_double>(2).err().unwrap()));
assert!(is_invalid_column_type(row.get_checked::<i32, String>(2).err().unwrap())); assert!(is_invalid_column_type(row.get_checked::<i32, String>(2).err().unwrap()));
assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(2).err().unwrap())); assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(2).err().unwrap()));
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_double>>(2).err().unwrap())); assert!(is_invalid_column_type(row.get_checked::<i32, Option<String>>(2).err().unwrap()));
// 3 is actually a float (c_double) // 3 is actually a float (c_double)
assert!(is_invalid_column_type(row.get_checked::<i32, c_int>(3).err().unwrap())); assert!(is_invalid_column_type(row.get_checked::<i32, c_int>(3).err().unwrap()));