mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 15:30:34 +08:00
Add as_type_or_null to ValueRef
This commit is contained in:
parent
e92e8a0924
commit
ca37ea2569
@ -45,6 +45,19 @@ impl<'a> ValueRef<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Null` returns None.
|
||||
/// If `self` is case `Integer`, returns the integral value.
|
||||
/// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
|
||||
/// InvalidColumnType).
|
||||
#[inline]
|
||||
pub fn as_i64_or_null(&self) -> FromSqlResult<Option<i64>> {
|
||||
match *self {
|
||||
ValueRef::Null => Ok(None),
|
||||
ValueRef::Integer(i) => Ok(Some(i)),
|
||||
_ => Err(FromSqlError::InvalidType),
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Real`, returns the floating point value. Otherwise,
|
||||
/// returns [`Err(Error::InvalidColumnType)`](crate::Error::
|
||||
/// InvalidColumnType).
|
||||
@ -56,6 +69,19 @@ impl<'a> ValueRef<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Null` returns None.
|
||||
/// If `self` is case `Real`, returns the floating point value.
|
||||
/// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
|
||||
/// InvalidColumnType).
|
||||
#[inline]
|
||||
pub fn as_f64_or_null(&self) -> FromSqlResult<Option<f64>> {
|
||||
match *self {
|
||||
ValueRef::Null => Ok(None),
|
||||
ValueRef::Real(f) => Ok(Some(f)),
|
||||
_ => Err(FromSqlError::InvalidType),
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Text`, returns the string value. Otherwise, returns
|
||||
/// [`Err(Error::InvalidColumnType)`](crate::Error::InvalidColumnType).
|
||||
#[inline]
|
||||
@ -68,6 +94,21 @@ impl<'a> ValueRef<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Null` returns None.
|
||||
/// If `self` is case `Text`, returns the string value.
|
||||
/// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
|
||||
/// InvalidColumnType).
|
||||
#[inline]
|
||||
pub fn as_str_or_null(&self) -> FromSqlResult<Option<&'a str>> {
|
||||
match *self {
|
||||
ValueRef::Null => Ok(None),
|
||||
ValueRef::Text(t) => std::str::from_utf8(t)
|
||||
.map_err(|e| FromSqlError::Other(Box::new(e)))
|
||||
.map(Some),
|
||||
_ => Err(FromSqlError::InvalidType),
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Blob`, returns the byte slice. Otherwise, returns
|
||||
/// [`Err(Error::InvalidColumnType)`](crate::Error::InvalidColumnType).
|
||||
#[inline]
|
||||
@ -78,6 +119,19 @@ impl<'a> ValueRef<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Null` returns None.
|
||||
/// If `self` is case `Blob`, returns the byte slice.
|
||||
/// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
|
||||
/// InvalidColumnType).
|
||||
#[inline]
|
||||
pub fn as_blob_or_null(&self) -> FromSqlResult<Option<&'a [u8]>> {
|
||||
match *self {
|
||||
ValueRef::Null => Ok(None),
|
||||
ValueRef::Blob(b) => Ok(Some(b)),
|
||||
_ => Err(FromSqlError::InvalidType),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the byte slice that makes up this ValueRef if it's either
|
||||
/// [`ValueRef::Blob`] or [`ValueRef::Text`].
|
||||
#[inline]
|
||||
@ -87,6 +141,18 @@ impl<'a> ValueRef<'a> {
|
||||
_ => Err(FromSqlError::InvalidType),
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is case `Null` returns None.
|
||||
/// If `self` is [`ValueRef::Blob`] or [`ValueRef::Text`] returns the byte
|
||||
/// slice that makes up this value
|
||||
#[inline]
|
||||
pub fn as_bytes_or_null(&self) -> FromSqlResult<Option<&'a [u8]>> {
|
||||
match *self {
|
||||
ValueRef::Null => Ok(None),
|
||||
ValueRef::Text(s) | ValueRef::Blob(s) => Ok(Some(s)),
|
||||
_ => Err(FromSqlError::InvalidType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ValueRef<'_>> for Value {
|
||||
|
Loading…
Reference in New Issue
Block a user