From fb19e718cfb7742e1791a1073ee39c2621577263 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 30 May 2016 20:35:56 +0200 Subject: [PATCH] Introduce an intermediary InvalidType error. InvalidType is used where the column/parameter index is not known. --- src/error.rs | 11 ++++++++++- src/functions.rs | 4 ++-- src/lib.rs | 5 ++++- src/types/chrono.rs | 22 +++++++++++----------- src/types/from_sql.rs | 32 ++++++++++++++++---------------- src/types/serde_json.rs | 4 ++-- src/types/time.rs | 4 ++-- src/types/value_ref.rs | 16 ++++++++-------- 8 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/error.rs b/src/error.rs index 03624d2..f480a42 100644 --- a/src/error.rs +++ b/src/error.rs @@ -53,6 +53,10 @@ pub enum Error { /// that column cannot be converted to the requested Rust type. InvalidColumnType(i32, Type), + /// Error when an SQLite value is requested, but the type of the result cannot be converted to the + /// requested Rust type. + InvalidType(Type), + /// Error when a query that was expected to insert one row did not insert any or insert many. StatementChangedRows(c_int), @@ -63,7 +67,7 @@ pub enum Error { /// Error returned by `functions::Context::get` when the function argument cannot be converted /// to the requested type. #[cfg(feature = "functions")] - InvalidFunctionParameterType(i32, Type), + InvalidFunctionParameterType(usize, Type), /// An error case available for implementors of custom user functions (e.g., /// `create_scalar_function`). @@ -107,6 +111,9 @@ impl fmt::Display for Error { Error::InvalidColumnType(i, ref t) => { write!(f, "Invalid column type {} at index: {}", t, i) } + Error::InvalidType(ref t) => { + write!(f, "Invalid type {}", t) + } Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i), Error::StatementFailedToInsertRow => write!(f, "Statement failed to insert new row"), @@ -140,6 +147,7 @@ impl error::Error for Error { Error::InvalidColumnIndex(_) => "invalid column index", Error::InvalidColumnName(_) => "invalid column name", Error::InvalidColumnType(_, _) => "invalid column type", + Error::InvalidType(_) => "invalid type", Error::StatementChangedRows(_) => "query inserted zero or more than one row", Error::StatementFailedToInsertRow => "statement failed to insert new row", @@ -165,6 +173,7 @@ impl error::Error for Error { Error::InvalidColumnIndex(_) | Error::InvalidColumnName(_) | Error::InvalidColumnType(_, _) | + Error::InvalidType(_) | Error::InvalidPath(_) | Error::StatementChangedRows(_) | Error::StatementFailedToInsertRow => None, diff --git a/src/functions.rs b/src/functions.rs index 42fbd78..8065e1a 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -217,8 +217,8 @@ impl<'a> Context<'a> { pub fn get(&self, idx: usize) -> Result { let arg = self.args[idx]; let value = unsafe { ValueRef::from_value(arg) }; - FromSql::column_result(value, idx as i32).map_err(|err| match err { - Error::InvalidColumnType(i, t) => Error::InvalidFunctionParameterType(i, t), + FromSql::column_result(value).map_err(|err| match err { + Error::InvalidType(t) => Error::InvalidFunctionParameterType(idx, t), _ => err, }) } diff --git a/src/lib.rs b/src/lib.rs index 0114285..72e9a1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1066,7 +1066,10 @@ impl<'a, 'stmt> Row<'a, 'stmt> { pub fn get_checked(&self, idx: I) -> Result { let idx = try!(idx.idx(self.stmt)); let value = unsafe { ValueRef::new(&self.stmt.stmt, idx) }; - FromSql::column_result(value, idx) + FromSql::column_result(value).map_err(|err| match err { + Error::InvalidType(t) => Error::InvalidColumnType(idx, t), + _ => err, + }) } /// Return the number of columns in the current row. diff --git a/src/types/chrono.rs b/src/types/chrono.rs index f415a7b..3d17dd6 100644 --- a/src/types/chrono.rs +++ b/src/types/chrono.rs @@ -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 { - value.as_str(idx).and_then(|s| match NaiveDate::parse_from_str(s, "%Y-%m-%d") { + fn column_result(value: ValueRef) -> Result { + 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 { - value.as_str(idx).and_then(|s| { + fn column_result(value: ValueRef) -> Result { + 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 { - value.as_str(idx).and_then(|s| { + fn column_result(value: ValueRef) -> Result { + 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 ToSql for DateTime { /// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime. impl FromSql for DateTime { - fn column_result(value: ValueRef, idx: i32) -> Result { + fn column_result(value: ValueRef) -> Result { { // 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 { } // 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. impl FromSql for DateTime { - fn column_result(value: ValueRef, idx: i32) -> Result { - let utc_dt = try!(DateTime::::column_result(value, idx)); + fn column_result(value: ValueRef) -> Result { + let utc_dt = try!(DateTime::::column_result(value)); Ok(utc_dt.with_timezone(&Local)) } } diff --git a/src/types/from_sql.rs b/src/types/from_sql.rs index 09471e2..1eba548 100644 --- a/src/types/from_sql.rs +++ b/src/types/from_sql.rs @@ -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; + fn column_result(value: ValueRef) -> Result; } impl FromSql for i32 { - fn column_result(value: ValueRef, idx: i32) -> Result { - i64::column_result(value, idx).map(|i| i as i32) + fn column_result(value: ValueRef) -> Result { + i64::column_result(value).map(|i| i as i32) } } impl FromSql for i64 { - fn column_result(value: ValueRef, idx: i32) -> Result { - value.as_i64(idx) + fn column_result(value: ValueRef) -> Result { + value.as_i64() } } impl FromSql for f64 { - fn column_result(value: ValueRef, idx: i32) -> Result { + fn column_result(value: ValueRef) -> Result { 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 { - i64::column_result(value, idx).map(|i| match i { + fn column_result(value: ValueRef) -> Result { + 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 { - value.as_str(idx).map(|s| s.to_string()) + fn column_result(value: ValueRef) -> Result { + value.as_str().map(|s| s.to_string()) } } impl FromSql for Vec { - fn column_result(value: ValueRef, idx: i32) -> Result { - value.as_blob(idx).map(|b| b.to_vec()) + fn column_result(value: ValueRef) -> Result { + value.as_blob().map(|b| b.to_vec()) } } impl FromSql for Option { - fn column_result(value: ValueRef, idx: i32) -> Result { + fn column_result(value: ValueRef) -> Result { 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 { + fn column_result(value: ValueRef) -> Result { Ok(value.into()) } } diff --git a/src/types/serde_json.rs b/src/types/serde_json.rs index 4c01f20..06d8855 100644 --- a/src/types/serde_json.rs +++ b/src/types/serde_json.rs @@ -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 { + fn column_result(value: ValueRef) -> Result { 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))) } diff --git a/src/types/time.rs b/src/types/time.rs index 05dc1b1..1f1a818 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -16,8 +16,8 @@ impl ToSql for time::Timespec { } impl FromSql for time::Timespec { - fn column_result(value: ValueRef, idx: i32) -> Result { - value.as_str(idx).and_then(|s| match time::strptime(s, SQLITE_DATETIME_FMT) { + fn column_result(value: ValueRef) -> Result { + 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))), }) diff --git a/src/types/value_ref.rs b/src/types/value_ref.rs index 01ae8e0..ad5b058 100644 --- a/src/types/value_ref.rs +++ b/src/types/value_ref.rs @@ -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 { + pub fn as_i64(&self) -> Result { 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 { + pub fn as_f64(&self) -> Result { 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())), } } }