1use cdbc::column::ColumnIndex;
2use cdbc::error::Error;
3
4use crate::{protocol, MySql, MySqlColumn, MySqlValueFormat, MySqlValueRef};
5use cdbc::row::Row;
6use cdbc::HashMap;
7use std::sync::Arc;
8use cdbc::utils::ustr::UStr;
9
10#[derive(Debug)]
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
19
20impl Row for MySqlRow {
21 type Database = MySql;
22
23 fn columns(&self) -> &[MySqlColumn] {
24 &self.columns
25 }
26
27 fn try_get_raw<I>(&self, index: I) -> Result<MySqlValueRef<'_>, Error>
28 where
29 I: ColumnIndex<Self>,
30 {
31 let index = index.index(self)?;
32 let column = &self.columns[index];
33 let value = self.row.get(index);
34
35 Ok(MySqlValueRef {
36 format: self.format,
37 row: Some(&self.row.storage),
38 type_info: column.type_info.clone(),
39 value,
40 })
41 }
42}
43
44impl ColumnIndex<MySqlRow> for &'_ str {
45 fn index(&self, row: &MySqlRow) -> Result<usize, Error> {
46 row.column_names
47 .get(*self)
48 .ok_or_else(|| Error::ColumnNotFound((*self).into()))
49 .map(|v| *v)
50 }
51}