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