mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Add types::BorrowedValue.
This commit is contained in:
parent
efc6c89370
commit
c90cd37c00
51
src/types/borrowed_value.rs
Normal file
51
src/types/borrowed_value.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use ::Result;
|
||||
use ::error::Error;
|
||||
use super::Value;
|
||||
|
||||
pub enum BorrowedValue<'a> {
|
||||
Null,
|
||||
Integer(i64),
|
||||
Real(f64),
|
||||
Text(&'a str),
|
||||
Blob(&'a [u8]),
|
||||
}
|
||||
|
||||
impl<'a> BorrowedValue<'a> {
|
||||
pub fn to_value(&self) -> Value {
|
||||
match *self {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_i64(&self) -> Result<i64> {
|
||||
match *self {
|
||||
BorrowedValue::Integer(i) => Ok(i),
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_f64(&self) -> Result<f64> {
|
||||
match *self {
|
||||
BorrowedValue::Real(f) => Ok(f),
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> Result<&str> {
|
||||
match *self {
|
||||
BorrowedValue::Text(ref t) => Ok(t),
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_blob(&self) -> Result<&[u8]> {
|
||||
match *self {
|
||||
BorrowedValue::Blob(ref b) => Ok(b),
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
}
|
@ -58,7 +58,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;
|
||||
|
||||
mod borrowed_value;
|
||||
mod from_sql;
|
||||
mod to_sql;
|
||||
mod time;
|
||||
|
Loading…
Reference in New Issue
Block a user