diff --git a/postgres-types/src/lib.rs b/postgres-types/src/lib.rs index 463e3dee..5c5a0d02 100644 --- a/postgres-types/src/lib.rs +++ b/postgres-types/src/lib.rs @@ -836,7 +836,7 @@ pub trait ToSql: fmt::Debug { ) -> Result>; /// Specify the encode format - fn encode_format(&self) -> Format { + fn encode_format(&self, _ty: &Type) -> Format { Format::Binary } } @@ -868,8 +868,8 @@ where T::accepts(ty) } - fn encode_format(&self) -> Format { - (*self).encode_format() + fn encode_format(&self, ty: &Type) -> Format { + (*self).encode_format(ty) } to_sql_checked!(); @@ -891,9 +891,9 @@ impl ToSql for Option { ::accepts(ty) } - fn encode_format(&self) -> Format { + fn encode_format(&self, ty: &Type) -> Format { match self { - Some(ref val) => val.encode_format(), + Some(ref val) => val.encode_format(ty), None => Format::Binary, } } diff --git a/tokio-postgres/src/query.rs b/tokio-postgres/src/query.rs index f1593dbc..5f8cbf7c 100644 --- a/tokio-postgres/src/query.rs +++ b/tokio-postgres/src/query.rs @@ -156,16 +156,18 @@ where I: IntoIterator, I::IntoIter: ExactSizeIterator, { + let param_types = statement.params(); let (param_formats, params): (Vec<_>, Vec<_>) = params .into_iter() - .map(|p| (p.borrow_to_sql().encode_format() as i16, p)) + .zip(param_types.iter()) + .map(|(p, ty)| (p.borrow_to_sql().encode_format(ty) as i16, p)) .unzip(); let params = params.into_iter(); assert!( - statement.params().len() == params.len(), + param_types.len() == params.len(), "expected {} parameters but got {}", - statement.params().len(), + param_types.len(), params.len() ); @@ -174,7 +176,7 @@ where portal, statement.name(), param_formats, - params.zip(statement.params()).enumerate(), + params.zip(param_types).enumerate(), |(idx, (param, ty)), buf| match param.borrow_to_sql().to_sql_checked(ty, buf) { Ok(IsNull::No) => Ok(postgres_protocol::IsNull::No), Ok(IsNull::Yes) => Ok(postgres_protocol::IsNull::Yes),