Trait sqlx_core::decode::Decode [−][src]
pub trait Decode<'r, DB: Database>: Sized {
fn decode(
value: <DB as HasValueRef<'r>>::ValueRef
) -> Result<Self, BoxDynError>;
}
Expand description
A type that can be decoded from the database.
How can I implement Decode
?
A manual implementation of Decode
can be useful when adding support for
types externally to SQLx.
The following showcases how to implement Decode
to be generic over Database
. The
implementation can be marginally simpler if you remove the DB
type parameter and explicitly
use the concrete ValueRef
and TypeInfo
types.
struct MyType;
// DB is the database driver
// `'r` is the lifetime of the `Row` being decoded
impl<'r, DB: Database> Decode<'r, DB> for MyType
where
// we want to delegate some of the work to string decoding so let's make sure strings
// are supported by the database
&'r str: Decode<'r, DB>
{
fn decode(
value: <DB as HasValueRef<'r>>::ValueRef,
) -> Result<MyType, Box<dyn Error + 'static + Send + Sync>> {
// the interface of ValueRef is largely unstable at the moment
// so this is not directly implementable
// however, you can delegate to a type that matches the format of the type you want
// to decode (such as a UTF-8 string)
let value = <&str as Decode<DB>>::decode(value)?;
// now you can parse this into your type (assuming there is a `FromStr`)
Ok(value.parse()?)
}
}
Required methods
fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError>
fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError>
Decode a new value of this type using a raw value from the database.
Implementations on Foreign Types
impl<'r, T1, T2, T3, T4> Decode<'r, Postgres> for (T1, T2, T3, T4) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4> Decode<'r, Postgres> for (T1, T2, T3, T4) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5> Decode<'r, Postgres> for (T1, T2, T3, T4, T5) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5> Decode<'r, Postgres> for (T1, T2, T3, T4, T5) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T7: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T7: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
T7: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T7: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T7: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
T7: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7, T8> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T7: 'r,
T8: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T7: Type<Postgres>,
T8: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
T7: for<'a> Decode<'a, Postgres>,
T8: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7, T8> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T7: 'r,
T8: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T7: Type<Postgres>,
T8: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
T7: for<'a> Decode<'a, Postgres>,
T8: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7, T8, T9> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T7: 'r,
T8: 'r,
T9: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T7: Type<Postgres>,
T8: Type<Postgres>,
T9: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
T7: for<'a> Decode<'a, Postgres>,
T8: for<'a> Decode<'a, Postgres>,
T9: for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7, T8, T9> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9) where
T1: 'r,
T2: 'r,
T3: 'r,
T4: 'r,
T5: 'r,
T6: 'r,
T7: 'r,
T8: 'r,
T9: 'r,
T1: Type<Postgres>,
T2: Type<Postgres>,
T3: Type<Postgres>,
T4: Type<Postgres>,
T5: Type<Postgres>,
T6: Type<Postgres>,
T7: Type<Postgres>,
T8: Type<Postgres>,
T9: Type<Postgres>,
T1: for<'a> Decode<'a, Postgres>,
T2: for<'a> Decode<'a, Postgres>,
T3: for<'a> Decode<'a, Postgres>,
T4: for<'a> Decode<'a, Postgres>,
T5: for<'a> Decode<'a, Postgres>,
T6: for<'a> Decode<'a, Postgres>,
T7: for<'a> Decode<'a, Postgres>,
T8: for<'a> Decode<'a, Postgres>,
T9: for<'a> Decode<'a, Postgres>,
Note: assumes the connection’s time_zone
is set to +00:00
(UTC).
Note: assumes the connection’s time_zone
is set to +00:00
(UTC).