Merge pull request #836 from subzerocloud/master

Implement Unknown encoding for query parameters
This commit is contained in:
Steven Fackler 2022-07-18 20:16:40 -04:00 committed by GitHub
commit 09e86560dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -834,8 +834,32 @@ pub trait ToSql: fmt::Debug {
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>;
/// Specify the encode format
fn encode_format(&self) -> Format { Format::Binary }
}
/// Supported Postgres message format types
///
/// Using Text format in a message assumes a Postgres `SERVER_ENCODING` of `UTF8`
pub enum Format {
/// Text format (UTF-8)
Text,
/// Compact, typed binary format
Binary,
}
/// Convert from `Format` to the Postgres integer representation of those formats
impl From<Format> for i16 {
fn from(format: Format) -> Self {
match format {
Format::Text => 0,
Format::Binary => 1,
}
}
}
impl<'a, T> ToSql for &'a T
where
T: ToSql,

View File

@ -156,6 +156,7 @@ where
I: IntoIterator<Item = P>,
I::IntoIter: ExactSizeIterator,
{
let (param_formats, params):(Vec<_>, Vec<_>) = params.into_iter().map(|p|->(i16, P){(p.borrow_to_sql().encode_format().into(),p)}).unzip();
let params = params.into_iter();
assert!(
@ -169,7 +170,7 @@ where
let r = frontend::bind(
portal,
statement.name(),
Some(1),
param_formats,
params.zip(statement.params()).enumerate(),
|(idx, (param, ty)), buf| match param.borrow_to_sql().to_sql_checked(ty, buf) {
Ok(IsNull::No) => Ok(postgres_protocol::IsNull::No),