Skip to content

Commit 6838688

Browse files
committed
remove "Unknown" and add Format enum
1 parent 08b7c65 commit 6838688

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

postgres-types/src/lib.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -771,35 +771,30 @@ pub trait ToSql: fmt::Debug {
771771
) -> Result<IsNull, Box<dyn Error + Sync + Send>>;
772772

773773
/// Specify the encode format
774-
fn encode_format(&self) -> i16 { 1 }
774+
fn encode_format(&self) -> Format { Format::Binary }
775775
}
776776

777+
/// Supported Postgres message format types
778+
///
779+
/// Using Text format in a message assumes a Postgres `SERVER_ENCODING` of `UTF8`
780+
pub enum Format {
781+
/// Text format (UTF-8)
782+
Text,
783+
/// Compact, typed binary format
784+
Binary,
785+
}
777786

778-
/// A Wrapper type used for sending query parameters encoded as unknown.
779-
#[derive(Debug)]
780-
pub struct Unknown<'a>(pub &'a str);
781-
782-
impl ToSql for Unknown<'_> {
783-
fn to_sql(
784-
&self,
785-
_ty: &Type,
786-
out: &mut BytesMut,
787-
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
788-
match *self {
789-
Unknown(val) => {
790-
types::text_to_sql(val, out);
791-
Ok(IsNull::No)
792-
}
787+
/// Convert from `Format` to the Postgres integer representation of those formats
788+
impl From<Format> for i16 {
789+
fn from(format: Format) -> Self {
790+
match format {
791+
Format::Text => 0,
792+
Format::Binary => 1,
793793
}
794794
}
795-
796-
fn accepts(_ty: &Type) -> bool { true }
797-
798-
fn encode_format(&self) -> i16 { 0 }
799-
800-
to_sql_checked!();
801795
}
802796

797+
803798
impl<'a, T> ToSql for &'a T
804799
where
805800
T: ToSql,

tokio-postgres/src/query.rs

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

163162
assert!(

0 commit comments

Comments
 (0)