[][src]Trait sqlx_core::decode::Decode

pub trait Decode<'r, DB: Database>: Sized {
    pub fn decode(
        value: <DB as HasValueRef<'r>>::ValueRef
    ) -> Result<Self, BoxDynError>; }

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

pub fn decode(
    value: <DB as HasValueRef<'r>>::ValueRef
) -> Result<Self, BoxDynError>
[src]

Decode a new value of this type using a raw value from the database.

Loading content...

Implementations on Foreign Types

impl<'r, DB, T> Decode<'r, DB> for Option<T> where
    DB: Database,
    T: Decode<'r, DB>, 
[src]

impl<'r, DB> Decode<'r, DB> for JsonValue where
    Self: Type<DB>,
    Json<Self>: Decode<'r, DB>,
    DB: Database
[src]

impl<'r, DB> Decode<'r, DB> for &'r JsonRawValue where
    Self: Type<DB>,
    Json<Self>: Decode<'r, DB>,
    DB: Database
[src]

impl<'r> Decode<'r, Any> for bool where
    bool: AnyDecode<'r>, 
[src]

impl<'r> Decode<'r, Any> for i32 where
    i32: AnyDecode<'r>, 
[src]

impl<'r> Decode<'r, Any> for i64 where
    i64: AnyDecode<'r>, 
[src]

impl<'r> Decode<'r, Any> for f32 where
    f32: AnyDecode<'r>, 
[src]

impl<'r> Decode<'r, Any> for f64 where
    f64: AnyDecode<'r>, 
[src]

impl<'r> Decode<'r, Any> for &'r str where
    &'r str: AnyDecode<'r>, 
[src]

impl<'r> Decode<'r, Any> for String where
    String: AnyDecode<'r>, 
[src]

impl<'r, T> Decode<'r, Postgres> for Vec<T> where
    T: for<'a> Decode<'a, Postgres> + Type<Postgres>,
    Self: Type<Postgres>, 
[src]

impl<'_> Decode<'_, Postgres> for bool[src]

impl<'r> Decode<'r, Postgres> for &'r [u8][src]

impl<'_> Decode<'_, Postgres> for Vec<u8>[src]

impl<'_> Decode<'_, Postgres> for f32[src]

impl<'_> Decode<'_, Postgres> for f64[src]

impl<'_> Decode<'_, Postgres> for i8[src]

impl<'_> Decode<'_, Postgres> for i16[src]

impl<'_> Decode<'_, Postgres> for u32[src]

impl<'_> Decode<'_, Postgres> for i32[src]

impl<'_> Decode<'_, Postgres> for i64[src]

impl<'r> Decode<'r, Postgres> for &'r str[src]

impl<'_> Decode<'_, Postgres> for String[src]

impl<'r, T1> Decode<'r, Postgres> for (T1,) where
    T1: 'r,
    T1: Type<Postgres>,
    T1: for<'a> Decode<'a, Postgres>, 
[src]

impl<'r, T1, T2> Decode<'r, Postgres> for (T1, T2) where
    T1: 'r,
    T2: 'r,
    T1: Type<Postgres>,
    T2: Type<Postgres>,
    T1: for<'a> Decode<'a, Postgres>,
    T2: for<'a> Decode<'a, Postgres>, 
[src]

impl<'r, T1, T2, T3> Decode<'r, Postgres> for (T1, T2, T3) where
    T1: 'r,
    T2: 'r,
    T3: 'r,
    T1: Type<Postgres>,
    T2: Type<Postgres>,
    T3: Type<Postgres>,
    T1: for<'a> Decode<'a, Postgres>,
    T2: for<'a> Decode<'a, Postgres>,
    T3: for<'a> Decode<'a, Postgres>, 
[src]

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>, 
[src]

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>, 
[src]

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>, 
[src]

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>, 
[src]

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>, 
[src]

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>, 
[src]

impl<'r> Decode<'r, Postgres> for ()[src]

impl<'r> Decode<'r, Sqlite> for bool[src]

impl<'r> Decode<'r, Sqlite> for &'r [u8][src]

impl<'r> Decode<'r, Sqlite> for Vec<u8>[src]

impl<'r> Decode<'r, Sqlite> for f32[src]

impl<'r> Decode<'r, Sqlite> for f64[src]

impl<'r> Decode<'r, Sqlite> for i8[src]

impl<'r> Decode<'r, Sqlite> for i16[src]

impl<'r> Decode<'r, Sqlite> for i32[src]

impl<'r> Decode<'r, Sqlite> for i64[src]

impl<'r> Decode<'r, Sqlite> for &'r str[src]

impl<'r> Decode<'r, Sqlite> for String[src]

impl<'_> Decode<'_, Sqlite> for Hyphenated[src]

impl<'_> Decode<'_, MySql> for bool[src]

