| 1 |
use super::result::PgResult; |
| 2 |
use crate::backend::Backend; |
| 3 |
use crate::pg::value::TypeOidLookup; |
| 4 |
use crate::pg::{Pg, PgValue}; |
| 5 |
use crate::row::*; |
| 6 |
use std::rc::Rc; |
| 7 |
|
| 8 |
#[derive(Clone)] |
| 9 |
#[allow(missing_debug_implementations)] |
| 10 |
pub struct PgRow { |
| 11 |
db_result: Rc<PgResult>, |
| 12 |
row_idx: usize, |
| 13 |
} |
| 14 |
|
| 15 |
impl PgRow { |
| 16 |
pub(crate) fn new(db_result: Rc<PgResult>, row_idx: usize) -> Self { |
| 17 |
PgRow { db_result, row_idx } |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl RowSealed for PgRow {} |
| 22 |
|
| 23 |
impl<'a> Row<'a, Pg> for PgRow { |
| 24 |
type Field<'f> |
| 25 |
= PgField<'f> |
| 26 |
where |
| 27 |
'a: 'f, |
| 28 |
Self: 'f; |
| 29 |
type InnerPartialRow = Self; |
| 30 |
|
| 31 |
fn field_count(&self) -> usize { |
| 32 |
self.db_result.column_count() |
| 33 |
} |
| 34 |
|
| 35 |
fn get<'b, I>(&'b self, idx: I) -> Option<Self::Field<'b>> |
| 36 |
where |
| 37 |
'a: 'b, |
| 38 |
Self: RowIndex<I>, |
| 39 |
{ |
| 40 |
let idx = self.idx(idx)?; |
| 41 |
Some(PgField { |
| 42 |
db_result: &self.db_result, |
| 43 |
row_idx: self.row_idx, |
| 44 |
col_idx: idx, |
| 45 |
}) |
| 46 |
} |
| 47 |
|
| 48 |
fn partial_row(&self, range: std::ops::Range<usize>) -> PartialRow<'_, Self::InnerPartialRow> { |
| 49 |
PartialRow::new(self, range) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl RowIndex<usize> for PgRow { |
| 54 |
fn idx(&self, idx: usize) -> Option<usize> { |
| 55 |
if idx < self.field_count() { |
| 56 |
Some(idx) |
| 57 |
} else { |
| 58 |
None |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl<'a> RowIndex<&'a str> for PgRow { |
| 64 |
fn idx(&self, field_name: &'a str) -> Option<usize> { |
| 65 |
(0..self.field_count()).find(|idx| self.db_result.column_name(*idx) == Some(field_name)) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[allow(missing_debug_implementations)] |
| 70 |
pub struct PgField<'a> { |
| 71 |
db_result: &'a PgResult, |
| 72 |
row_idx: usize, |
| 73 |
col_idx: usize, |
| 74 |
} |
| 75 |
|
| 76 |
impl<'a> Field<'a, Pg> for PgField<'a> { |
| 77 |
fn field_name(&self) -> Option<&str> { |
| 78 |
self.db_result.column_name(self.col_idx) |
| 79 |
} |
| 80 |
|
| 81 |
fn value(&self) -> Option<<Pg as Backend>::RawValue<'_>> { |
| 82 |
let raw = self.db_result.get(self.row_idx, self.col_idx)?; |
| 83 |
|
| 84 |
Some(PgValue::new_internal(raw, self)) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl TypeOidLookup for PgField<'_> { |
| 89 |
fn lookup(&self) -> std::num::NonZeroU32 { |
| 90 |
self.db_result.column_type(self.col_idx) |
| 91 |
} |
| 92 |
} |