Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose type modifier in Column struct #1145

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio-postgres/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub async fn prepare(
name: field.name().to_string(),
table_oid: Some(field.table_oid()).filter(|n| *n != 0),
column_id: Some(field.column_id()).filter(|n| *n != 0),
type_modifier: field.type_modifier(),
r#type: type_,
};
columns.push(column);
Expand Down
6 changes: 6 additions & 0 deletions tokio-postgres/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct Column {
pub(crate) name: String,
pub(crate) table_oid: Option<u32>,
pub(crate) column_id: Option<i16>,
pub(crate) type_modifier: i32,
pub(crate) r#type: Type,
}

Expand All @@ -86,6 +87,11 @@ impl Column {
self.column_id
}

/// Return the type modifier
pub fn type_modifier(&self) -> i32 {
self.type_modifier
}

/// Returns the type of the column.
pub fn type_(&self) -> &Type {
&self.r#type
Expand Down
24 changes: 24 additions & 0 deletions tokio-postgres/tests/test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@ async fn pipelined_prepare() {
assert_eq!(statement2.columns()[0].type_(), &Type::INT8);
}

#[tokio::test]
async fn prepare_type_modifier() {
let client = connect("user=postgres").await;

let statement = client
.prepare("SELECT $1::BIGINT, $2::VARCHAR(7), $3::VARCHAR(101)")
.await
.unwrap();

let varlena_header_length = 4;
assert_eq!(statement.columns()[0].type_(), &Type::INT8);
assert_eq!(statement.columns()[0].type_modifier(), -1);
assert_eq!(statement.columns()[1].type_(), &Type::VARCHAR);
assert_eq!(
statement.columns()[1].type_modifier(),
7 + varlena_header_length
);
assert_eq!(statement.columns()[2].type_(), &Type::VARCHAR);
assert_eq!(
statement.columns()[2].type_modifier(),
101 + varlena_header_length
);
}

#[tokio::test]
async fn insert_select() {
let client = connect("user=postgres").await;
Expand Down