Introduce an intermediary InvalidType error.

InvalidType is used where the column/parameter index is not known.
This commit is contained in:
gwenn
2016-05-30 20:35:56 +02:00
parent cabcaf3dcb
commit fb19e718cf
8 changed files with 55 additions and 43 deletions

View File

@@ -21,8 +21,8 @@ impl ToSql for NaiveDate {
/// "YYYY-MM-DD" => ISO 8601 calendar date without timezone.
impl FromSql for NaiveDate {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
value.as_str(idx).and_then(|s| match NaiveDate::parse_from_str(s, "%Y-%m-%d") {
fn column_result(value: ValueRef) -> Result<Self> {
value.as_str().and_then(|s| match NaiveDate::parse_from_str(s, "%Y-%m-%d") {
Ok(dt) => Ok(dt),
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
})
@@ -39,8 +39,8 @@ impl ToSql for NaiveTime {
/// "HH:MM"/"HH:MM:SS"/"HH:MM:SS.SSS" => ISO 8601 time without timezone.
impl FromSql for NaiveTime {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
value.as_str(idx).and_then(|s| {
fn column_result(value: ValueRef) -> Result<Self> {
value.as_str().and_then(|s| {
let fmt = match s.len() {
5 => "%H:%M",
8 => "%H:%M:%S",
@@ -65,8 +65,8 @@ impl ToSql for NaiveDateTime {
/// "YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS" => ISO 8601 combined date and time
/// without timezone. ("YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported)
impl FromSql for NaiveDateTime {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
value.as_str(idx).and_then(|s| {
fn column_result(value: ValueRef) -> Result<Self> {
value.as_str().and_then(|s| {
let fmt = if s.len() >= 11 && s.as_bytes()[10] == b'T' {
"%Y-%m-%dT%H:%M:%S%.f"
} else {
@@ -91,10 +91,10 @@ impl<Tz: TimeZone> ToSql for DateTime<Tz> {
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime<UTC>.
impl FromSql for DateTime<UTC> {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
fn column_result(value: ValueRef) -> Result<Self> {
{
// Try to parse value as rfc3339 first.
let s = try!(value.as_str(idx));
let s = try!(value.as_str());
// If timestamp looks space-separated, make a copy and replace it with 'T'.
let s = if s.len() >= 11 && s.as_bytes()[10] == b' ' {
@@ -115,14 +115,14 @@ impl FromSql for DateTime<UTC> {
}
// Couldn't parse as rfc3339 - fall back to NaiveDateTime.
NaiveDateTime::column_result(value, idx).map(|dt| UTC.from_utc_datetime(&dt))
NaiveDateTime::column_result(value).map(|dt| UTC.from_utc_datetime(&dt))
}
}
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime<Local>.
impl FromSql for DateTime<Local> {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
let utc_dt = try!(DateTime::<UTC>::column_result(value, idx));
fn column_result(value: ValueRef) -> Result<Self> {
let utc_dt = try!(DateTime::<UTC>::column_result(value));
Ok(utc_dt.with_timezone(&Local))
}
}

View File

@@ -4,34 +4,34 @@ use ::error::Error;
/// A trait for types that can be created from a SQLite value.
pub trait FromSql: Sized {
fn column_result(value: ValueRef, idx: i32) -> Result<Self>;
fn column_result(value: ValueRef) -> Result<Self>;
}
impl FromSql for i32 {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
i64::column_result(value, idx).map(|i| i as i32)
fn column_result(value: ValueRef) -> Result<Self> {
i64::column_result(value).map(|i| i as i32)
}
}
impl FromSql for i64 {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
value.as_i64(idx)
fn column_result(value: ValueRef) -> Result<Self> {
value.as_i64()
}
}
impl FromSql for f64 {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
fn column_result(value: ValueRef) -> Result<Self> {
match value {
ValueRef::Integer(i) => Ok(i as f64),
ValueRef::Real(f) => Ok(f),
_ => Err(Error::InvalidColumnType(idx, value.data_type())),
_ => Err(Error::InvalidType(value.data_type())),
}
}
}
impl FromSql for bool {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
i64::column_result(value, idx).map(|i| match i {
fn column_result(value: ValueRef) -> Result<Self> {
i64::column_result(value).map(|i| match i {
0 => false,
_ => true,
})
@@ -39,28 +39,28 @@ impl FromSql for bool {
}
impl FromSql for String {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
value.as_str(idx).map(|s| s.to_string())
fn column_result(value: ValueRef) -> Result<Self> {
value.as_str().map(|s| s.to_string())
}
}
impl FromSql for Vec<u8> {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
value.as_blob(idx).map(|b| b.to_vec())
fn column_result(value: ValueRef) -> Result<Self> {
value.as_blob().map(|b| b.to_vec())
}
}
impl<T: FromSql> FromSql for Option<T> {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
fn column_result(value: ValueRef) -> Result<Self> {
match value {
ValueRef::Null => Ok(None),
_ => FromSql::column_result(value, idx).map(Some),
_ => FromSql::column_result(value).map(Some),
}
}
}
impl FromSql for Value {
fn column_result(value: ValueRef, _: i32) -> Result<Self> {
fn column_result(value: ValueRef) -> Result<Self> {
Ok(value.into())
}
}

View File

@@ -19,11 +19,11 @@ impl ToSql for Value {
/// Deserialize text/blob to JSON `Value`.
impl FromSql for Value {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
fn column_result(value: ValueRef) -> Result<Self> {
match value {
ValueRef::Text(ref s) => serde_json::from_str(s),
ValueRef::Blob(ref b) => serde_json::from_slice(b),
_ => return Err(Error::InvalidColumnType(idx, value.data_type())),
_ => return Err(Error::InvalidType(value.data_type())),
}
.map_err(|err| Error::FromSqlConversionFailure(Box::new(err)))
}

View File

@@ -16,8 +16,8 @@ impl ToSql for time::Timespec {
}
impl FromSql for time::Timespec {
fn column_result(value: ValueRef, idx: i32) -> Result<Self> {
value.as_str(idx).and_then(|s| match time::strptime(s, SQLITE_DATETIME_FMT) {
fn column_result(value: ValueRef) -> Result<Self> {
value.as_str().and_then(|s| match time::strptime(s, SQLITE_DATETIME_FMT) {
Ok(tm) => Ok(tm.to_timespec()),
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
})

View File

@@ -35,37 +35,37 @@ impl<'a> ValueRef<'a> {
impl<'a> ValueRef<'a> {
/// If `self` is case `Integer`, returns the integral value. Otherwise, returns
/// `Err(Error::InvalidColumnType)`.
pub fn as_i64(&self, idx: i32) -> Result<i64> {
pub fn as_i64(&self) -> Result<i64> {
match *self {
ValueRef::Integer(i) => Ok(i),
_ => Err(Error::InvalidColumnType(idx, self.data_type())),
_ => Err(Error::InvalidType(self.data_type())),
}
}
/// If `self` is case `Real`, returns the floating point value. Otherwise, returns
/// `Err(Error::InvalidColumnType)`.
pub fn as_f64(&self, idx: i32) -> Result<f64> {
pub fn as_f64(&self) -> Result<f64> {
match *self {
ValueRef::Real(f) => Ok(f),
_ => Err(Error::InvalidColumnType(idx, self.data_type())),
_ => Err(Error::InvalidType(self.data_type())),
}
}
/// If `self` is case `Text`, returns the string value. Otherwise, returns
/// `Err(Error::InvalidColumnType)`.
pub fn as_str(&self, idx: i32) -> Result<&str> {
pub fn as_str(&self) -> Result<&str> {
match *self {
ValueRef::Text(ref t) => Ok(t),
_ => Err(Error::InvalidColumnType(idx, self.data_type())),
_ => Err(Error::InvalidType(self.data_type())),
}
}
/// If `self` is case `Blob`, returns the byte slice. Otherwise, returns
/// `Err(Error::InvalidColumnType)`.
pub fn as_blob(&self, idx: i32) -> Result<&[u8]> {
pub fn as_blob(&self) -> Result<&[u8]> {
match *self {
ValueRef::Blob(ref b) => Ok(b),
_ => Err(Error::InvalidColumnType(idx, self.data_type())),
_ => Err(Error::InvalidType(self.data_type())),
}
}
}