Query

Trait Query 

Source
pub trait Query<C: Client>:
    QueryText
    + ToParams<C>
    + Sync {
    type Row: FromRow<C>;
}
Expand description

A database query that returns zero or more result rows.

A Query is something that has QueryText, can be converted to the parameters of some database Client, and has a result type that can be produced from that Client’s rows.

You can use the derive macro to produce each of these parts:

#[derive(FromRow)]
struct Todo {
    id: i32,
    label: String,
}

#[derive(Query)]
#[aykroyd(row(Todo), text = "SELECT id, label FROM todo")]
struct GetAllTodos;

Just as with a Statement, a Query can have parameters, taken in source order. For queries with more than a handful of parameters, this can get error-prone. Help ensure that the struct fields and the query text stay in sync by annotating parameter index on the fields.

#[derive(Query)]
#[aykroyd(row(Pet), text = "
    SELECT first_name, last_name, species
    FROM pet
    WHERE first_name = $1
    AND last_name = $2
    AND species = $3
")]
struct SearchPets<'a> {
    #[aykroyd(param = "$3")]
    pub species: &'a str,
    #[aykroyd(param = "$1")]
    pub first: &'a str,
    #[aykroyd(param = "$2")]
    pub last: &'a str,
}

The query text can be provided inline, as above, or loaded from a file. The path is relative to a queries/ directory at the root of the crate.

#[derive(Statement)]
#[aykroyd(file = "summarize-quarter.sql")]
struct SummarizeQuarter;

Required Associated Types§

Implementors§

Source§

impl<C, R, A, B> Query<C> for Either<A, B>
where C: Client, R: FromRow<C>, A: Query<C, Row = R>, B: Query<C, Row = R>,

Source§

type Row = R