rocket_db_pools::diesel::prelude

Trait QueryableByName

Source
pub trait QueryableByName<DB>: Sized
where DB: Backend,
{ // Required method fn build<'a>( row: &impl NamedRow<'a, DB>, ) -> Result<Self, Box<dyn Error + Send + Sync>>; }
Expand description

Deserializes the result of a query constructed with sql_query.

This trait can be derived

§Examples

If we just want to map a query to our struct, we can use derive.

#[derive(QueryableByName, PartialEq, Debug)]
struct User {
    id: i32,
    name: String,
}

let first_user = sql_query("SELECT * FROM users ORDER BY id LIMIT 1")
    .get_result(connection)?;
let expected = User { id: 1, name: "Sean".into() };
assert_eq!(expected, first_user);

If we want to do additional work during deserialization, we can use deserialize_as to use a different implementation.

struct LowercaseString(String);

impl Into<String> for LowercaseString {
    fn into(self) -> String {
        self.0
    }
}

impl<DB, ST> FromSql<ST, DB> for LowercaseString
where
    DB: Backend,
    String: FromSql<ST, DB>,
{
    fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
        String::from_sql(bytes)
            .map(|s| LowercaseString(s.to_lowercase()))
    }
}

#[derive(QueryableByName, PartialEq, Debug)]
struct User {
    id: i32,
    #[diesel(deserialize_as = LowercaseString)]
    name: String,
}

let first_user = sql_query("SELECT * FROM users ORDER BY id LIMIT 1")
    .get_result(connection)?;
let expected = User { id: 1, name: "sean".into() };
assert_eq!(expected, first_user);

Required Methods§

Source

fn build<'a>( row: &impl NamedRow<'a, DB>, ) -> Result<Self, Box<dyn Error + Send + Sync>>

Construct an instance of Self from the database row

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>, T9: QueryableByName<__DB>, T10: QueryableByName<__DB>, T11: QueryableByName<__DB>, T12: QueryableByName<__DB>, T13: QueryableByName<__DB>, T14: QueryableByName<__DB>, T15: QueryableByName<__DB>,

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>, T9: QueryableByName<__DB>, T10: QueryableByName<__DB>, T11: QueryableByName<__DB>, T12: QueryableByName<__DB>, T13: QueryableByName<__DB>, T14: QueryableByName<__DB>,

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>, T9: QueryableByName<__DB>, T10: QueryableByName<__DB>, T11: QueryableByName<__DB>, T12: QueryableByName<__DB>, T13: QueryableByName<__DB>,

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>, T9: QueryableByName<__DB>, T10: QueryableByName<__DB>, T11: QueryableByName<__DB>, T12: QueryableByName<__DB>,

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>, T9: QueryableByName<__DB>, T10: QueryableByName<__DB>, T11: QueryableByName<__DB>,

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>, T9: QueryableByName<__DB>, T10: QueryableByName<__DB>,

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>, T9: QueryableByName<__DB>,

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>, T8: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6, T7, T8), Box<dyn Error + Send + Sync>>

Source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>, T7: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6, T7), Box<dyn Error + Send + Sync>>

Source§

impl<T0, T1, T2, T3, T4, T5, T6, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5, T6)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>, T6: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6), Box<dyn Error + Send + Sync>>

Source§

impl<T0, T1, T2, T3, T4, T5, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4, T5)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>, T5: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1, T2, T3, T4, T5), Box<dyn Error + Send + Sync>>

Source§

impl<T0, T1, T2, T3, T4, __DB> QueryableByName<__DB> for (T0, T1, T2, T3, T4)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>, T4: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1, T2, T3, T4), Box<dyn Error + Send + Sync>>

Source§

impl<T0, T1, T2, T3, __DB> QueryableByName<__DB> for (T0, T1, T2, T3)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>, T3: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1, T2, T3), Box<dyn Error + Send + Sync>>

Source§

impl<T0, T1, T2, __DB> QueryableByName<__DB> for (T0, T1, T2)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>, T2: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1, T2), Box<dyn Error + Send + Sync>>

Source§

impl<T0, T1, __DB> QueryableByName<__DB> for (T0, T1)
where __DB: Backend, T0: QueryableByName<__DB>, T1: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0, T1), Box<dyn Error + Send + Sync>>

Source§

impl<T0, __DB> QueryableByName<__DB> for (T0,)
where __DB: Backend, T0: QueryableByName<__DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, __DB>, ) -> Result<(T0,), Box<dyn Error + Send + Sync>>

Source§

impl<T, DB> QueryableByName<DB> for Option<T>
where DB: Backend, T: QueryableByName<DB>,

Source§

fn build<'a>( row: &impl NamedRow<'a, DB>, ) -> Result<Option<T>, Box<dyn Error + Send + Sync>>

Implementors§