Skip to content

Commit 09e8656

Browse files
authored
Merge pull request sfackler#836 from subzerocloud/master
Implement Unknown encoding for query parameters
2 parents 8736f45 + 6838688 commit 09e8656

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

postgres-types/src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,32 @@ pub trait ToSql: fmt::Debug {
834834
ty: &Type,
835835
out: &mut BytesMut,
836836
) -> Result<IsNull, Box<dyn Error + Sync + Send>>;
837+
838+
/// Specify the encode format
839+
fn encode_format(&self) -> Format { Format::Binary }
837840
}
838841

842+
/// Supported Postgres message format types
843+
///
844+
/// Using Text format in a message assumes a Postgres `SERVER_ENCODING` of `UTF8`
845+
pub enum Format {
846+
/// Text format (UTF-8)
847+
Text,
848+
/// Compact, typed binary format
849+
Binary,
850+
}
851+
852+
/// Convert from `Format` to the Postgres integer representation of those formats
853+
impl From<Format> for i16 {
854+
fn from(format: Format) -> Self {
855+
match format {
856+
Format::Text => 0,
857+
Format::Binary => 1,
858+
}
859+
}
860+
}
861+
862+
839863
impl<'a, T> ToSql for &'a T
840864
where
841865
T: ToSql,

tokio-postgres/src/query.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ where
156156
I: IntoIterator<Item = P>,
157157
I::IntoIter: ExactSizeIterator,
158158
{
159+
let (param_formats, params):(Vec<_>, Vec<_>) = params.into_iter().map(|p|->(i16, P){(p.borrow_to_sql().encode_format().into(),p)}).unzip();
159160
let params = params.into_iter();
160161

161162
assert!(
@@ -169,7 +170,7 @@ where
169170
let r = frontend::bind(
170171
portal,
171172
statement.name(),
172-
Some(1),
173+
param_formats,
173174
params.zip(statement.params()).enumerate(),
174175
|(idx, (param, ty)), buf| match param.borrow_to_sql().to_sql_checked(ty, buf) {
175176
Ok(IsNull::No) => Ok(postgres_protocol::IsNull::No),

0 commit comments

Comments
 (0)