diff --git a/tokio-postgres/src/prepare.rs b/tokio-postgres/src/prepare.rs index 07fb45694..c0596e113 100644 --- a/tokio-postgres/src/prepare.rs +++ b/tokio-postgres/src/prepare.rs @@ -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); diff --git a/tokio-postgres/src/statement.rs b/tokio-postgres/src/statement.rs index c5d657738..ae98691dd 100644 --- a/tokio-postgres/src/statement.rs +++ b/tokio-postgres/src/statement.rs @@ -67,6 +67,7 @@ pub struct Column { pub(crate) name: String, pub(crate) table_oid: Option, pub(crate) column_id: Option, + pub(crate) type_modifier: i32, pub(crate) r#type: Type, } @@ -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 diff --git a/tokio-postgres/tests/test/main.rs b/tokio-postgres/tests/test/main.rs index 737f46631..8841b657f 100644 --- a/tokio-postgres/tests/test/main.rs +++ b/tokio-postgres/tests/test/main.rs @@ -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;