sqlx_mysql/
row.rs

1use std::sync::Arc;
2
3pub(crate) use sqlx_core::row::*;
4
5use crate::column::ColumnIndex;
6use crate::error::Error;
7use crate::ext::ustr::UStr;
8use crate::HashMap;
9use crate::{protocol, MySql, MySqlColumn, MySqlValueFormat, MySqlValueRef};
10
11/// Implementation of [`Row`] for MySQL.
12pub struct MySqlRow {
13    pub(crate) row: protocol::Row,
14    pub(crate) format: MySqlValueFormat,
15    pub(crate) columns: Arc<Vec<MySqlColumn>>,
16    pub(crate) column_names: Arc<HashMap<UStr, usize>>,
17}
18
19impl Row for MySqlRow {
20    type Database = MySql;
21
22    fn columns(&self) -> &[MySqlColumn] {
23        &self.columns
24    }
25
26    fn try_get_raw<I>(&self, index: I) -> Result<MySqlValueRef<'_>, Error>
27    where
28        I: ColumnIndex<Self>,
29    {
30        let index = index.index(self)?;
31        let column = &self.columns[index];
32        let value = self.row.get(index);
33
34        Ok(MySqlValueRef {
35            format: self.format,
36            row: Some(&self.row.storage),
37            type_info: column.type_info.clone(),
38            value,
39        })
40    }
41}
42
43impl ColumnIndex<MySqlRow> for &'_ str {
44    fn index(&self, row: &MySqlRow) -> Result<usize, Error> {
45        row.column_names
46            .get(*self)
47            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
48            .copied()
49    }
50}
51
52impl std::fmt::Debug for MySqlRow {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        debug_row(self, f)
55    }
56}