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
56
57
58
59
use std::collections::HashMap;
use std::sync::Arc;

use crate::decode::Decode;
use crate::mysql::protocol;
use crate::mysql::MySql;
use crate::row::{Row, RowIndex};
use crate::types::HasSqlType;

pub struct MySqlRow {
    pub(super) row: protocol::Row,
    pub(super) columns: Arc<HashMap<Box<str>, usize>>,
}

impl Row for MySqlRow {
    type Database = MySql;

    fn len(&self) -> usize {
        self.row.len()
    }

    fn get<T, I>(&self, index: I) -> T
    where
        Self::Database: HasSqlType<T>,
        I: RowIndex<Self>,
        T: Decode<Self::Database>,
    {
        index.try_get(self).unwrap()
    }
}

impl RowIndex<MySqlRow> for usize {
    fn try_get<T>(&self, row: &MySqlRow) -> crate::Result<T>
    where
        <MySqlRow as Row>::Database: HasSqlType<T>,
        T: Decode<<MySqlRow as Row>::Database>,
    {
        Ok(Decode::decode_nullable(row.row.get(*self))?)
    }
}

impl RowIndex<MySqlRow> for &'_ str {
    fn try_get<T>(&self, row: &MySqlRow) -> crate::Result<T>
    where
        <MySqlRow as Row>::Database: HasSqlType<T>,
        T: Decode<<MySqlRow as Row>::Database>,
    {
        let index = row
            .columns
            .get(*self)
            .ok_or_else(|| crate::Error::ColumnNotFound((*self).into()))?;

        let value = Decode::decode_nullable(row.row.get(*index))?;

        Ok(value)
    }
}

impl_from_row_for_row!(MySqlRow);