Skip to content

Commit 569689d

Browse files
committed
encode format with types
1 parent 8f955eb commit 569689d

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

postgres-types/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ pub trait ToSql: fmt::Debug {
836836
) -> Result<IsNull, Box<dyn Error + Sync + Send>>;
837837

838838
/// Specify the encode format
839-
fn encode_format(&self) -> Format {
839+
fn encode_format(&self, _ty: &Type) -> Format {
840840
Format::Binary
841841
}
842842
}
@@ -868,8 +868,8 @@ where
868868
T::accepts(ty)
869869
}
870870

871-
fn encode_format(&self) -> Format {
872-
(*self).encode_format()
871+
fn encode_format(&self, ty: &Type) -> Format {
872+
(*self).encode_format(ty)
873873
}
874874

875875
to_sql_checked!();
@@ -891,9 +891,9 @@ impl<T: ToSql> ToSql for Option<T> {
891891
<T as ToSql>::accepts(ty)
892892
}
893893

894-
fn encode_format(&self) -> Format {
894+
fn encode_format(&self, ty: &Type) -> Format {
895895
match self {
896-
Some(ref val) => val.encode_format(),
896+
Some(ref val) => val.encode_format(ty),
897897
None => Format::Binary,
898898
}
899899
}

tokio-postgres/src/query.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,18 @@ where
156156
I: IntoIterator<Item = P>,
157157
I::IntoIter: ExactSizeIterator,
158158
{
159+
let param_types = statement.params();
159160
let (param_formats, params): (Vec<_>, Vec<_>) = params
160161
.into_iter()
161-
.map(|p| (p.borrow_to_sql().encode_format() as i16, p))
162+
.zip(param_types.iter())
163+
.map(|(p, ty)| (p.borrow_to_sql().encode_format(ty) as i16, p))
162164
.unzip();
163165
let params = params.into_iter();
164166

165167
assert!(
166-
statement.params().len() == params.len(),
168+
param_types.len() == params.len(),
167169
"expected {} parameters but got {}",
168-
statement.params().len(),
170+
param_types.len(),
169171
params.len()
170172
);
171173

@@ -174,7 +176,7 @@ where
174176
portal,
175177
statement.name(),
176178
param_formats,
177-
params.zip(statement.params()).enumerate(),
179+
params.zip(param_types).enumerate(),
178180
|(idx, (param, ty)), buf| match param.borrow_to_sql().to_sql_checked(ty, buf) {
179181
Ok(IsNull::No) => Ok(postgres_protocol::IsNull::No),
180182
Ok(IsNull::Yes) => Ok(postgres_protocol::IsNull::Yes),

0 commit comments

Comments
 (0)