Skip to content

Commit

Permalink
Merge pull request sfackler#937 from NAlexPear/encode_format_types
Browse files Browse the repository at this point in the history
Encode Format based on Type
  • Loading branch information
sfackler authored Aug 16, 2022
2 parents 8f955eb + 8158eed commit 91b2187
Showing 2 changed files with 16 additions and 12 deletions.
10 changes: 5 additions & 5 deletions postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -836,7 +836,7 @@ pub trait ToSql: fmt::Debug {
) -> Result<IsNull, Box<dyn Error + Sync + Send>>;

/// 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<T: ToSql> ToSql for Option<T> {
<T as ToSql>::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,
}
}
18 changes: 11 additions & 7 deletions tokio-postgres/src/query.rs
Original file line number Diff line number Diff line change
@@ -156,25 +156,29 @@ where
I: IntoIterator<Item = P>,
I::IntoIter: ExactSizeIterator,
{
let (param_formats, params): (Vec<_>, Vec<_>) = params
.into_iter()
.map(|p| (p.borrow_to_sql().encode_format() as i16, p))
.unzip();
let param_types = statement.params();
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()
);

let (param_formats, params): (Vec<_>, Vec<_>) = params
.zip(param_types.iter())
.map(|(p, ty)| (p.borrow_to_sql().encode_format(ty) as i16, p))
.unzip();

let params = params.into_iter();

let mut error_idx = 0;
let r = frontend::bind(
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),

0 comments on commit 91b2187

Please sign in to comment.