Skip to content

Commit c597a22

Browse files
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 <austin.bonander@gmail.com> * Update sqlx-postgres/src/column.rs Co-authored-by: Austin Bonander <austin.bonander@gmail.com> * Update sqlx-postgres/src/message/row_description.rs Co-authored-by: Austin Bonander <austin.bonander@gmail.com> * Update sqlx-postgres/src/column.rs Co-authored-by: Austin Bonander <austin.bonander@gmail.com> * Update sqlx-postgres/src/message/row_description.rs Co-authored-by: Austin Bonander <austin.bonander@gmail.com> * fix --------- Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
1 parent 10bec32 commit c597a22

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

sqlx-postgres/src/column.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,33 @@ pub struct PgColumn {
1010
pub(crate) name: UStr,
1111
pub(crate) type_info: PgTypeInfo,
1212
#[cfg_attr(feature = "offline", serde(skip))]
13-
pub(crate) relation_id: Option<i32>,
13+
pub(crate) relation_id: Option<crate::types::Oid>,
1414
#[cfg_attr(feature = "offline", serde(skip))]
1515
pub(crate) relation_attribute_no: Option<i16>,
1616
}
1717

18+
impl PgColumn {
19+
/// Returns the OID of the table this column is from, if applicable.
20+
///
21+
/// This will be `None` if the column is the result of an expression.
22+
///
23+
/// Corresponds to column `attrelid` of the `pg_catalog.pg_attribute` table:
24+
/// <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
25+
pub fn relation_id(&self) -> Option<crate::types::Oid> {
26+
self.relation_id
27+
}
28+
29+
/// Returns the 1-based index of this column in its parent table, if applicable.
30+
///
31+
/// This will be `None` if the column is the result of an expression.
32+
///
33+
/// Corresponds to column `attnum` of the `pg_catalog.pg_attribute` table:
34+
/// <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
35+
pub fn relation_attribute_no(&self) -> Option<i16> {
36+
self.relation_attribute_no
37+
}
38+
}
39+
1840
impl Column for PgColumn {
1941
type Database = Postgres;
2042

sqlx-postgres/src/message/row_description.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Field {
1717

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

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

68-
let relation_id = buf.get_i32();
68+
let relation_id = buf.get_u32();
6969
let relation_attribute_no = buf.get_i16();
7070
let data_type_id = Oid(buf.get_u32());
7171
let data_type_size = buf.get_i16();
@@ -77,7 +77,7 @@ impl BackendMessage for RowDescription {
7777
relation_id: if relation_id == 0 {
7878
None
7979
} else {
80-
Some(relation_id)
80+
Some(Oid(relation_id))
8181
},
8282
relation_attribute_no: if relation_attribute_no == 0 {
8383
None

0 commit comments

Comments
 (0)