mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 19:41:37 +08:00
Add ToSql/FromSql for [u8; N]
This commit is contained in:
parent
314977de4d
commit
7f09d295ba
@ -144,6 +144,9 @@ impl Context<'_> {
|
|||||||
FromSqlError::Other(err) => {
|
FromSqlError::Other(err) => {
|
||||||
Error::FromSqlConversionFailure(idx, value.data_type(), err)
|
Error::FromSqlConversionFailure(idx, value.data_type(), err)
|
||||||
}
|
}
|
||||||
|
FromSqlError::InvalidSize(_, _) => {
|
||||||
|
Error::FromSqlConversionFailure(idx, value.data_type(), Box::new(err))
|
||||||
|
}
|
||||||
#[cfg(feature = "i128_blob")]
|
#[cfg(feature = "i128_blob")]
|
||||||
FromSqlError::InvalidI128Size(_) => {
|
FromSqlError::InvalidI128Size(_) => {
|
||||||
Error::FromSqlConversionFailure(idx, value.data_type(), Box::new(err))
|
Error::FromSqlConversionFailure(idx, value.data_type(), Box::new(err))
|
||||||
|
@ -290,6 +290,9 @@ impl<'stmt> Row<'stmt> {
|
|||||||
FromSqlError::Other(err) => {
|
FromSqlError::Other(err) => {
|
||||||
Error::FromSqlConversionFailure(idx as usize, value.data_type(), err)
|
Error::FromSqlConversionFailure(idx as usize, value.data_type(), err)
|
||||||
}
|
}
|
||||||
|
FromSqlError::InvalidSize(_, _) => {
|
||||||
|
Error::FromSqlConversionFailure(idx as usize, value.data_type(), Box::new(err))
|
||||||
|
}
|
||||||
#[cfg(feature = "i128_blob")]
|
#[cfg(feature = "i128_blob")]
|
||||||
FromSqlError::InvalidI128Size(_) => Error::InvalidColumnType(
|
FromSqlError::InvalidI128Size(_) => Error::InvalidColumnType(
|
||||||
idx,
|
idx,
|
||||||
|
@ -15,6 +15,10 @@ pub enum FromSqlError {
|
|||||||
/// requested type.
|
/// requested type.
|
||||||
OutOfRange(i64),
|
OutOfRange(i64),
|
||||||
|
|
||||||
|
/// Error when the blob result returned by SQLite cannot be stored into the
|
||||||
|
/// requested type due to a size mismatch. (type size, blob size)
|
||||||
|
InvalidSize(usize, usize),
|
||||||
|
|
||||||
/// Error returned when reading an `i128` from a
|
/// Error returned when reading an `i128` from a
|
||||||
/// blob with a size other than 16. Only available when the `i128_blob`
|
/// blob with a size other than 16. Only available when the `i128_blob`
|
||||||
/// feature is enabled.
|
/// feature is enabled.
|
||||||
@ -51,6 +55,9 @@ impl fmt::Display for FromSqlError {
|
|||||||
match *self {
|
match *self {
|
||||||
FromSqlError::InvalidType => write!(f, "Invalid type"),
|
FromSqlError::InvalidType => write!(f, "Invalid type"),
|
||||||
FromSqlError::OutOfRange(i) => write!(f, "Value {} out of range", i),
|
FromSqlError::OutOfRange(i) => write!(f, "Value {} out of range", i),
|
||||||
|
FromSqlError::InvalidSize(i, j) => {
|
||||||
|
write!(f, "Cannot read {} byte value out of {} byte blob", i, j)
|
||||||
|
}
|
||||||
#[cfg(feature = "i128_blob")]
|
#[cfg(feature = "i128_blob")]
|
||||||
FromSqlError::InvalidI128Size(s) => {
|
FromSqlError::InvalidI128Size(s) => {
|
||||||
write!(f, "Cannot read 128bit value out of {} byte blob", s)
|
write!(f, "Cannot read 128bit value out of {} byte blob", s)
|
||||||
@ -177,6 +184,16 @@ impl FromSql for Vec<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> FromSql for [u8; N] {
|
||||||
|
#[inline]
|
||||||
|
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||||
|
let slice = value.as_blob()?;
|
||||||
|
slice
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| FromSqlError::InvalidSize(N, slice.len()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "i128_blob")]
|
#[cfg(feature = "i128_blob")]
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
|
||||||
impl FromSql for i128 {
|
impl FromSql for i128 {
|
||||||
|
@ -224,6 +224,13 @@ impl ToSql for Vec<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> ToSql for [u8; N] {
|
||||||
|
#[inline]
|
||||||
|
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||||
|
Ok(ToSqlOutput::from(&self[..]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ToSql for [u8] {
|
impl ToSql for [u8] {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||||
@ -265,6 +272,15 @@ mod test {
|
|||||||
is_to_sql::<u32>();
|
is_to_sql::<u32>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_u8_array() {
|
||||||
|
let a: [u8; 99] = [0u8; 99];
|
||||||
|
let _a: &[&dyn ToSql] = crate::params![a];
|
||||||
|
let r = ToSql::to_sql(&a);
|
||||||
|
|
||||||
|
assert!(r.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cow_str() {
|
fn test_cow_str() {
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
@ -600,6 +600,9 @@ impl Values<'_> {
|
|||||||
FromSqlError::Other(err) => {
|
FromSqlError::Other(err) => {
|
||||||
Error::FromSqlConversionFailure(idx, value.data_type(), err)
|
Error::FromSqlConversionFailure(idx, value.data_type(), err)
|
||||||
}
|
}
|
||||||
|
FromSqlError::InvalidSize(_, _) => {
|
||||||
|
Error::FromSqlConversionFailure(idx, value.data_type(), Box::new(err))
|
||||||
|
}
|
||||||
FromSqlError::OutOfRange(i) => Error::IntegralValueOutOfRange(idx, i),
|
FromSqlError::OutOfRange(i) => Error::IntegralValueOutOfRange(idx, i),
|
||||||
#[cfg(feature = "i128_blob")]
|
#[cfg(feature = "i128_blob")]
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
|
||||||
|
Loading…
Reference in New Issue
Block a user