1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
use std::collections::HashMap; use std::sync::Arc; use crate::decode::Decode; use crate::postgres::protocol::DataRow; use crate::postgres::Postgres; use crate::row::{Row, RowIndex}; use crate::types::HasSqlType; pub struct PgRow { pub(super) data: DataRow, pub(super) columns: Arc<HashMap<Box<str>, usize>>, } impl Row for PgRow { type Database = Postgres; fn len(&self) -> usize { self.data.len() } fn get<T, I>(&self, index: I) -> T where Self::Database: HasSqlType<T>, I: RowIndex<Self>, T: Decode<Self::Database>, { index.try_get(self).unwrap() } } impl RowIndex<PgRow> for usize { fn try_get<T>(&self, row: &PgRow) -> crate::Result<T> where <PgRow as Row>::Database: HasSqlType<T>, T: Decode<<PgRow as Row>::Database>, { Ok(Decode::decode_nullable(row.data.get(*self))?) } } impl RowIndex<PgRow> for &'_ str { fn try_get<T>(&self, row: &PgRow) -> crate::Result<T> where <PgRow as Row>::Database: HasSqlType<T>, T: Decode<<PgRow as Row>::Database>, { let index = row .columns .get(*self) .ok_or_else(|| crate::Error::ColumnNotFound((*self).into()))?; let value = Decode::decode_nullable(row.data.get(*index))?; Ok(value) } } impl_from_row_for_row!(PgRow);