mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-16 17:34:33 +00:00
decode: use HasRawValue
This commit is contained in:
@@ -13,8 +13,8 @@ use crate::types::TypeInfo;
|
||||
pub trait Database
|
||||
where
|
||||
Self: Sized + Send + 'static,
|
||||
Self: for<'a> HasRow<'a, Database = Self>,
|
||||
Self: for<'a> HasRawValue<'a>,
|
||||
Self: for<'c> HasRow<'c, Database = Self>,
|
||||
Self: for<'c> HasRawValue<'c>,
|
||||
Self: for<'c, 'q> HasCursor<'c, 'q, Database = Self>,
|
||||
{
|
||||
/// The concrete `Connection` implementation for this database.
|
||||
@@ -30,7 +30,7 @@ where
|
||||
type TableId: Display + Clone;
|
||||
}
|
||||
|
||||
pub trait HasRawValue<'a> {
|
||||
pub trait HasRawValue<'c> {
|
||||
type RawValue;
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ pub trait HasCursor<'c, 'q> {
|
||||
type Cursor: Cursor<'c, 'q, Database = Self::Database>;
|
||||
}
|
||||
|
||||
pub trait HasRow<'a> {
|
||||
pub trait HasRow<'c> {
|
||||
type Database: Database;
|
||||
|
||||
type Row: Row<'a, Database = Self::Database>;
|
||||
type Row: Row<'c, Database = Self::Database>;
|
||||
}
|
||||
|
||||
@@ -3,46 +3,15 @@
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
use crate::database::Database;
|
||||
use crate::types::Type;
|
||||
use crate::database::{Database, HasRawValue};
|
||||
|
||||
/// Decode a single value from the database.
|
||||
pub trait Decode<'de, DB>
|
||||
where
|
||||
Self: Sized,
|
||||
DB: Database,
|
||||
DB: HasRawValue<'de>,
|
||||
{
|
||||
fn decode(raw: &'de [u8]) -> crate::Result<Self>;
|
||||
|
||||
/// Creates a new value of this type from a `NULL` SQL value.
|
||||
///
|
||||
/// The default implementation returns [DecodeError::UnexpectedNull].
|
||||
fn decode_null() -> crate::Result<Self> {
|
||||
Err(crate::Error::Decode(UnexpectedNullError.into()))
|
||||
}
|
||||
|
||||
fn decode_nullable(raw: Option<&'de [u8]>) -> crate::Result<Self> {
|
||||
if let Some(raw) = raw {
|
||||
Self::decode(raw)
|
||||
} else {
|
||||
Self::decode_null()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T, DB> Decode<'de, DB> for Option<T>
|
||||
where
|
||||
DB: Database,
|
||||
T: Type<DB>,
|
||||
T: Decode<'de, DB>,
|
||||
{
|
||||
fn decode(buf: &'de [u8]) -> crate::Result<Self> {
|
||||
T::decode(buf).map(Some)
|
||||
}
|
||||
|
||||
fn decode_null() -> crate::Result<Self> {
|
||||
Ok(None)
|
||||
}
|
||||
fn decode(raw: DB::RawValue) -> crate::Result<Self>;
|
||||
}
|
||||
|
||||
/// An unexpected `NULL` was encountered during decoding.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Contains the Row and FromRow traits.
|
||||
|
||||
use crate::database::Database;
|
||||
use crate::database::{Database, HasRawValue};
|
||||
use crate::decode::Decode;
|
||||
use crate::types::Type;
|
||||
|
||||
@@ -38,10 +38,13 @@ pub trait Row<'c>: Unpin + Send {
|
||||
I: ColumnIndex<'c, Self>,
|
||||
T: Decode<'c, Self::Database>,
|
||||
{
|
||||
Ok(Decode::decode_nullable(self.try_get_raw(index)?)?)
|
||||
Ok(Decode::decode(self.try_get_raw(index)?)?)
|
||||
}
|
||||
|
||||
fn try_get_raw<'i, I>(&'c self, index: I) -> crate::Result<Option<&'c [u8]>>
|
||||
fn try_get_raw<'i, I>(
|
||||
&'c self,
|
||||
index: I,
|
||||
) -> crate::Result<<Self::Database as HasRawValue<'c>>::RawValue>
|
||||
where
|
||||
I: ColumnIndex<'c, Self> + 'i;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user