use crate::error::Error;
use crate::internal;
pub struct Row(pub(crate) internal::row::Impl);
impl Row {
pub fn get<'r, T, I>(&'r self, index: I) -> Result<T, Error>
where
T: Decode<'r>,
I: RowIndex,
{
internal::row::get(self, index)
}
}
impl From<internal::row::Impl> for Row {
fn from(row: internal::row::Impl) -> Self {
Row(row)
}
}
#[cfg(feature = "sqlx")]
pub trait Decode<'r>: sqlx::Type<sqlx::Any> + sqlx::Decode<'r, sqlx::Any> {}
#[cfg(not(feature = "sqlx"))]
pub trait Decode<'r> {}
#[cfg(feature = "sqlx")]
pub trait RowIndex: sqlx::ColumnIndex<sqlx::any::AnyRow> {}
#[cfg(not(feature = "sqlx"))]
pub trait RowIndex {}
pub trait DecodeOwned: for<'r> Decode<'r> {}
impl<T: for<'r> Decode<'r>> DecodeOwned for T {}
#[cfg(feature = "sqlx")]
const _: () = {
impl<'r, T: sqlx::Type<sqlx::Any> + sqlx::Decode<'r, sqlx::Any>> Decode<'r> for T {}
impl<T: sqlx::ColumnIndex<sqlx::any::AnyRow>> RowIndex for T {}
};
#[cfg(not(feature = "sqlx"))]
const _: () = {
impl<'r, T> Decode<'r> for T {}
impl RowIndex for usize {}
impl RowIndex for &str {}
};
pub trait FromRow: Sized {
fn from_row(row: Row) -> Result<Self, Error>;
fn from_row_using_position(row: Row) -> Result<Self, Error>;
}
macro_rules! impl_from_row {
($($generic:ident@$index:literal),+) => {
impl<$($generic),+> FromRow for ($($generic,)+)
where
$(
$generic: DecodeOwned,
)+
{
fn from_row(row: Row) -> Result<Self, Error> {
Ok((
$(
row.get::<$generic, usize>($index)?,
)+
))
}
fn from_row_using_position(row: Row) -> Result<Self, Error> {
Self::from_row(row)
}
}
};
}
impl_from_row!(A@0);
impl_from_row!(A@0, B@1);
impl_from_row!(A@0, B@1, C@2);
impl_from_row!(A@0, B@1, C@2, D@3);
impl_from_row!(A@0, B@1, C@2, D@3, E@4);
impl_from_row!(A@0, B@1, C@2, D@3, E@4, F@5);
impl_from_row!(A@0, B@1, C@2, D@3, E@4, F@5, G@6);
impl_from_row!(A@0, B@1, C@2, D@3, E@4, F@5, G@6, H@7);