sqlx_postgres/
column.rs

1use crate::ext::ustr::UStr;
2use crate::{PgTypeInfo, Postgres};
3
4use sqlx_core::column::ColumnOrigin;
5pub(crate) use sqlx_core::column::{Column, ColumnIndex};
6
7#[derive(Debug, Clone)]
8#[cfg_attr(feature = "offline", derive(serde::Serialize, serde::Deserialize))]
9pub struct PgColumn {
10    pub(crate) ordinal: usize,
11    pub(crate) name: UStr,
12    pub(crate) type_info: PgTypeInfo,
13
14    #[cfg_attr(feature = "offline", serde(default))]
15    pub(crate) origin: ColumnOrigin,
16
17    #[cfg_attr(feature = "offline", serde(skip))]
18    pub(crate) relation_id: Option<crate::types::Oid>,
19    #[cfg_attr(feature = "offline", serde(skip))]
20    pub(crate) relation_attribute_no: Option<i16>,
21}
22
23impl PgColumn {
24    /// Returns the OID of the table this column is from, if applicable.
25    ///
26    /// This will be `None` if the column is the result of an expression.
27    ///
28    /// Corresponds to column `attrelid` of the `pg_catalog.pg_attribute` table:
29    /// <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
30    pub fn relation_id(&self) -> Option<crate::types::Oid> {
31        self.relation_id
32    }
33
34    /// Returns the 1-based index of this column in its parent table, if applicable.
35    ///
36    /// This will be `None` if the column is the result of an expression.
37    ///
38    /// Corresponds to column `attnum` of the `pg_catalog.pg_attribute` table:
39    /// <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
40    pub fn relation_attribute_no(&self) -> Option<i16> {
41        self.relation_attribute_no
42    }
43}
44
45impl Column for PgColumn {
46    type Database = Postgres;
47
48    fn ordinal(&self) -> usize {
49        self.ordinal
50    }
51
52    fn name(&self) -> &str {
53        &self.name
54    }
55
56    fn type_info(&self) -> &PgTypeInfo {
57        &self.type_info
58    }
59
60    fn origin(&self) -> ColumnOrigin {
61        self.origin.clone()
62    }
63}