Skip to main content

rorm_db/row/
mod.rs

1//! This module defines a wrapper for sqlx's AnyRow
2
3pub use self::error::*;
4pub use self::index::*;
5use crate::internal::any::{AnyDecode, AnyRow, AnyType};
6use crate::row::get::try_get;
7
8mod error;
9mod get;
10mod index;
11
12/// Represents a single row from the database.
13pub struct Row(pub(crate) AnyRow);
14
15impl Row {
16    /// Index into the database row and decode a single value.
17    ///
18    /// A string index can be used to access a column by name
19    /// and a `usize` index can be used to access a column by position.
20    pub fn get<'r, 'i, T>(&'r self, index: impl Into<RowIndex<'i>>) -> Result<T, RowError<'i>>
21    where
22        T: Decode<'r>,
23    {
24        match &self.0 {
25            #[cfg(feature = "postgres")]
26            AnyRow::Postgres(row) => try_get(row, index.into()),
27            #[cfg(feature = "sqlite")]
28            AnyRow::Sqlite(row) => try_get(row, index.into()),
29        }
30    }
31}
32
33/// Something which can be decoded from a [`Row`]'s cell.
34pub trait Decode<'r>: AnyType + AnyDecode<'r> {}
35impl<'r, T: AnyType + AnyDecode<'r>> Decode<'r> for T {}
36
37/// Something which can be decoded from a [`Row`]'s cell without borrowing.
38pub trait DecodeOwned: for<'r> Decode<'r> {}
39impl<T: for<'r> Decode<'r>> DecodeOwned for T {}