use std::sync::Arc; use super::sqlite_value::{OwnedSqliteValue, SqliteValue}; use crate::backend::Backend; use crate::row::{Field, PartialRow, Row, RowIndex, RowSealed}; use crate::sqlite::Sqlite; #[derive(Debug)] pub struct OwnedSqliteRow { pub(super) values: Vec>, column_names: Arc<[Option]>, } impl OwnedSqliteRow { pub(super) fn new( values: Vec>, column_names: Arc<[Option]>, ) -> Self { OwnedSqliteRow { values, column_names, } } } impl RowSealed for OwnedSqliteRow {} impl<'a> Row<'a, Sqlite> for OwnedSqliteRow { type Field<'field> = OwnedSqliteField<'field> where 'a: 'field, Self: 'field; type InnerPartialRow = Self; fn field_count(&self) -> usize { self.values.len() } fn get<'field, I>(&'field self, idx: I) -> Option> where 'a: 'field, Self: RowIndex, { let idx = self.idx(idx)?; Some(OwnedSqliteField { row: self, col_idx: idx, }) } fn partial_row(&self, range: std::ops::Range) -> PartialRow<'_, Self::InnerPartialRow> { PartialRow::new(self, range) } } impl RowIndex for OwnedSqliteRow { fn idx(&self, idx: usize) -> Option { if idx < self.field_count() { Some(idx) } else { None } } } impl<'idx> RowIndex<&'idx str> for OwnedSqliteRow { fn idx(&self, field_name: &'idx str) -> Option { self.column_names .iter() .position(|n| n.as_ref().map(|s| s as &str) == Some(field_name)) } } #[allow(missing_debug_implementations)] pub struct OwnedSqliteField<'row> { pub(super) row: &'row OwnedSqliteRow, pub(super) col_idx: usize, } impl<'row> Field<'row, Sqlite> for OwnedSqliteField<'row> { fn field_name(&self) -> Option<&str> { self.row .column_names .get(self.col_idx) .and_then(|o| o.as_ref().map(|s| s.as_ref())) } fn is_null(&self) -> bool { self.value().is_none() } fn value(&self) -> Option<::RawValue<'row>> { SqliteValue::from_owned_row(self.row, self.col_idx) } }