Trait diesel::deserialize::FromSql

source ·
pub trait FromSql<A, DB: Backend>: Sized {
    // Required method
    fn from_sql(bytes: DB::RawValue<'_>) -> Result<Self>;

    // Provided method
    fn from_nullable_sql(bytes: Option<DB::RawValue<'_>>) -> Result<Self> { ... }
}
Expand description

Deserialize a single field of a given SQL type.

When possible, implementations of this trait should prefer to use an existing implementation, rather than reading from bytes. (For example, if you are implementing this for an enum which is represented as an integer in the database, prefer i32::from_sql(bytes) (or the explicit form <i32 as FromSql<Integer, DB>>::from_sql(bytes)) over reading from bytes directly)

Types which implement this trait should also have #[derive(FromSqlRow)]

§Backend specific details

  • For PostgreSQL, the bytes will be sent using the binary protocol, not text.
  • For SQLite, the actual type of DB::RawValue is private API. All implementations of this trait must be written in terms of an existing primitive.
  • For MySQL, the value of bytes will depend on the return value of type_metadata for the given SQL type. See MysqlType for details.
  • For third party backends, consult that backend’s documentation.

§Examples

Most implementations of this trait will be defined in terms of an existing implementation.

#[repr(i32)]
#[derive(Debug, Clone, Copy, FromSqlRow)]
pub enum MyEnum {
    A = 1,
    B = 2,
}

impl<DB> FromSql<Integer, DB> for MyEnum
where
    DB: Backend,
    i32: FromSql<Integer, DB>,
{
    fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
        match i32::from_sql(bytes)? {
            1 => Ok(MyEnum::A),
            2 => Ok(MyEnum::B),
            x => Err(format!("Unrecognized variant {}", x).into()),
        }
    }
}

Required Methods§

source

fn from_sql(bytes: DB::RawValue<'_>) -> Result<Self>

See the trait documentation.

Provided Methods§

source

fn from_nullable_sql(bytes: Option<DB::RawValue<'_>>) -> Result<Self>

A specialized variant of from_sql for handling null values.

The default implementation returns an UnexpectedNullError for an encountered null value and calls Self::from_sql otherwise

If your custom type supports null values you need to provide a custom implementation.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FromSql<BigInt, Sqlite> for i64

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Binary, Sqlite> for *const [u8]

The returned pointer is only valid for the lifetime to the argument of from_sql. This impl is intended for uses where you want to write a new impl in terms of Vec<u8>, but don’t want to allocate. We have to return a raw pointer instead of a reference with a lifetime due to the structure of FromSql

source§

fn from_sql(bytes: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Bool, Sqlite> for bool

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Date, Sqlite> for String

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Double, Sqlite> for f64

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Float, Sqlite> for f32

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Integer, Sqlite> for i32

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<SmallInt, Sqlite> for i16

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Text, Sqlite> for *const str

The returned pointer is only valid for the lifetime to the argument of from_sql. This impl is intended for uses where you want to write a new impl in terms of String, but don’t want to allocate. We have to return a raw pointer instead of a reference with a lifetime due to the structure of FromSql

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Time, Sqlite> for String

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Timestamp, Sqlite> for String

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl FromSql<Timestamptz, Sqlite> for String

source§

fn from_sql(value: SqliteValue<'_, '_, '_>) -> Result<Self>

source§

impl<'a, T, ST, DB> FromSql<ST, DB> for Cow<'a, T>
where T: 'a + ToOwned + ?Sized, DB: Backend, T::Owned: FromSql<ST, DB>,

source§

fn from_sql(bytes: DB::RawValue<'_>) -> Result<Self>

source§

impl<ST, DB> FromSql<ST, DB> for String
where DB: Backend, *const str: FromSql<ST, DB>,

source§

fn from_sql(bytes: DB::RawValue<'_>) -> Result<Self>

source§

impl<ST, DB> FromSql<ST, DB> for Vec<u8>
where DB: Backend, *const [u8]: FromSql<ST, DB>,

source§

fn from_sql(bytes: DB::RawValue<'_>) -> Result<Self>

source§

impl<T, ST, DB> FromSql<Nullable<ST>, DB> for Option<T>
where T: FromSql<ST, DB>, DB: Backend, ST: SqlType<IsNull = NotNull>,

source§

fn from_sql(bytes: DB::RawValue<'_>) -> Result<Self>

source§

fn from_nullable_sql(bytes: Option<DB::RawValue<'_>>) -> Result<Self>

Implementors§

source§

impl<'a, DB> FromSql<Text, DB> for MigrationVersion<'a>
where String: FromSql<Text, DB>, DB: Backend,