sqlx_rxqlite/
row.rs

1#![allow(clippy::rc_buffer)]
2
3use std::sync::Arc;
4
5use sqlx_core::column::ColumnIndex;
6use sqlx_core::error::Error;
7use sqlx_core::ext::ustr::UStr;
8use sqlx_core::row::Row;
9use sqlx_core::HashMap;
10
11//use crate::statement::StatementHandle;
12use crate::{RXQLite, RXQLiteColumn, RXQLiteValue, RXQLiteValueRef};
13
14/// Implementation of [`Row`] for SQLite.
15pub struct RXQLiteRow {
16    pub(crate) values: Box<[RXQLiteValue]>,
17    pub(crate) columns: Arc<Vec<RXQLiteColumn>>,
18    pub(crate) column_names: Arc<HashMap<UStr, usize>>,
19}
20
21// Accessing values from the statement object is
22// safe across threads as long as we don't call [sqlite3_step]
23
24// we block ourselves from doing that by only exposing
25// a set interface on [StatementHandle]
26
27//unsafe impl Send for RXQLiteRow {}
28//unsafe impl Sync for RXQLiteRow {}
29
30impl RXQLiteRow {
31    /*
32    pub(crate) fn current(
33        statement: &StatementHandle,
34        columns: &Arc<Vec<RXQLiteColumn>>,
35        column_names: &Arc<HashMap<UStr, usize>>,
36    ) -> Self {
37        let size = statement.column_count();
38        let mut values = Vec::with_capacity(size);
39
40        for i in 0..size {
41            values.push(unsafe {
42                let raw = statement.column_value(i);
43
44                RXQLiteValue::new(raw, columns[i].type_info.clone())
45            });
46        }
47
48        Self {
49            values: values.into_boxed_slice(),
50            columns: Arc::clone(columns),
51            column_names: Arc::clone(column_names),
52        }
53    }
54    */
55}
56
57impl Row for RXQLiteRow {
58    type Database = RXQLite;
59
60    fn columns(&self) -> &[RXQLiteColumn] {
61        &self.columns
62    }
63
64    fn try_get_raw<I>(&self, index: I) -> Result<RXQLiteValueRef<'_>, Error>
65    where
66        I: ColumnIndex<Self>,
67    {
68        let index = index.index(self)?;
69        Ok(RXQLiteValueRef::value(&self.values[index]))
70    }
71}
72
73impl ColumnIndex<RXQLiteRow> for &'_ str {
74    fn index(&self, row: &RXQLiteRow) -> Result<usize, Error> {
75        row.column_names
76            .get(*self)
77            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
78            .map(|v| *v)
79    }
80}
81
82// #[cfg(feature = "any")]
83// impl From<RXQLiteRow> for crate::any::AnyRow {
84//     #[inline]
85//     fn from(row: RXQLiteRow) -> Self {
86//         crate::any::AnyRow {
87//             columns: row.columns.iter().map(|col| col.clone().into()).collect(),
88//             kind: crate::any::row::AnyRowKind::RXQLite(row),
89//         }
90//     }
91// }