Skip to content

Commit 91b2187

Browse files
authored
Merge pull request #937 from NAlexPear/encode_format_types
Encode Format based on Type
2 parents 8f955eb + 8158eed commit 91b2187

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,29 @@ where
156156
I: IntoIterator<Item = P>,
157157
I::IntoIter: ExactSizeIterator,
158158
{
159-
let (param_formats, params): (Vec<_>, Vec<_>) = params
160-
.into_iter()
161-
.map(|p| (p.borrow_to_sql().encode_format() as i16, p))
162-
.unzip();
159+
let param_types = statement.params();
163160
let params = params.into_iter();
164161

165162
assert!(
166-
statement.params().len() == params.len(),
163+
param_types.len() == params.len(),
167164
"expected {} parameters but got {}",
168-
statement.params().len(),
165+
param_types.len(),
169166
params.len()
170167
);
171168

169+
let (param_formats, params): (Vec<_>, Vec<_>) = params
170+
.zip(param_types.iter())
171+
.map(|(p, ty)| (p.borrow_to_sql().encode_format(ty) as i16, p))
172+
.unzip();
173+
174+
let params = params.into_iter();
175+
172176
let mut error_idx = 0;
173177
let r = frontend::bind(
174178
portal,
175179
statement.name(),
176180
param_formats,
177-
params.zip(statement.params()).enumerate(),
181+
params.zip(param_types).enumerate(),
178182
|(idx, (param, ty)), buf| match param.borrow_to_sql().to_sql_checked(ty, buf) {
179183
Ok(IsNull::No) => Ok(postgres_protocol::IsNull::No),
180184
Ok(IsNull::Yes) => Ok(postgres_protocol::IsNull::Yes),

0 commit comments

Comments
 (0)