From c597a225c577a4c50604fd264ab580e0588c0f11 Mon Sep 17 00:00:00 2001 From: Kurt Wolf Date: Sat, 7 Sep 2024 18:43:03 -0700 Subject: [PATCH] feat: expose relation_id and relation_attribution_no on PgColumn (#3492) * expose relation_id and relation_attribution_no on PgColumn * Update sqlx-postgres/src/message/row_description.rs Co-authored-by: Austin Bonander * Update sqlx-postgres/src/column.rs Co-authored-by: Austin Bonander * Update sqlx-postgres/src/message/row_description.rs Co-authored-by: Austin Bonander * Update sqlx-postgres/src/column.rs Co-authored-by: Austin Bonander * Update sqlx-postgres/src/message/row_description.rs Co-authored-by: Austin Bonander * fix --------- Co-authored-by: Austin Bonander --- sqlx-postgres/src/column.rs | 24 +++++++++++++++++++- sqlx-postgres/src/message/row_description.rs | 6 ++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/sqlx-postgres/src/column.rs b/sqlx-postgres/src/column.rs index cc2d259a..a838c27b 100644 --- a/sqlx-postgres/src/column.rs +++ b/sqlx-postgres/src/column.rs @@ -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, + pub(crate) relation_id: Option, #[cfg_attr(feature = "offline", serde(skip))] pub(crate) relation_attribute_no: Option, } +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: + /// + pub fn relation_id(&self) -> Option { + 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: + /// + pub fn relation_attribute_no(&self) -> Option { + self.relation_attribute_no + } +} + impl Column for PgColumn { type Database = Postgres; diff --git a/sqlx-postgres/src/message/row_description.rs b/sqlx-postgres/src/message/row_description.rs index 3f3155ed..668f31ed 100644 --- a/sqlx-postgres/src/message/row_description.rs +++ b/sqlx-postgres/src/message/row_description.rs @@ -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, + pub relation_id: Option, /// If the field can be identified as a column of a specific table, the attribute number of /// the column; otherwise zero. @@ -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(); @@ -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