Skip to content

Commit

Permalink
feat: expose relation_id and relation_attribution_no on PgColumn (#3492)
Browse files Browse the repository at this point in the history
* expose relation_id and relation_attribution_no on PgColumn

* Update sqlx-postgres/src/message/row_description.rs

Co-authored-by: Austin Bonander <[email protected]>

* Update sqlx-postgres/src/column.rs

Co-authored-by: Austin Bonander <[email protected]>

* Update sqlx-postgres/src/message/row_description.rs

Co-authored-by: Austin Bonander <[email protected]>

* Update sqlx-postgres/src/column.rs

Co-authored-by: Austin Bonander <[email protected]>

* Update sqlx-postgres/src/message/row_description.rs

Co-authored-by: Austin Bonander <[email protected]>

* fix

---------

Co-authored-by: Austin Bonander <[email protected]>
  • Loading branch information
kurtbuilds and abonander authored Sep 8, 2024
1 parent 10bec32 commit c597a22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 23 additions & 1 deletion sqlx-postgres/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,33 @@ pub struct PgColumn {
pub(crate) name: UStr,
pub(crate) type_info: PgTypeInfo,
#[cfg_attr(feature = "offline", serde(skip))]
pub(crate) relation_id: Option<i32>,
pub(crate) relation_id: Option<crate::types::Oid>,
#[cfg_attr(feature = "offline", serde(skip))]
pub(crate) relation_attribute_no: Option<i16>,
}

impl PgColumn {
/// Returns the OID of the table this column is from, if applicable.
///
/// This will be `None` if the column is the result of an expression.
///
/// Corresponds to column `attrelid` of the `pg_catalog.pg_attribute` table:
/// <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
pub fn relation_id(&self) -> Option<crate::types::Oid> {
self.relation_id
}

/// Returns the 1-based index of this column in its parent table, if applicable.
///
/// This will be `None` if the column is the result of an expression.
///
/// Corresponds to column `attnum` of the `pg_catalog.pg_attribute` table:
/// <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
pub fn relation_attribute_no(&self) -> Option<i16> {
self.relation_attribute_no
}
}

impl Column for PgColumn {
type Database = Postgres;

Expand Down
6 changes: 3 additions & 3 deletions sqlx-postgres/src/message/row_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Field {

/// If the field can be identified as a column of a specific table, the
/// object ID of the table; otherwise zero.
pub relation_id: Option<i32>,
pub relation_id: Option<Oid>,

/// If the field can be identified as a column of a specific table, the attribute number of
/// the column; otherwise zero.
Expand Down Expand Up @@ -65,7 +65,7 @@ impl BackendMessage for RowDescription {
));
}

let relation_id = buf.get_i32();
let relation_id = buf.get_u32();
let relation_attribute_no = buf.get_i16();
let data_type_id = Oid(buf.get_u32());
let data_type_size = buf.get_i16();
Expand All @@ -77,7 +77,7 @@ impl BackendMessage for RowDescription {
relation_id: if relation_id == 0 {
None
} else {
Some(relation_id)
Some(Oid(relation_id))
},
relation_attribute_no: if relation_attribute_no == 0 {
None
Expand Down

0 comments on commit c597a22

Please sign in to comment.