use crate::client::Client;
use crate::error::Error;
use crate::query::{QueryText, ToParams};
use crate::row::{ColumnsIndexed, FromColumnsIndexed};
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::FromRow;
#[derive(FromRow)]
pub struct Customer {
id: i32,
first_name: String,
last_name: String,
}
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::FromRow;
#[derive(FromRow)]
#[aykroyd(by_index)]
pub struct Customer {
id: i32,
first_name: String,
last_name: String,
}
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::FromRow;
#[derive(FromRow)]
pub struct QueryResults(i32, f32, String);
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::Query;
use rust_decimal::Decimal;
#[derive(Query)]
#[aykroyd(row((i32, Decimal)), text = "
SELECT EXTRACT(MONTH FROM closed_on), SUM(amount) FROM sales
")]
pub struct SalesByMonth;
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::FromRow;
#[derive(FromRow)]
pub struct Widget {
#[aykroyd(column = "type")]
pub ty: String,
}
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::FromRow;
#[derive(FromRow)]
pub struct Widget {
#[aykroyd(column = 4)]
pub ty: String,
}
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::{FromRow, Query};
# struct Color;
#[derive(FromRow)]
#[aykroyd(by_index)]
struct Person {
name: String,
favorite_color: Color,
}
#[derive(FromRow)]
#[aykroyd(by_index)]
struct Pet {
name: String,
#[aykroyd(nested)]
owner: Person,
#[aykroyd(nested)]
vet: Option<Person>,
}
#[derive(Query)]
#[aykroyd(row(Pet), text = "
SELECT pet.name, owner.name, owner.fav_color FROM pets
")]
struct GetPets;
```
"##
)]
pub trait FromRow<C: Client>: Sized {
fn from_row(row: &C::Row<'_>) -> Result<Self, Error<C::Error>>;
}
macro_rules! impl_tuple_from_row {
(
$(
$name:ident
),*
$(,)?
) => {
impl<
C,
$(
$name,
)*
> FromRow<C> for ($($name,)*)
where
C: Client,
($($name,)*): FromColumnsIndexed<C>,
{
fn from_row(row: &C::Row<'_>) -> Result<Self, Error<C::Error>> {
FromColumnsIndexed::from_columns(ColumnsIndexed::new(row))
}
}
};
}
impl_tuple_from_row!();
impl_tuple_from_row!(T0);
impl_tuple_from_row!(T0, T1);
impl_tuple_from_row!(T0, T1, T2);
impl_tuple_from_row!(T0, T1, T2, T3);
impl_tuple_from_row!(T0, T1, T2, T3, T4);
impl_tuple_from_row!(T0, T1, T2, T3, T4, T5);
impl_tuple_from_row!(T0, T1, T2, T3, T4, T5, T6);
impl_tuple_from_row!(T0, T1, T2, T3, T4, T5, T6, T7);
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::Statement;
#[derive(Statement)]
#[aykroyd(text = "
INSERT INTO customers (first_name, last_name) VALUES ($1, $2)
")]
pub struct InsertCustomer<'a> {
first_name: &'a str,
last_name: &'a str,
}
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::Statement;
#[derive(Statement)]
#[aykroyd(text = "
INSERT INTO customers (first, last, middle)
VALUES ($1, $2, $3)
")]
pub struct InsertCustomer<'a> {
#[aykroyd(param = "$1")]
pub first: &'a str,
#[aykroyd(param = "$3")]
pub middle: &'a str,
#[aykroyd(param = "$2")]
pub last: &'a str,
}
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::Statement;
#[derive(Statement)]
#[aykroyd(file = "insert-customer.sql")]
pub struct InsertCustomer<'a> {
#[aykroyd(param = "$1")]
pub first: &'a str,
#[aykroyd(param = "$3")]
pub middle: &'a str,
#[aykroyd(param = "$2")]
pub last: &'a str,
}
```
"##
)]
pub trait Statement<C: Client>: QueryText + ToParams<C> + Sync {}
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::{FromRow, Query};
#[derive(FromRow)]
struct Todo {
id: i32,
label: String,
}
#[derive(Query)]
#[aykroyd(row(Todo), text = "SELECT id, label FROM todo")]
struct GetAllTodos;
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::Query;
# #[derive(aykroyd::FromRow)]
# struct Pet;
#[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,
}
```
"##
)]
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::Statement;
#[derive(Statement)]
#[aykroyd(file = "summarize-quarter.sql")]
struct SummarizeQuarter;
```
"##
)]
pub trait Query<C: Client>: QueryText + ToParams<C> + Sync {
type Row: FromRow<C>;
}
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use aykroyd::{FromRow, QueryOne};
#[derive(FromRow)]
struct Todo {
id: i32,
label: String,
}
#[derive(QueryOne)]
#[aykroyd(row(Todo), text = "
SELECT id, label FROM todo WHERE id = $1
")]
struct GetTodoById(i32);
```
"##
)]
pub trait QueryOne<C: Client>: Query<C> {}