Remove Binary/Text choice from ToSql

Text format doesn't work with our COPY implementation, and FromSql is
already binary only.
This commit is contained in:
Steven Fackler 2014-09-29 23:41:13 -07:00
parent 249db6b55a
commit 011d531b1d
2 changed files with 25 additions and 38 deletions

View File

@ -138,7 +138,7 @@ use message::{Bind,
Sync,
Terminate};
use message::{WriteMessage, ReadMessage};
use types::{Oid, PostgresType, ToSql, FromSql, PgUnknownType, Binary};
use types::{Oid, PostgresType, ToSql, FromSql, PgUnknownType};
#[macro_escape]
mod macros;
@ -1137,23 +1137,19 @@ impl<'conn> PostgresStatement<'conn> {
actual: params.len(),
});
}
let mut formats = vec![];
let mut values = vec![];
for (param, ty) in params.iter().zip(self.param_types.iter()) {
let (format, value) = try!(param.to_sql(ty));
formats.push(format as i16);
let value = try!(param.to_sql(ty));
values.push(value);
};
let result_formats = Vec::from_elem(self.result_desc.len(), Binary as i16);
try_pg!(conn.write_messages([
Bind {
portal: portal_name,
statement: self.name.as_slice(),
formats: formats.as_slice(),
formats: [1],
values: values.as_slice(),
result_formats: result_formats.as_slice()
result_formats: [1]
},
Execute {
portal: portal_name,
@ -1623,10 +1619,10 @@ impl<'a> PostgresCopyInStatement<'a> {
match (row.next(), types.next()) {
(Some(val), Some(ty)) => {
match try!(val.to_sql(ty)) {
(_, None) => {
None => {
let _ = buf.write_be_i32(-1);
}
(_, Some(val)) => {
Some(val) => {
let _ = buf.write_be_i32(val.len() as i32);
let _ = buf.write(val.as_slice());
}

View File

@ -198,14 +198,6 @@ make_postgres_type!(
INT8RANGEARRAYOID => PgInt8RangeArray member PgInt8Range
)
/// The wire format of a Postgres value
pub enum Format {
/// A user-readable string format
Text = 0,
/// A machine-readable binary format
Binary = 1
}
macro_rules! check_types(
($($expected:pat)|+, $actual:ident) => (
match $actual {
@ -497,10 +489,9 @@ impl FromSql for HashMap<String, Option<String>> {
/// A trait for types that can be converted into Postgres values
pub trait ToSql {
/// Converts the value of `self` into a format appropriate for the Postgres
/// backend.
fn to_sql(&self, ty: &PostgresType)
-> PostgresResult<(Format, Option<Vec<u8>>)>;
/// Converts the value of `self` into the binary format appropriate for the
/// Postgres backend.
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>>;
}
#[doc(hidden)]
@ -612,11 +603,11 @@ impl RawToSql for Json {
macro_rules! to_option_impl(
($($oid:pat)|+, $t:ty) => (
impl ToSql for Option<$t> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
check_types!($($oid)|+, ty)
match *self {
None => Ok((Text, None)),
None => Ok(None),
Some(ref val) => val.to_sql(ty)
}
}
@ -627,11 +618,11 @@ macro_rules! to_option_impl(
macro_rules! to_option_impl_lifetime(
($($oid:pat)|+, $t:ty) => (
impl<'a> ToSql for Option<$t> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
check_types!($($oid)|+, ty)
match *self {
None => Ok((Text, None)),
None => Ok(None),
Some(ref val) => val.to_sql(ty)
}
}
@ -642,12 +633,12 @@ macro_rules! to_option_impl_lifetime(
macro_rules! to_raw_to_impl(
($($oid:ident)|+, $t:ty) => (
impl ToSql for $t {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
check_types!($($oid)|+, ty)
let mut writer = MemWriter::new();
try!(self.raw_to_sql(&mut writer));
Ok((Binary, Some(writer.unwrap())))
Ok(Some(writer.unwrap()))
}
}
@ -670,18 +661,18 @@ to_raw_to_impl!(PgInt8Range, Range<i64>)
to_raw_to_impl!(PgTsRange | PgTstzRange, Range<Timespec>)
impl<'a> ToSql for &'a str {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
check_types!(PgVarchar | PgText | PgCharN | PgName, ty)
Ok((Text, Some(self.as_bytes().to_vec())))
Ok(Some(self.as_bytes().to_vec()))
}
}
to_option_impl_lifetime!(PgVarchar | PgText | PgCharN | PgName, &'a str)
impl<'a> ToSql for &'a [u8] {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
check_types!(PgByteA, ty)
Ok((Binary, Some(self.to_vec())))
Ok(Some(self.to_vec()))
}
}
@ -692,7 +683,7 @@ to_raw_to_impl!(PgTimestamp | PgTimestampTZ, Timespec)
macro_rules! to_array_impl(
($($oid:ident)|+, $t:ty) => (
impl ToSql for ArrayBase<Option<$t>> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
check_types!($($oid)|+, ty)
let mut buf = MemWriter::new();
@ -718,7 +709,7 @@ macro_rules! to_array_impl(
}
}
Ok((Binary, Some(buf.unwrap())))
Ok(Some(buf.unwrap()))
}
}
@ -742,7 +733,7 @@ to_array_impl!(PgInt8RangeArray, Range<i64>)
to_array_impl!(PgJsonArray, Json)
impl ToSql for HashMap<String, Option<String>> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
match *ty {
PgUnknownType { name: ref name, .. } if "hstore" == name.as_slice() => {}
_ => return Err(PgWrongType(ty.clone()))
@ -765,12 +756,12 @@ impl ToSql for HashMap<String, Option<String>> {
}
}
Ok((Binary, Some(buf.unwrap())))
Ok(Some(buf.unwrap()))
}
}
impl ToSql for Option<HashMap<String, Option<String>>> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<(Format, Option<Vec<u8>>)> {
fn to_sql(&self, ty: &PostgresType) -> PostgresResult<Option<Vec<u8>>> {
match *ty {
PgUnknownType { name: ref name, .. } if "hstore" == name.as_slice() => {}
_ => return Err(PgWrongType(ty.clone()))
@ -778,7 +769,7 @@ impl ToSql for Option<HashMap<String, Option<String>>> {
match *self {
Some(ref inner) => inner.to_sql(ty),
None => Ok((Binary, None))
None => Ok(None)
}
}
}