sqlx_xugu/
row.rs

1use sqlx_core::bytes::Bytes;
2use sqlx_core::ext::ustr::UStr;
3pub(crate) use sqlx_core::row::*;
4use sqlx_core::HashMap;
5use std::sync::Arc;
6
7use crate::column::{ColumnIndex, XuguColumn};
8use crate::error::Error;
9use crate::{Xugu, XuguValueRef};
10
11/// Implementation of [`Row`] for Xugu.
12#[derive(Debug)]
13pub struct XuguRow {
14    pub(crate) row: Arc<Vec<Bytes>>,
15    pub(crate) columns: Arc<Vec<XuguColumn>>,
16    pub(crate) column_names: Arc<HashMap<UStr, usize>>,
17}
18
19impl Row for XuguRow {
20    type Database = Xugu;
21
22    fn columns(&self) -> &[XuguColumn] {
23        &self.columns
24    }
25
26    fn try_get_raw<I>(&self, index: I) -> Result<XuguValueRef<'_>, 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).map(|x| x.as_ref());
33
34        Ok(XuguValueRef {
35            row: Some(&self.row),
36            type_info: column.type_info.clone(),
37            value,
38        })
39    }
40}
41
42impl ColumnIndex<XuguRow> for &'_ str {
43    fn index(&self, row: &XuguRow) -> Result<usize, Error> {
44        row.column_names
45            .get(*self)
46            .or_else(|| {
47                // 列名忽略大小写时,列名大写检索
48                row.column_names.get(&*self.to_uppercase())
49            })
50            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
51            .copied()
52    }
53}