[][src]Trait sqlx::FromRow

pub trait FromRow<'r, R> where
    R: Row
{ fn from_row(row: &'r R) -> Result<Self, Error>; }

A record that can be built from a row returned by the database.

In order to use query_as the output type must implement FromRow.

Derivable

This trait can be derived by SQLx for any struct. The generated implementation will consist of a sequence of calls to Row::try_get using the name from each struct field.

This example is not tested
#[derive(sqlx::FromRow)]
struct User {
    id: i32,
    name: String,
}

Field attributes

Several attributes can be specified to customize how each column in a row is read:

rename

When the name of a field in Rust does not match the name of its corresponding column, you can use the rename attribute to specify the name that the field has in the row. For example:

This example is not tested
#[derive(sqlx::FromRow)]
struct User {
    id: i32,
    name: String,
    #[sqlx(rename = "description")]
    about_me: String
}

Given a query such as:

SELECT id, name, description FROM users;

will read the content of the column description into the field about_me.

rename_all

By default, field names are expected verbatim (with the exception of the raw identifier prefix r#, if present). Placed at the struct level, this attribute changes how the field name is mapped to its SQL column name:

This example is not tested
#[derive(sqlx::FromRow)]
#[sqlx(rename_all = "camelCase")]
struct UserPost {
    id: i32,
    // remapped to "userId"
    user_id: i32,
    contents: String
}

The supported values are snake_case (available if you have non-snake-case field names for some reason), lowercase, UPPERCASE, camelCase, SCREAMING_SNAKE_CASE and kebab-case. The styling of each option is intended to be an example of its behavior.

default

When your struct contains a field that is not present in your query, if the field type has an implementation for Default, you can use the default attribute to assign the default value to said field. For example:

This example is not tested
#[derive(sqlx::FromRow)]
struct User {
    id: i32,
    name: String,
    #[sqlx(default)]
    location: Option<String>
}

Given a query such as:

SELECT id, name FROM users;

will set the value of the field location to the default value of Option<String>, which is None.

Required methods

fn from_row(row: &'r R) -> Result<Self, Error>

Loading content...

Implementations on Foreign Types

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T10: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T11: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T12: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T13: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T14: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T15: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T16: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T10: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T11: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T10: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T11: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T12: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T10: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T11: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T12: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T13: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T14: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5> FromRow<'r, R> for (T1, T2, T3, T4, T5) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3> FromRow<'r, R> for (T1, T2, T3) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1> FromRow<'r, R> for (T1,) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T10: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T11: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T12: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T13: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2> FromRow<'r, R> for (T1, T2) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4> FromRow<'r, R> for (T1, T2, T3, T4) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T10: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

impl<'r, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> FromRow<'r, R> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) where
    R: Row,
    T1: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T10: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T11: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T12: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T13: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T14: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T15: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T2: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T3: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T4: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T5: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T6: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T7: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T8: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    T9: Decode<'r, <R as Row>::Database> + Type<<R as Row>::Database>,
    usize: ColumnIndex<R>, 
[src]

Loading content...

Implementors

Loading content...