impl<'r> Decode<'r, MySql> for &'r [u8][src]

impl<'_> Decode<'_, MySql> for Vec<u8>[src]

impl<'_> Decode<'_, MySql> for f32[src]

impl<'_> Decode<'_, MySql> for f64[src]

impl<'_> Decode<'_, MySql> for i8[src]

impl<'_> Decode<'_, MySql> for i16[src]

impl<'_> Decode<'_, MySql> for i32[src]

impl<'_> Decode<'_, MySql> for i64[src]

impl<'r> Decode<'r, MySql> for &'r str[src]

impl<'_> Decode<'_, MySql> for String[src]

impl<'_> Decode<'_, MySql> for u8[src]

impl<'_> Decode<'_, MySql> for u16[src]

impl<'_> Decode<'_, MySql> for u32[src]

impl<'_> Decode<'_, MySql> for u64[src]

impl<'_> Decode<'_, MySql> for Hyphenated[src]

impl<'_> Decode<'_, Mssql> for bool[src]

impl<'_> Decode<'_, Mssql> for f32[src]

impl<'_> Decode<'_, Mssql> for f64[src]

impl<'_> Decode<'_, Mssql> for i8[src]

impl<'_> Decode<'_, Mssql> for i16[src]

impl<'_> Decode<'_, Mssql> for i32[src]

impl<'_> Decode<'_, Mssql> for i64[src]

impl<'_> Decode<'_, Mssql> for String[src]

Loading content...

Implementors

impl<'_> Decode<'_, MySql> for BigDecimal[src]

impl<'_> Decode<'_, MySql> for Decimal[src]

impl<'_> Decode<'_, MySql> for Uuid[src]

impl<'_> Decode<'_, Postgres> for IpNetwork[src]

impl<'_> Decode<'_, Postgres> for PgMoney[src]

impl<'_> Decode<'_, Postgres> for BigDecimal[src]

impl<'_> Decode<'_, Postgres> for BitVec[src]

impl<'_> Decode<'_, Postgres> for Decimal[src]

impl<'_> Decode<'_, Postgres> for Uuid[src]

impl<'_> Decode<'_, Sqlite> for Uuid[src]

impl<'de> Decode<'de, Postgres> for PgInterval[src]

impl<'r> Decode<'r, MySql> for DateTime<Utc>[src]

impl<'r> Decode<'r, MySql> for NaiveDate[src]

impl<'r> Decode<'r, MySql> for NaiveDateTime[src]

impl<'r> Decode<'r, MySql> for NaiveTime[src]

impl<'r> Decode<'r, MySql> for Date[src]

impl<'r> Decode<'r, MySql> for OffsetDateTime[src]

impl<'r> Decode<'r, MySql> for PrimitiveDateTime[src]

impl<'r> Decode<'r, MySql> for Time[src]

impl<'r> Decode<'r, Postgres> for PgTimeTz<NaiveTime, FixedOffset>[src]

impl<'r> Decode<'r, Postgres> for PgTimeTz<Time, UtcOffset>[src]

impl<'r> Decode<'r, Postgres> for DateTime<FixedOffset>[src]

impl<'r> Decode<'r, Postgres> for DateTime<Local>[src]

impl<'r> Decode<'r, Postgres> for DateTime<Utc>[src]

impl<'r> Decode<'r, Postgres> for NaiveDate[src]

impl<'r> Decode<'r, Postgres> for NaiveDateTime[src]

impl<'r> Decode<'r, Postgres> for NaiveTime[src]

impl<'r> Decode<'r, Postgres> for Date[src]

impl<'r> Decode<'r, Postgres> for OffsetDateTime[src]

impl<'r> Decode<'r, Postgres> for PrimitiveDateTime[src]

impl<'r> Decode<'r, Postgres> for Time[src]

impl<'r> Decode<'r, Sqlite> for DateTime<FixedOffset>[src]

impl<'r> Decode<'r, Sqlite> for DateTime<Local>[src]

impl<'r> Decode<'r, Sqlite> for DateTime<Utc>[src]

impl<'r> Decode<'r, Sqlite> for NaiveDate[src]

impl<'r> Decode<'r, Sqlite> for NaiveDateTime[src]

impl<'r> Decode<'r, Sqlite> for NaiveTime[src]

impl<'r, T> Decode<'r, MySql> for Json<T> where
    T: 'r + Deserialize<'r>, 
[src]

impl<'r, T> Decode<'r, Postgres> for PgRange<T> where
    T: Type<Postgres> + for<'a> Decode<'a, Postgres>, 
[src]

impl<'r, T> Decode<'r, Sqlite> for Json<T> where
    T: 'r + Deserialize<'r>, 
[src]

impl<'r, T: 'r> Decode<'r, Postgres> for Json<T> where
    T: Deserialize<'r>, 
[src]

Loading content...