Skip to main content

diesel/sqlite/connection/
owned_row.rs

1use super::sqlite_value::{OwnedSqliteValue, SqliteValue};
2use crate::backend::Backend;
3use crate::row::{Field, PartialRow, Row, RowIndex, RowSealed};
4use crate::sqlite::Sqlite;
5use alloc::string::String;
6use alloc::sync::Arc;
7use alloc::vec::Vec;
8
9#[derive(#[automatically_derived]
impl ::core::fmt::Debug for OwnedSqliteRow {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "OwnedSqliteRow", "values", &self.values, "column_names",
            &&self.column_names)
    }
}Debug)]
10pub struct OwnedSqliteRow {
11    pub(super) values: Vec<Option<OwnedSqliteValue>>,
12    column_names: Arc<[Option<String>]>,
13}
14
15impl OwnedSqliteRow {
16    pub(super) fn new(
17        values: Vec<Option<OwnedSqliteValue>>,
18        column_names: Arc<[Option<String>]>,
19    ) -> Self {
20        OwnedSqliteRow {
21            values,
22            column_names,
23        }
24    }
25}
26
27impl RowSealed for OwnedSqliteRow {}
28
29impl<'a> Row<'a, Sqlite> for OwnedSqliteRow {
30    type Field<'field>
31        = OwnedSqliteField<'field>
32    where
33        'a: 'field,
34        Self: 'field;
35    type InnerPartialRow = Self;
36
37    fn field_count(&self) -> usize {
38        self.values.len()
39    }
40
41    fn get<'field, I>(&'field self, idx: I) -> Option<Self::Field<'field>>
42    where
43        'a: 'field,
44        Self: RowIndex<I>,
45    {
46        let idx = self.idx(idx)?;
47        Some(OwnedSqliteField {
48            row: self,
49            col_idx: idx,
50        })
51    }
52
53    fn partial_row(&self, range: core::ops::Range<usize>) -> PartialRow<'_, Self::InnerPartialRow> {
54        PartialRow::new(self, range)
55    }
56}
57
58impl RowIndex<usize> for OwnedSqliteRow {
59    fn idx(&self, idx: usize) -> Option<usize> {
60        if idx < self.field_count() {
61            Some(idx)
62        } else {
63            None
64        }
65    }
66}
67
68impl<'idx> RowIndex<&'idx str> for OwnedSqliteRow {
69    fn idx(&self, field_name: &'idx str) -> Option<usize> {
70        self.column_names
71            .iter()
72            .position(|n| n.as_ref().map(|s| s as &str) == Some(field_name))
73    }
74}
75
76#[allow(missing_debug_implementations)]
77pub struct OwnedSqliteField<'row> {
78    pub(super) row: &'row OwnedSqliteRow,
79    pub(super) col_idx: usize,
80}
81
82impl<'row> Field<'row, Sqlite> for OwnedSqliteField<'row> {
83    fn field_name(&self) -> Option<&str> {
84        self.row
85            .column_names
86            .get(self.col_idx)
87            .and_then(|o| o.as_ref().map(|s| s.as_ref()))
88    }
89
90    fn is_null(&self) -> bool {
91        self.value().is_none()
92    }
93
94    fn value(&self) -> Option<<Sqlite as Backend>::RawValue<'row>> {
95        SqliteValue::from_owned_row(self.row, self.col_idx)
96    }
97}