From 2f4990dafd18816fc99182f37768683471e32dcd Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Tue, 24 May 2016 21:34:18 -0400 Subject: [PATCH] Rename BorrowedValue -> ValueRef. --- src/functions.rs | 18 ++++----- src/lib.rs | 18 ++++----- src/types/chrono.rs | 12 +++--- src/types/from_sql.rs | 26 ++++++------ src/types/mod.rs | 8 ++-- src/types/serde_json.rs | 8 ++-- src/types/time.rs | 4 +- src/types/{borrowed_value.rs => value_ref.rs} | 40 +++++++++---------- 8 files changed, 67 insertions(+), 67 deletions(-) rename src/types/{borrowed_value.rs => value_ref.rs} (62%) diff --git a/src/functions.rs b/src/functions.rs index b261fce..32e4d1e 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -62,7 +62,7 @@ pub use ffi::sqlite3_value; pub use ffi::sqlite3_value_type; pub use ffi::sqlite3_value_numeric_type; -use types::{Null, FromSql, BorrowedValue}; +use types::{Null, FromSql, ValueRef}; use {Result, Error, Connection, str_to_cstring, InnerConnection}; @@ -156,14 +156,14 @@ impl ToResult for Null { } } -impl<'a> BorrowedValue<'a> { - unsafe fn from_value(value: *mut sqlite3_value) -> BorrowedValue<'a> { +impl<'a> ValueRef<'a> { + unsafe fn from_value(value: *mut sqlite3_value) -> ValueRef<'a> { use std::slice::from_raw_parts; match ffi::sqlite3_value_type(value) { - ffi::SQLITE_NULL => BorrowedValue::Null, - ffi::SQLITE_INTEGER => BorrowedValue::Integer(ffi::sqlite3_value_int64(value)), - ffi::SQLITE_FLOAT => BorrowedValue::Real(ffi::sqlite3_value_double(value)), + ffi::SQLITE_NULL => ValueRef::Null, + ffi::SQLITE_INTEGER => ValueRef::Integer(ffi::sqlite3_value_int64(value)), + ffi::SQLITE_FLOAT => ValueRef::Real(ffi::sqlite3_value_double(value)), ffi::SQLITE_TEXT => { let text = ffi::sqlite3_value_text(value); assert!(!text.is_null(), "unexpected SQLITE_TEXT value type with NULL data"); @@ -171,7 +171,7 @@ impl<'a> BorrowedValue<'a> { // sqlite3_value_text returns UTF8 data, so our unwrap here should be fine. let s = s.to_str().expect("sqlite3_value_text returned invalid UTF-8"); - BorrowedValue::Text(s) + ValueRef::Text(s) } ffi::SQLITE_BLOB => { let blob = ffi::sqlite3_value_blob(value); @@ -180,7 +180,7 @@ impl<'a> BorrowedValue<'a> { let len = ffi::sqlite3_value_bytes(value); assert!(len >= 0, "unexpected negative return from sqlite3_value_bytes"); - BorrowedValue::Blob(from_raw_parts(blob as *const u8, len as usize)) + ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) } _ => unreachable!("sqlite3_value_type returned invalid value") } @@ -216,7 +216,7 @@ impl<'a> Context<'a> { /// Will return Err if the underlying SQLite type cannot be converted to a `T`. pub fn get(&self, idx: usize) -> Result { let arg = self.args[idx]; - let value = unsafe { BorrowedValue::from_value(arg) }; + let value = unsafe { ValueRef::from_value(arg) }; FromSql::column_result(value).map_err(|err| match err { Error::InvalidColumnType => Error::InvalidFunctionParameterType, _ => err diff --git a/src/lib.rs b/src/lib.rs index 909ba1f..8d84890 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,7 @@ use std::result; use std::str; use libc::{c_int, c_char}; -use types::{ToSql, FromSql, BorrowedValue}; +use types::{ToSql, FromSql, ValueRef}; use error::{error_from_sqlite_code, error_from_handle}; use raw_statement::RawStatement; use cache::StatementCache; @@ -1065,7 +1065,7 @@ impl<'a, 'stmt> Row<'a, 'stmt> { /// for this row. pub fn get_checked(&self, idx: I) -> Result { let idx = try!(idx.idx(self.stmt)); - let value = unsafe { BorrowedValue::new(&self.stmt.stmt, idx) }; + let value = unsafe { ValueRef::new(&self.stmt.stmt, idx) }; FromSql::column_result(value) } @@ -1100,16 +1100,16 @@ impl<'a> RowIndex for &'a str { } } -impl<'a> BorrowedValue<'a> { - unsafe fn new(stmt: &RawStatement, col: c_int) -> BorrowedValue { +impl<'a> ValueRef<'a> { + unsafe fn new(stmt: &RawStatement, col: c_int) -> ValueRef { use std::slice::from_raw_parts; let raw = stmt.ptr(); match stmt.column_type(col) { - ffi::SQLITE_NULL => BorrowedValue::Null, - ffi::SQLITE_INTEGER => BorrowedValue::Integer(ffi::sqlite3_column_int64(raw, col)), - ffi::SQLITE_FLOAT => BorrowedValue::Real(ffi::sqlite3_column_double(raw, col)), + ffi::SQLITE_NULL => ValueRef::Null, + ffi::SQLITE_INTEGER => ValueRef::Integer(ffi::sqlite3_column_int64(raw, col)), + ffi::SQLITE_FLOAT => ValueRef::Real(ffi::sqlite3_column_double(raw, col)), ffi::SQLITE_TEXT => { let text = ffi::sqlite3_column_text(raw, col); assert!(!text.is_null(), "unexpected SQLITE_TEXT column type with NULL data"); @@ -1117,7 +1117,7 @@ impl<'a> BorrowedValue<'a> { // sqlite3_column_text returns UTF8 data, so our unwrap here should be fine. let s = s.to_str().expect("sqlite3_column_text returned invalid UTF-8"); - BorrowedValue::Text(s) + ValueRef::Text(s) } ffi::SQLITE_BLOB => { let blob = ffi::sqlite3_column_blob(raw, col); @@ -1126,7 +1126,7 @@ impl<'a> BorrowedValue<'a> { let len = ffi::sqlite3_column_bytes(raw, col); assert!(len >= 0, "unexpected negative return from sqlite3_column_bytes"); - BorrowedValue::Blob(from_raw_parts(blob as *const u8, len as usize)) + ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) } _ => unreachable!("sqlite3_column_type returned invalid value") } diff --git a/src/types/chrono.rs b/src/types/chrono.rs index 7ecb28f..3d17dd6 100644 --- a/src/types/chrono.rs +++ b/src/types/chrono.rs @@ -7,7 +7,7 @@ use self::chrono::{NaiveDate, NaiveTime, NaiveDateTime, DateTime, TimeZone, UTC, use libc::c_int; use {Error, Result}; -use types::{FromSql, ToSql, BorrowedValue}; +use types::{FromSql, ToSql, ValueRef}; use ffi::sqlite3_stmt; @@ -21,7 +21,7 @@ impl ToSql for NaiveDate { /// "YYYY-MM-DD" => ISO 8601 calendar date without timezone. impl FromSql for NaiveDate { - fn column_result(value: BorrowedValue) -> Result { + 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,7 +39,7 @@ 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: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { value.as_str().and_then(|s| { let fmt = match s.len() { 5 => "%H:%M", @@ -65,7 +65,7 @@ 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: BorrowedValue) -> Result { + 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" @@ -91,7 +91,7 @@ impl ToSql for DateTime { /// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime. impl FromSql for DateTime { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { { // Try to parse value as rfc3339 first. let s = try!(value.as_str()); @@ -121,7 +121,7 @@ impl FromSql for DateTime { /// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime. impl FromSql for DateTime { - fn column_result(value: BorrowedValue) -> Result { + 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 ba35b73..28df415 100644 --- a/src/types/from_sql.rs +++ b/src/types/from_sql.rs @@ -1,36 +1,36 @@ -use super::{BorrowedValue, Value}; +use super::{ValueRef, Value}; use ::Result; use ::error::Error; /// A trait for types that can be created from a SQLite value. pub trait FromSql: Sized { - fn column_result(value: BorrowedValue) -> Result; + fn column_result(value: ValueRef) -> Result; } impl FromSql for i32 { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { i64::column_result(value).map(|i| i as i32) } } impl FromSql for i64 { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { value.as_i64() } } impl FromSql for f64 { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { match value { - BorrowedValue::Integer(i) => Ok(i as f64), - BorrowedValue::Real(f) => Ok(f), + ValueRef::Integer(i) => Ok(i as f64), + ValueRef::Real(f) => Ok(f), _ => Err(Error::InvalidColumnType), } } } impl FromSql for bool { - fn column_result(value: BorrowedValue) -> Result { + 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: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { value.as_str().map(|s| s.to_string()) } } impl FromSql for Vec { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { value.as_blob().map(|b| b.to_vec()) } } impl FromSql for Option { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { match value { - BorrowedValue::Null => Ok(None), + ValueRef::Null => Ok(None), _ => FromSql::column_result(value).map(Some), } } } impl FromSql for Value { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { Ok(value.into()) } } diff --git a/src/types/mod.rs b/src/types/mod.rs index 03bb75a..7853291 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -29,7 +29,7 @@ //! pub struct TimespecSql(pub time::Timespec); //! //! impl FromSql for TimespecSql { -//! fn column_result(value: BorrowedValue) -> Result { +//! fn column_result(value: ValueRef) -> Result { //! f64::column_result(value).map(|as_f64| { //! TimespecSql(time::Timespec{ sec: as_f64.trunc() as i64, //! nsec: (as_f64.fract() * 1.0e9) as i32 }) @@ -56,9 +56,9 @@ pub use ffi::{SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NUL pub use self::from_sql::FromSql; pub use self::to_sql::ToSql; -pub use self::borrowed_value::BorrowedValue; +pub use self::value_ref::ValueRef; -mod borrowed_value; +mod value_ref; mod from_sql; mod to_sql; mod time; @@ -89,7 +89,7 @@ pub struct Null; /// Owning [dynamic type value](http://sqlite.org/datatype3.html). Value's type is typically /// dictated by SQLite (not by the caller). /// -/// See [`BorrowedValue`](enum.BorrowedValue.html) for a non-owning dynamic type value. +/// See [`ValueRef`](enum.ValueRef.html) for a non-owning dynamic type value. #[derive(Clone,Debug,PartialEq)] pub enum Value { /// The value is a `NULL` value. diff --git a/src/types/serde_json.rs b/src/types/serde_json.rs index a3da2fc..937f914 100644 --- a/src/types/serde_json.rs +++ b/src/types/serde_json.rs @@ -5,7 +5,7 @@ use libc::c_int; use self::serde_json::Value; use {Error, Result}; -use types::{FromSql, ToSql, BorrowedValue}; +use types::{FromSql, ToSql, ValueRef}; use ffi::sqlite3_stmt; @@ -19,10 +19,10 @@ impl ToSql for Value { /// Deserialize text/blob to JSON `Value`. impl FromSql for Value { - fn column_result(value: BorrowedValue) -> Result { + fn column_result(value: ValueRef) -> Result { match value { - BorrowedValue::Text(ref s) => serde_json::from_str(s), - BorrowedValue::Blob(ref b) => serde_json::from_slice(b), + ValueRef::Text(ref s) => serde_json::from_str(s), + ValueRef::Blob(ref b) => serde_json::from_slice(b), _ => return Err(Error::InvalidColumnType), }.map_err(|err| Error::FromSqlConversionFailure(Box::new(err))) } diff --git a/src/types/time.rs b/src/types/time.rs index ae524ec..1f1a818 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -2,7 +2,7 @@ extern crate time; use libc::c_int; use {Error, Result}; -use types::{FromSql, ToSql, BorrowedValue}; +use types::{FromSql, ToSql, ValueRef}; use ffi::sqlite3_stmt; @@ -16,7 +16,7 @@ impl ToSql for time::Timespec { } impl FromSql for time::Timespec { - fn column_result(value: BorrowedValue) -> Result { + 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/borrowed_value.rs b/src/types/value_ref.rs similarity index 62% rename from src/types/borrowed_value.rs rename to src/types/value_ref.rs index f8e5068..aa2d3b8 100644 --- a/src/types/borrowed_value.rs +++ b/src/types/value_ref.rs @@ -7,7 +7,7 @@ use super::Value; /// /// See [`Value`](enum.Value.html) for an owning dynamic type value. #[derive(Copy,Clone,Debug,PartialEq)] -pub enum BorrowedValue<'a> { +pub enum ValueRef<'a> { /// The value is a `NULL` value. Null, /// The value is a signed integer. @@ -20,12 +20,12 @@ pub enum BorrowedValue<'a> { Blob(&'a [u8]), } -impl<'a> BorrowedValue<'a> { +impl<'a> ValueRef<'a> { /// If `self` is case `Integer`, returns the integral value. Otherwise, returns /// `Err(Error::InvalidColumnType)`. pub fn as_i64(&self) -> Result { match *self { - BorrowedValue::Integer(i) => Ok(i), + ValueRef::Integer(i) => Ok(i), _ => Err(Error::InvalidColumnType), } } @@ -34,7 +34,7 @@ impl<'a> BorrowedValue<'a> { /// `Err(Error::InvalidColumnType)`. pub fn as_f64(&self) -> Result { match *self { - BorrowedValue::Real(f) => Ok(f), + ValueRef::Real(f) => Ok(f), _ => Err(Error::InvalidColumnType), } } @@ -43,7 +43,7 @@ impl<'a> BorrowedValue<'a> { /// `Err(Error::InvalidColumnType)`. pub fn as_str(&self) -> Result<&str> { match *self { - BorrowedValue::Text(ref t) => Ok(t), + ValueRef::Text(ref t) => Ok(t), _ => Err(Error::InvalidColumnType), } } @@ -52,32 +52,32 @@ impl<'a> BorrowedValue<'a> { /// `Err(Error::InvalidColumnType)`. pub fn as_blob(&self) -> Result<&[u8]> { match *self { - BorrowedValue::Blob(ref b) => Ok(b), + ValueRef::Blob(ref b) => Ok(b), _ => Err(Error::InvalidColumnType), } } } -impl<'a> From> for Value { - fn from(borrowed: BorrowedValue) -> Value { +impl<'a> From> for Value { + fn from(borrowed: ValueRef) -> Value { match borrowed { - BorrowedValue::Null => Value::Null, - BorrowedValue::Integer(i) => Value::Integer(i), - BorrowedValue::Real(r) => Value::Real(r), - BorrowedValue::Text(s) => Value::Text(s.to_string()), - BorrowedValue::Blob(b) => Value::Blob(b.to_vec()), + ValueRef::Null => Value::Null, + ValueRef::Integer(i) => Value::Integer(i), + ValueRef::Real(r) => Value::Real(r), + ValueRef::Text(s) => Value::Text(s.to_string()), + ValueRef::Blob(b) => Value::Blob(b.to_vec()), } } } -impl<'a> From<&'a Value> for BorrowedValue<'a> { - fn from(value: &'a Value) -> BorrowedValue<'a> { +impl<'a> From<&'a Value> for ValueRef<'a> { + fn from(value: &'a Value) -> ValueRef<'a> { match *value { - Value::Null => BorrowedValue::Null, - Value::Integer(i) => BorrowedValue::Integer(i), - Value::Real(r) => BorrowedValue::Real(r), - Value::Text(ref s) => BorrowedValue::Text(s), - Value::Blob(ref b) => BorrowedValue::Blob(b), + Value::Null => ValueRef::Null, + Value::Integer(i) => ValueRef::Integer(i), + Value::Real(r) => ValueRef::Real(r), + Value::Text(ref s) => ValueRef::Text(s), + Value::Blob(ref b) => ValueRef::Blob(b), } } }