mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Rename BorrowedValue -> ValueRef.
This commit is contained in:
parent
308789495c
commit
2f4990dafd
@ -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<T: FromSql>(&self, idx: usize) -> Result<T> {
|
||||
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
|
||||
|
18
src/lib.rs
18
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<I: RowIndex, T: FromSql>(&self, idx: I) -> Result<T> {
|
||||
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")
|
||||
}
|
||||
|
@ -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<Self> {
|
||||
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,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<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
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<Self> {
|
||||
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"
|
||||
@ -91,7 +91,7 @@ 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: BorrowedValue) -> Result<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
{
|
||||
// Try to parse value as rfc3339 first.
|
||||
let s = try!(value.as_str());
|
||||
@ -121,7 +121,7 @@ impl FromSql for DateTime<UTC> {
|
||||
|
||||
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime<Local>.
|
||||
impl FromSql for DateTime<Local> {
|
||||
fn column_result(value: BorrowedValue) -> Result<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
let utc_dt = try!(DateTime::<UTC>::column_result(value));
|
||||
Ok(utc_dt.with_timezone(&Local))
|
||||
}
|
||||
|
@ -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<Self>;
|
||||
fn column_result(value: ValueRef) -> Result<Self>;
|
||||
}
|
||||
|
||||
impl FromSql for i32 {
|
||||
fn column_result(value: BorrowedValue) -> Result<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
i64::column_result(value).map(|i| i as i32)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for i64 {
|
||||
fn column_result(value: BorrowedValue) -> Result<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
value.as_i64()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for f64 {
|
||||
fn column_result(value: BorrowedValue) -> Result<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
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<Self> {
|
||||
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: BorrowedValue) -> Result<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
value.as_str().map(|s| s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for Vec<u8> {
|
||||
fn column_result(value: BorrowedValue) -> Result<Self> {
|
||||
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: BorrowedValue) -> Result<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
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<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
Ok(value.into())
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@
|
||||
//! pub struct TimespecSql(pub time::Timespec);
|
||||
//!
|
||||
//! impl FromSql for TimespecSql {
|
||||
//! fn column_result(value: BorrowedValue) -> Result<Self> {
|
||||
//! fn column_result(value: ValueRef) -> Result<Self> {
|
||||
//! 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.
|
||||
|
@ -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<Self> {
|
||||
fn column_result(value: ValueRef) -> Result<Self> {
|
||||
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)))
|
||||
}
|
||||
|
@ -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<Self> {
|
||||
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))),
|
||||
|
@ -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<i64> {
|
||||
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<f64> {
|
||||
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<BorrowedValue<'a>> for Value {
|
||||
fn from(borrowed: BorrowedValue) -> Value {
|
||||
impl<'a> From<ValueRef<'a>> 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),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user