Skip to content

Commit

Permalink
tokio-postgres: Expose table OID and column ID on Column
Browse files Browse the repository at this point in the history
Pass through the table OID and column ID from field descriptions for
prepared statements to fields on Column, with exposed getters
  • Loading branch information
glittershark committed Aug 22, 2023
1 parent 5302d7e commit d64b29c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 6 additions & 1 deletion tokio-postgres/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ pub async fn prepare(
let mut it = row_description.fields();
while let Some(field) = it.next().map_err(Error::parse)? {
let type_ = get_type(client, field.type_oid()).await?;
let column = Column::new(field.name().to_string(), type_);
let column = Column::new(
field.name().to_string(),
type_,
Some(field.table_oid()).filter(|oid| *oid != 0),
Some(field.column_id()).filter(|id| *id != 0),
);
columns.push(column);
}
}
Expand Down
27 changes: 25 additions & 2 deletions tokio-postgres/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,23 @@ impl Statement {
pub struct Column {
name: String,
type_: Type,
table_oid: Option<u32>,
column_id: Option<i16>,
}

impl Column {
pub(crate) fn new(name: String, type_: Type) -> Column {
Column { name, type_ }
pub(crate) fn new(
name: String,
type_: Type,
table_oid: Option<u32>,
column_id: Option<i16>,
) -> Column {
Column {
name,
type_,
table_oid,
column_id,
}
}

/// Returns the name of the column.
Expand All @@ -84,6 +96,17 @@ impl Column {
pub fn type_(&self) -> &Type {
&self.type_
}

/// Returns the oid of the column's table, if it is a direct alias to a column in a table
pub fn table_oid(&self) -> Option<u32> {
self.table_oid
}

/// Returns the column's attribute number in its table, if it is a direct alias to a column in a
/// table
pub fn column_id(&self) -> Option<i16> {
self.column_id
}
}

impl fmt::Debug for Column {
Expand Down

0 comments on commit d64b29c

Please sign in to comment.