1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Contains the Row and FromRow traits.

use crate::database::Database;
use crate::decode::Decode;
use crate::types::HasSqlType;

pub trait RowIndex<R: ?Sized>
where
    R: Row,
{
    fn try_get<T>(&self, row: &R) -> crate::Result<T>
    where
        R::Database: HasSqlType<T>,
        T: Decode<R::Database>;
}

/// Represents a single row of the result set.
pub trait Row: Unpin + Send + 'static {
    type Database: Database + ?Sized;

    /// Returns `true` if the row contains no values.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the number of values in the row.
    fn len(&self) -> usize;

    /// Returns the value at the `index`; can either be an integer ordinal or a column name.
    fn get<T, I>(&self, index: I) -> T
    where
        Self::Database: HasSqlType<T>,
        I: RowIndex<Self>,
        T: Decode<Self::Database>;
}

/// A **record** that can be built from a row returned from by the database.
pub trait FromRow<R>
where
    R: Row,
{
    fn from_row(row: R) -> Self;
}

#[allow(unused_macros)]
macro_rules! impl_from_row_for_row {
    ($R:ty) => {
        impl crate::row::FromRow<$R> for $R {
            #[inline]
            fn from_row(row: $R) -> Self {
                row
            }
        }
    };
}