sqlx_core_oldapi/postgres/
row.rs

1use crate::column::ColumnIndex;
2use crate::error::Error;
3use crate::postgres::message::DataRow;
4use crate::postgres::statement::PgStatementMetadata;
5use crate::postgres::value::PgValueFormat;
6use crate::postgres::{PgColumn, PgValueRef, Postgres};
7use crate::row::Row;
8use std::sync::Arc;
9
10/// Implementation of [`Row`] for PostgreSQL.
11pub struct PgRow {
12    pub(crate) data: DataRow,
13    pub(crate) format: PgValueFormat,
14    pub(crate) metadata: Arc<PgStatementMetadata>,
15}
16
17impl crate::row::private_row::Sealed for PgRow {}
18
19impl Row for PgRow {
20    type Database = Postgres;
21
22    fn columns(&self) -> &[PgColumn] {
23        &self.metadata.columns
24    }
25
26    fn try_get_raw<I>(&self, index: I) -> Result<PgValueRef<'_>, Error>
27    where
28        I: ColumnIndex<Self>,
29    {
30        let index = index.index(self)?;
31        let column = &self.metadata.columns[index];
32        let value = self.data.get(index);
33
34        Ok(PgValueRef {
35            format: self.format,
36            row: Some(&self.data.storage),
37            type_info: column.type_info.clone(),
38            value,
39        })
40    }
41}
42
43impl ColumnIndex<PgRow> for &'_ str {
44    fn index(&self, row: &PgRow) -> Result<usize, Error> {
45        row.metadata
46            .column_names
47            .get(*self)
48            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
49            .map(|v| *v)
50    }
51}
52
53#[cfg(feature = "any")]
54impl From<PgRow> for crate::any::AnyRow {
55    #[inline]
56    fn from(row: PgRow) -> Self {
57        crate::any::AnyRow {
58            columns: row
59                .metadata
60                .columns
61                .iter()
62                .map(|col| col.clone().into())
63                .collect(),
64
65            kind: crate::any::row::AnyRowKind::Postgres(row),
66        }
67    }
68}