Skip to content

Commit 011d531

Browse files
committed
Remove Binary/Text choice from ToSql
Text format doesn't work with our COPY implementation, and FromSql is already binary only.
1 parent 249db6b commit 011d531

2 files changed

Lines changed: 25 additions & 38 deletions

File tree

src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ use message::{Bind,
138138
Sync,
139139
Terminate};
140140
use message::{WriteMessage, ReadMessage};
141-
use types::{Oid, PostgresType, ToSql, FromSql, PgUnknownType, Binary};
141+
use types::{Oid, PostgresType, ToSql, FromSql, PgUnknownType};
142142

143143
#[macro_escape]
144144
mod macros;
@@ -1137,23 +1137,19 @@ impl<'conn> PostgresStatement<'conn> {
11371137
actual: params.len(),
11381138
});
11391139
}
1140-
let mut formats = vec![];
11411140
let mut values = vec![];
11421141
for (param, ty) in params.iter().zip(self.param_types.iter()) {
1143-
let (format, value) = try!(param.to_sql(ty));
1144-
formats.push(format as i16);
1142+
let value = try!(param.to_sql(ty));
11451143
values.push(value);
11461144
};
11471145

1148-
let result_formats = Vec::from_elem(self.result_desc.len(), Binary as i16);
1149-
11501146
try_pg!(conn.write_messages([
11511147
Bind {
11521148
portal: portal_name,
11531149
statement: self.name.as_slice(),
1154-
formats: formats.as_slice(),
1150+
formats: [1],
11551151
values: values.as_slice(),
1156-
result_formats: result_formats.as_slice()
1152+
result_formats: [1]
11571153
},
11581154
Execute {
11591155
portal: portal_name,
@@ -1623,10 +1619,10 @@ impl<'a> PostgresCopyInStatement<'a> {
16231619
match (row.next(), types.next()) {
16241620
(Some(val), Some(ty)) => {
16251621
match try!(val.to_sql(ty)) {
1626-
(_, None) => {
1622+
None => {
16271623
let _ = buf.write_be_i32(-1);
16281624
}
1629-
(_, Some(val)) => {
1625+
Some(val) => {
16301626
let _ = buf.write_be_i32(val.len() as i32);
16311627
let _ = buf.write(val.as_slice());
16321628
}

src/types/mod.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,6 @@ make_postgres_type!(
198198
INT8RANGEARRAYOID => PgInt8RangeArray member PgInt8Range
199199
)
200200

201-
/// The wire format of a Postgres value
202-
pub enum Format {
203-
/// A user-readable string format
204-
Text = 0,
205-
/// A machine-readable binary format
206-
Binary = 1
207-
}
208-
209201
macro_rules! check_types(
210202
($($expected:pat)|+, $actual:ident) => (
211203
match $actual {
@@ -497,10 +489,9 @@ impl FromSql for HashMap<String, Option<String>> {
497489

498490
/// A trait for types that can be converted into Postgres values
499491
pub trait ToSql {
500-
/// Converts the value of `self` into a format appropriate for the Postgres
501-
/// backend.
502-
fn to_sql(&self, ty: &PostgresType)
503-
-> PostgresResult<(Format, Option<Vec<u8>>)>;
492+
/// Converts the value of `self` into the binary format appropriate for the
493+
/// Postgres backend.
494+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>>;
504495
}
505496

506497
#[doc(hidden)]
@@ -612,11 +603,11 @@ impl RawToSql for Json {
612603
macro_rules! to_option_impl(
613604
($($oid:pat)|+, $t:ty) => (
614605
impl ToSql for Option<$t> {
615-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
606+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
616607
check_types!($($oid)|+, ty)
617608

618609
match *self {
619-
None => Ok((Text, None)),
610+
None => Ok(None),
620611
Some(ref val) => val.to_sql(ty)
621612
}
622613
}
@@ -627,11 +618,11 @@ macro_rules! to_option_impl(
627618
macro_rules! to_option_impl_lifetime(
628619
($($oid:pat)|+, $t:ty) => (
629620
impl<'a> ToSql for Option<$t> {
630-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
621+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
631622
check_types!($($oid)|+, ty)
632623

633624
match *self {
634-
None => Ok((Text, None)),
625+
None => Ok(None),
635626
Some(ref val) => val.to_sql(ty)
636627
}
637628
}
@@ -642,12 +633,12 @@ macro_rules! to_option_impl_lifetime(
642633
macro_rules! to_raw_to_impl(
643634
($($oid:ident)|+, $t:ty) => (
644635
impl ToSql for $t {
645-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
636+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
646637
check_types!($($oid)|+, ty)
647638

648639
let mut writer = MemWriter::new();
649640
try!(self.raw_to_sql(&mut writer));
650-
Ok((Binary, Some(writer.unwrap())))
641+
Ok(Some(writer.unwrap()))
651642
}
652643
}
653644

@@ -670,18 +661,18 @@ to_raw_to_impl!(PgInt8Range, Range<i64>)
670661
to_raw_to_impl!(PgTsRange | PgTstzRange, Range<Timespec>)
671662

672663
impl<'a> ToSql for &'a str {
673-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
664+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
674665
check_types!(PgVarchar | PgText | PgCharN | PgName, ty)
675-
Ok((Text, Some(self.as_bytes().to_vec())))
666+
Ok(Some(self.as_bytes().to_vec()))
676667
}
677668
}
678669

679670
to_option_impl_lifetime!(PgVarchar | PgText | PgCharN | PgName, &'a str)
680671

681672
impl<'a> ToSql for &'a [u8] {
682-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
673+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
683674
check_types!(PgByteA, ty)
684-
Ok((Binary, Some(self.to_vec())))
675+
Ok(Some(self.to_vec()))
685676
}
686677
}
687678

@@ -692,7 +683,7 @@ to_raw_to_impl!(PgTimestamp | PgTimestampTZ, Timespec)
692683
macro_rules! to_array_impl(
693684
($($oid:ident)|+, $t:ty) => (
694685
impl ToSql for ArrayBase<Option<$t>> {
695-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
686+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
696687
check_types!($($oid)|+, ty)
697688
let mut buf = MemWriter::new();
698689

@@ -718,7 +709,7 @@ macro_rules! to_array_impl(
718709
}
719710
}
720711

721-
Ok((Binary, Some(buf.unwrap())))
712+
Ok(Some(buf.unwrap()))
722713
}
723714
}
724715

@@ -742,7 +733,7 @@ to_array_impl!(PgInt8RangeArray, Range<i64>)
742733
to_array_impl!(PgJsonArray, Json)
743734

744735
impl ToSql for HashMap<String, Option<String>> {
745-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
736+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
746737
match *ty {
747738
PgUnknownType { name: ref name, .. } if "hstore" == name.as_slice() => {}
748739
_ => return Err(PgWrongType(ty.clone()))
@@ -765,20 +756,20 @@ impl ToSql for HashMap<String, Option<String>> {
765756
}
766757
}
767758

768-
Ok((Binary, Some(buf.unwrap())))
759+
Ok(Some(buf.unwrap()))
769760
}
770761
}
771762

772763
impl ToSql for Option<HashMap<String, Option<String>>> {
773-
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
764+
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
774765
match *ty {
775766
PgUnknownType { name: ref name, .. } if "hstore" == name.as_slice() => {}
776767
_ => return Err(PgWrongType(ty.clone()))
777768
}
778769

779770
match *self {
780771
Some(ref inner) => inner.to_sql(ty),
781-
None => Ok((Binary, None))
772+
None => Ok(None)
782773
}
783774
}
784775
}

0 commit comments

Comments
 (0)