Rustfmt
This commit is contained in:
parent
7322308a16
commit
42fa212482
37
src/lib.rs
37
src/lib.rs
@ -279,9 +279,7 @@ impl InnerConnection {
|
||||
options.push(("database".to_owned(), database));
|
||||
}
|
||||
|
||||
try!(conn.stream.write_message(&frontend::StartupMessage {
|
||||
parameters: &options,
|
||||
}));
|
||||
try!(conn.stream.write_message(&frontend::StartupMessage { parameters: &options }));
|
||||
try!(conn.stream.flush());
|
||||
|
||||
try!(conn.handle_auth(user));
|
||||
@ -337,7 +335,8 @@ impl InnerConnection {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_message_with_notification_nonblocking(&mut self) -> std::io::Result<Option<backend::Message>> {
|
||||
fn read_message_with_notification_nonblocking(&mut self)
|
||||
-> std::io::Result<Option<backend::Message>> {
|
||||
debug_assert!(!self.desynchronized);
|
||||
loop {
|
||||
match try_desync!(self, self.stream.read_message_nonblocking()) {
|
||||
@ -528,7 +527,8 @@ impl InnerConnection {
|
||||
let mut values = vec![];
|
||||
for (param, ty) in params.iter().zip(param_types) {
|
||||
let mut buf = vec![];
|
||||
match try!(param.to_sql_checked(ty, &mut buf, &SessionInfo::new(self)).map_err(Error::Conversion)) {
|
||||
match try!(param.to_sql_checked(ty, &mut buf, &SessionInfo::new(self))
|
||||
.map_err(Error::Conversion)) {
|
||||
IsNull::Yes => values.push(None),
|
||||
IsNull::No => values.push(Some(buf)),
|
||||
}
|
||||
@ -681,18 +681,16 @@ impl InnerConnection {
|
||||
Some(ref data) => {
|
||||
try!(Option::<Oid>::from_sql(&Type::Oid, &mut &**data, &ctx)
|
||||
.map_err(Error::Conversion))
|
||||
},
|
||||
}
|
||||
None => {
|
||||
try!(Option::<Oid>::from_sql_null(&Type::Oid, &ctx)
|
||||
.map_err(Error::Conversion))
|
||||
},
|
||||
try!(Option::<Oid>::from_sql_null(&Type::Oid, &ctx).map_err(Error::Conversion))
|
||||
}
|
||||
};
|
||||
let basetype = try!(Oid::from_sql(&Type::Oid, &mut &**row[4].as_ref().unwrap(), &ctx)
|
||||
.map_err(Error::Conversion));
|
||||
let schema = try!(String::from_sql(&Type::Name,
|
||||
&mut &**row[5].as_ref().unwrap(),
|
||||
&ctx)
|
||||
.map_err(Error::Conversion));
|
||||
let schema =
|
||||
try!(String::from_sql(&Type::Name, &mut &**row[5].as_ref().unwrap(), &ctx)
|
||||
.map_err(Error::Conversion));
|
||||
let relid = try!(Oid::from_sql(&Type::Oid, &mut &**row[6].as_ref().unwrap(), &ctx)
|
||||
.map_err(Error::Conversion));
|
||||
(name, type_, elem_oid, rngsubtype, basetype, schema, relid)
|
||||
@ -753,10 +751,8 @@ impl InnerConnection {
|
||||
let ctx = SessionInfo::new(self);
|
||||
let mut variants = vec![];
|
||||
for row in rows {
|
||||
variants.push(try!(String::from_sql(&Type::Name,
|
||||
&mut &**row[0].as_ref().unwrap(),
|
||||
&ctx)
|
||||
.map_err(Error::Conversion)));
|
||||
variants.push(try!(String::from_sql(&Type::Name, &mut &**row[0].as_ref().unwrap(), &ctx)
|
||||
.map_err(Error::Conversion)));
|
||||
}
|
||||
|
||||
Ok(variants)
|
||||
@ -789,10 +785,9 @@ impl InnerConnection {
|
||||
for row in rows {
|
||||
let (name, type_) = {
|
||||
let ctx = SessionInfo::new(self);
|
||||
let name = try!(String::from_sql(&Type::Name,
|
||||
&mut &**row[0].as_ref().unwrap(),
|
||||
&ctx)
|
||||
.map_err(Error::Conversion));
|
||||
let name =
|
||||
try!(String::from_sql(&Type::Name, &mut &**row[0].as_ref().unwrap(), &ctx)
|
||||
.map_err(Error::Conversion));
|
||||
let type_ = try!(Oid::from_sql(&Type::Oid, &mut &**row[1].as_ref().unwrap(), &ctx)
|
||||
.map_err(Error::Conversion));
|
||||
(name, type_)
|
||||
|
10
src/stmt.rs
10
src/stmt.rs
@ -365,7 +365,9 @@ impl<'conn> Statement<'conn> {
|
||||
try!(conn.raw_execute(&self.info.name, "", 0, self.param_types(), params));
|
||||
|
||||
let (format, column_formats) = match try!(conn.read_message()) {
|
||||
backend::Message::CopyOutResponse { format, column_formats } => (format, column_formats),
|
||||
backend::Message::CopyOutResponse { format, column_formats } => {
|
||||
(format, column_formats)
|
||||
}
|
||||
backend::Message::CopyInResponse { .. } => {
|
||||
try!(conn.stream.write_message(&frontend::CopyFail { message: "" }));
|
||||
try!(conn.stream.write_message(&frontend::CopyDone));
|
||||
@ -432,14 +434,16 @@ impl<'conn> Statement<'conn> {
|
||||
}
|
||||
backend::Message::ErrorResponse { fields } => {
|
||||
loop {
|
||||
if let backend::Message::ReadyForQuery { .. } = try!(info.conn.read_message()) {
|
||||
if let backend::Message::ReadyForQuery { .. } =
|
||||
try!(info.conn.read_message()) {
|
||||
return DbError::new(fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
loop {
|
||||
if let backend::Message::ReadyForQuery { .. } = try!(info.conn.read_message()) {
|
||||
if let backend::Message::ReadyForQuery { .. } =
|
||||
try!(info.conn.read_message()) {
|
||||
return Err(Error::Io(bad_response()));
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,11 @@ impl FromSql for BitVec {
|
||||
}
|
||||
|
||||
impl ToSql for BitVec {
|
||||
fn to_sql(&self, _: &Type, mut out: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
mut out: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
try!(types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out));
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
|
@ -12,7 +12,10 @@ fn base() -> NaiveDateTime {
|
||||
}
|
||||
|
||||
impl FromSql for NaiveDateTime {
|
||||
fn from_sql(_: &Type, raw: &[u8], _: &SessionInfo) -> Result<NaiveDateTime, Box<Error + Sync + Send>> {
|
||||
fn from_sql(_: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<NaiveDateTime, Box<Error + Sync + Send>> {
|
||||
let t = try!(types::timestamp_from_sql(raw));
|
||||
Ok(base() + Duration::microseconds(t))
|
||||
}
|
||||
@ -21,7 +24,11 @@ impl FromSql for NaiveDateTime {
|
||||
}
|
||||
|
||||
impl ToSql for NaiveDateTime {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let time = match (*self - base()).num_microseconds() {
|
||||
Some(time) => time,
|
||||
None => return Err("value too large to transmit".into()),
|
||||
@ -35,7 +42,10 @@ impl ToSql for NaiveDateTime {
|
||||
}
|
||||
|
||||
impl FromSql for DateTime<UTC> {
|
||||
fn from_sql(type_: &Type, raw: &[u8], info: &SessionInfo) -> Result<DateTime<UTC>, Box<Error + Sync + Send>> {
|
||||
fn from_sql(type_: &Type,
|
||||
raw: &[u8],
|
||||
info: &SessionInfo)
|
||||
-> Result<DateTime<UTC>, Box<Error + Sync + Send>> {
|
||||
let naive = try!(NaiveDateTime::from_sql(type_, raw, info));
|
||||
Ok(DateTime::from_utc(naive, UTC))
|
||||
}
|
||||
@ -44,7 +54,11 @@ impl FromSql for DateTime<UTC> {
|
||||
}
|
||||
|
||||
impl ToSql for DateTime<UTC> {
|
||||
fn to_sql(&self, type_: &Type, w: &mut Vec<u8>, info: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
type_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
info: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
self.naive_utc().to_sql(type_, w, info)
|
||||
}
|
||||
|
||||
@ -53,7 +67,10 @@ impl ToSql for DateTime<UTC> {
|
||||
}
|
||||
|
||||
impl FromSql for DateTime<Local> {
|
||||
fn from_sql(type_: &Type, raw: &[u8], info: &SessionInfo) -> Result<DateTime<Local>, Box<Error + Sync + Send>> {
|
||||
fn from_sql(type_: &Type,
|
||||
raw: &[u8],
|
||||
info: &SessionInfo)
|
||||
-> Result<DateTime<Local>, Box<Error + Sync + Send>> {
|
||||
let utc = try!(DateTime::<UTC>::from_sql(type_, raw, info));
|
||||
Ok(utc.with_timezone(&Local))
|
||||
}
|
||||
@ -62,7 +79,11 @@ impl FromSql for DateTime<Local> {
|
||||
}
|
||||
|
||||
impl ToSql for DateTime<Local> {
|
||||
fn to_sql(&self, type_: &Type, mut w: &mut Vec<u8>, info: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
type_: &Type,
|
||||
mut w: &mut Vec<u8>,
|
||||
info: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
self.with_timezone(&UTC).to_sql(type_, w, info)
|
||||
}
|
||||
|
||||
@ -71,7 +92,10 @@ impl ToSql for DateTime<Local> {
|
||||
}
|
||||
|
||||
impl FromSql for DateTime<FixedOffset> {
|
||||
fn from_sql(type_: &Type, raw: &[u8], info: &SessionInfo) -> Result<DateTime<FixedOffset>, Box<Error + Sync + Send>> {
|
||||
fn from_sql(type_: &Type,
|
||||
raw: &[u8],
|
||||
info: &SessionInfo)
|
||||
-> Result<DateTime<FixedOffset>, Box<Error + Sync + Send>> {
|
||||
let utc = try!(DateTime::<UTC>::from_sql(type_, raw, info));
|
||||
Ok(utc.with_timezone(&FixedOffset::east(0)))
|
||||
}
|
||||
@ -80,7 +104,11 @@ impl FromSql for DateTime<FixedOffset> {
|
||||
}
|
||||
|
||||
impl ToSql for DateTime<FixedOffset> {
|
||||
fn to_sql(&self, type_: &Type, w: &mut Vec<u8>, info: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
type_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
info: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
self.with_timezone(&UTC).to_sql(type_, w, info)
|
||||
}
|
||||
|
||||
@ -89,7 +117,10 @@ impl ToSql for DateTime<FixedOffset> {
|
||||
}
|
||||
|
||||
impl FromSql for NaiveDate {
|
||||
fn from_sql(_: &Type, raw: &[u8], _: &SessionInfo) -> Result<NaiveDate, Box<Error + Sync + Send>> {
|
||||
fn from_sql(_: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<NaiveDate, Box<Error + Sync + Send>> {
|
||||
let jd = try!(types::date_from_sql(raw));
|
||||
Ok(base().date() + Duration::days(jd as i64))
|
||||
}
|
||||
@ -98,7 +129,11 @@ impl FromSql for NaiveDate {
|
||||
}
|
||||
|
||||
impl ToSql for NaiveDate {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let jd = (*self - base().date()).num_days();
|
||||
if jd > i32::max_value() as i64 || jd < i32::min_value() as i64 {
|
||||
return Err("value too large to transmit".into());
|
||||
@ -113,7 +148,10 @@ impl ToSql for NaiveDate {
|
||||
}
|
||||
|
||||
impl FromSql for NaiveTime {
|
||||
fn from_sql(_: &Type, raw: &[u8], _: &SessionInfo) -> Result<NaiveTime, Box<Error + Sync + Send>> {
|
||||
fn from_sql(_: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<NaiveTime, Box<Error + Sync + Send>> {
|
||||
let usec = try!(types::time_from_sql(raw));
|
||||
Ok(NaiveTime::from_hms(0, 0, 0) + Duration::microseconds(usec))
|
||||
}
|
||||
@ -122,7 +160,11 @@ impl FromSql for NaiveTime {
|
||||
}
|
||||
|
||||
impl ToSql for NaiveTime {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let delta = *self - NaiveTime::from_hms(0, 0, 0);
|
||||
let time = match delta.num_microseconds() {
|
||||
Some(time) => time,
|
||||
|
@ -7,7 +7,10 @@ use postgres_protocol::types;
|
||||
use types::{FromSql, ToSql, Type, IsNull, SessionInfo};
|
||||
|
||||
impl FromSql for MacAddress {
|
||||
fn from_sql(_: &Type, raw: &[u8], _: &SessionInfo) -> Result<MacAddress, Box<Error + Sync + Send>> {
|
||||
fn from_sql(_: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<MacAddress, Box<Error + Sync + Send>> {
|
||||
let bytes = try!(types::macaddr_from_sql(raw));
|
||||
Ok(MacAddress::new(bytes))
|
||||
}
|
||||
@ -16,7 +19,11 @@ impl FromSql for MacAddress {
|
||||
}
|
||||
|
||||
impl ToSql for MacAddress {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let mut bytes = [0; 6];
|
||||
bytes.copy_from_slice(self.as_bytes());
|
||||
types::macaddr_to_sql(bytes, w);
|
||||
|
111
src/types/mod.rs
111
src/types/mod.rs
@ -47,7 +47,11 @@ macro_rules! to_sql_checked {
|
||||
// WARNING: this function is not considered part of this crate's public API.
|
||||
// It is subject to change at any time.
|
||||
#[doc(hidden)]
|
||||
pub fn __to_sql_checked<T>(v: &T, ty: &Type, out: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>>
|
||||
pub fn __to_sql_checked<T>(v: &T,
|
||||
ty: &Type,
|
||||
out: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>>
|
||||
where T: ToSql
|
||||
{
|
||||
if !T::accepts(ty) {
|
||||
@ -307,7 +311,10 @@ pub trait FromSql: Sized {
|
||||
///
|
||||
/// The caller of this method is responsible for ensuring that this type
|
||||
/// is compatible with the Postgres `Type`.
|
||||
fn from_sql(ty: &Type, raw: &[u8], ctx: &SessionInfo) -> Result<Self, Box<Error + Sync + Send>>;
|
||||
fn from_sql(ty: &Type,
|
||||
raw: &[u8],
|
||||
ctx: &SessionInfo)
|
||||
-> Result<Self, Box<Error + Sync + Send>>;
|
||||
|
||||
/// Creates a new value of this type from a `NULL` SQL value.
|
||||
///
|
||||
@ -327,7 +334,10 @@ pub trait FromSql: Sized {
|
||||
}
|
||||
|
||||
impl<T: FromSql> FromSql for Option<T> {
|
||||
fn from_sql(ty: &Type, raw: &[u8], ctx: &SessionInfo) -> Result<Option<T>, Box<Error + Sync + Send>> {
|
||||
fn from_sql(ty: &Type,
|
||||
raw: &[u8],
|
||||
ctx: &SessionInfo)
|
||||
-> Result<Option<T>, Box<Error + Sync + Send>> {
|
||||
<T as FromSql>::from_sql(ty, raw, ctx).map(Some)
|
||||
}
|
||||
|
||||
@ -341,7 +351,10 @@ impl<T: FromSql> FromSql for Option<T> {
|
||||
}
|
||||
|
||||
impl<T: FromSql> FromSql for Vec<T> {
|
||||
fn from_sql(ty: &Type, raw: &[u8], info: &SessionInfo) -> Result<Vec<T>, Box<Error + Sync + Send>> {
|
||||
fn from_sql(ty: &Type,
|
||||
raw: &[u8],
|
||||
info: &SessionInfo)
|
||||
-> Result<Vec<T>, Box<Error + Sync + Send>> {
|
||||
let member_type = match *ty.kind() {
|
||||
Kind::Array(ref member) => member,
|
||||
_ => panic!("expected array type"),
|
||||
@ -371,7 +384,10 @@ impl<T: FromSql> FromSql for Vec<T> {
|
||||
}
|
||||
|
||||
impl FromSql for Vec<u8> {
|
||||
fn from_sql(_: &Type, raw: &[u8], _: &SessionInfo) -> Result<Vec<u8>, Box<Error + Sync + Send>> {
|
||||
fn from_sql(_: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<Vec<u8>, Box<Error + Sync + Send>> {
|
||||
Ok(types::bytea_from_sql(raw).to_owned())
|
||||
}
|
||||
|
||||
@ -414,7 +430,10 @@ simple_from!(f32, float4_from_sql, Type::Float4);
|
||||
simple_from!(f64, float8_from_sql, Type::Float8);
|
||||
|
||||
impl FromSql for HashMap<String, Option<String>> {
|
||||
fn from_sql(_: &Type, raw: &[u8], _: &SessionInfo) -> Result<HashMap<String, Option<String>>, Box<Error + Sync + Send>> {
|
||||
fn from_sql(_: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<HashMap<String, Option<String>>, Box<Error + Sync + Send>> {
|
||||
try!(types::hstore_from_sql(raw))
|
||||
.map(|(k, v)| (k.to_owned(), v.map(|v| v.to_owned())))
|
||||
.collect()
|
||||
@ -501,7 +520,12 @@ pub trait ToSql: fmt::Debug {
|
||||
/// The return value indicates if this value should be represented as
|
||||
/// `NULL`. If this is the case, implementations **must not** write
|
||||
/// anything to `out`.
|
||||
fn to_sql(&self, ty: &Type, out: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> where Self: Sized;
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
out: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>>
|
||||
where Self: Sized;
|
||||
|
||||
/// Determines if a value of this type can be converted to the specified
|
||||
/// Postgres `Type`.
|
||||
@ -511,13 +535,21 @@ pub trait ToSql: fmt::Debug {
|
||||
///
|
||||
/// *All* implementations of this method should be generated by the
|
||||
/// `to_sql_checked!()` macro.
|
||||
fn to_sql_checked(&self, ty: &Type, out: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>>;
|
||||
fn to_sql_checked(&self,
|
||||
ty: &Type,
|
||||
out: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>>;
|
||||
}
|
||||
|
||||
impl<'a, T> ToSql for &'a T
|
||||
where T: ToSql
|
||||
{
|
||||
fn to_sql(&self, ty: &Type, out: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
out: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
(*self).to_sql(ty, out, ctx)
|
||||
}
|
||||
|
||||
@ -529,7 +561,11 @@ impl<'a, T> ToSql for &'a T
|
||||
}
|
||||
|
||||
impl<T: ToSql> ToSql for Option<T> {
|
||||
fn to_sql(&self, ty: &Type, out: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
out: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
match *self {
|
||||
Some(ref val) => val.to_sql(ty, out, ctx),
|
||||
None => Ok(IsNull::Yes),
|
||||
@ -544,18 +580,20 @@ impl<T: ToSql> ToSql for Option<T> {
|
||||
}
|
||||
|
||||
impl<'a, T: ToSql> ToSql for &'a [T] {
|
||||
fn to_sql(&self, ty: &Type, w: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let member_type = match *ty.kind() {
|
||||
Kind::Array(ref member) => member,
|
||||
_ => panic!("expected array type"),
|
||||
};
|
||||
|
||||
let dimensions = [
|
||||
ArrayDimension {
|
||||
len: try!(downcast(self.len())),
|
||||
lower_bound: 1,
|
||||
}
|
||||
];
|
||||
let dimensions = [ArrayDimension {
|
||||
len: try!(downcast(self.len())),
|
||||
lower_bound: 1,
|
||||
}];
|
||||
|
||||
try!(types::array_to_sql(dimensions.iter().cloned(),
|
||||
true,
|
||||
@ -582,7 +620,11 @@ impl<'a, T: ToSql> ToSql for &'a [T] {
|
||||
}
|
||||
|
||||
impl<'a> ToSql for &'a [u8] {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
types::bytea_to_sql(*self, w);
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
@ -593,7 +635,11 @@ impl<'a> ToSql for &'a [u8] {
|
||||
}
|
||||
|
||||
impl<T: ToSql> ToSql for Vec<T> {
|
||||
fn to_sql(&self, ty: &Type, w: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
<&[T] as ToSql>::to_sql(&&**self, ty, w, ctx)
|
||||
}
|
||||
|
||||
@ -605,7 +651,11 @@ impl<T: ToSql> ToSql for Vec<T> {
|
||||
}
|
||||
|
||||
impl ToSql for Vec<u8> {
|
||||
fn to_sql(&self, ty: &Type, w: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
<&[u8] as ToSql>::to_sql(&&**self, ty, w, ctx)
|
||||
}
|
||||
|
||||
@ -617,7 +667,11 @@ impl ToSql for Vec<u8> {
|
||||
}
|
||||
|
||||
impl<'a> ToSql for &'a str {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
types::text_to_sql(*self, w);
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
@ -634,7 +688,11 @@ impl<'a> ToSql for &'a str {
|
||||
}
|
||||
|
||||
impl ToSql for String {
|
||||
fn to_sql(&self, ty: &Type, w: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
<&str as ToSql>::to_sql(&&**self, ty, w, ctx)
|
||||
}
|
||||
|
||||
@ -670,8 +728,13 @@ simple_to!(f32, float4_to_sql, Type::Float4);
|
||||
simple_to!(f64, float8_to_sql, Type::Float8);
|
||||
|
||||
impl ToSql for HashMap<String, Option<String>> {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
try!(types::hstore_to_sql(self.iter().map(|(k, v)| (&**k, v.as_ref().map(|v| &**v))), w));
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
try!(types::hstore_to_sql(self.iter().map(|(k, v)| (&**k, v.as_ref().map(|v| &**v))),
|
||||
w));
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,10 @@ use std::error::Error;
|
||||
use types::{FromSql, ToSql, IsNull, Type, SessionInfo};
|
||||
|
||||
impl FromSql for json::Json {
|
||||
fn from_sql(ty: &Type, raw: &[u8], _: &SessionInfo) -> Result<json::Json, Box<Error + Sync + Send>> {
|
||||
fn from_sql(ty: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<json::Json, Box<Error + Sync + Send>> {
|
||||
let mut raw = Cursor::new(raw);
|
||||
if let Type::Jsonb = *ty {
|
||||
// We only support version 1 of the jsonb binary format
|
||||
@ -23,7 +26,11 @@ impl FromSql for json::Json {
|
||||
}
|
||||
|
||||
impl ToSql for json::Json {
|
||||
fn to_sql(&self, ty: &Type, mut out: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
mut out: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
if let Type::Jsonb = *ty {
|
||||
out.push(1);
|
||||
}
|
||||
|
@ -8,7 +8,10 @@ use std::io::Write;
|
||||
use types::{FromSql, ToSql, IsNull, Type, SessionInfo};
|
||||
|
||||
impl FromSql for Value {
|
||||
fn from_sql(ty: &Type, mut raw: &[u8], _: &SessionInfo) -> Result<Value, Box<Error + Sync + Send>> {
|
||||
fn from_sql(ty: &Type,
|
||||
mut raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<Value, Box<Error + Sync + Send>> {
|
||||
if let Type::Jsonb = *ty {
|
||||
// We only support version 1 of the jsonb binary format
|
||||
if try!(raw.read_u8()) != 1 {
|
||||
@ -22,7 +25,11 @@ impl FromSql for Value {
|
||||
}
|
||||
|
||||
impl ToSql for Value {
|
||||
fn to_sql(&self, ty: &Type, mut out: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
mut out: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
if let Type::Jsonb = *ty {
|
||||
out.push(1);
|
||||
}
|
||||
|
@ -16,7 +16,10 @@ pub enum Date<T> {
|
||||
}
|
||||
|
||||
impl<T: FromSql> FromSql for Date<T> {
|
||||
fn from_sql(ty: &Type, raw: &[u8], ctx: &SessionInfo) -> Result<Self, Box<Error + Sync + Send>> {
|
||||
fn from_sql(ty: &Type,
|
||||
raw: &[u8],
|
||||
ctx: &SessionInfo)
|
||||
-> Result<Self, Box<Error + Sync + Send>> {
|
||||
match try!(types::date_from_sql(raw)) {
|
||||
i32::MAX => Ok(Date::PosInfinity),
|
||||
i32::MIN => Ok(Date::NegInfinity),
|
||||
@ -29,7 +32,11 @@ impl<T: FromSql> FromSql for Date<T> {
|
||||
}
|
||||
}
|
||||
impl<T: ToSql> ToSql for Date<T> {
|
||||
fn to_sql(&self, ty: &Type, out: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
out: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let value = match *self {
|
||||
Date::PosInfinity => i32::MAX,
|
||||
Date::NegInfinity => i32::MIN,
|
||||
@ -60,7 +67,10 @@ pub enum Timestamp<T> {
|
||||
}
|
||||
|
||||
impl<T: FromSql> FromSql for Timestamp<T> {
|
||||
fn from_sql(ty: &Type, raw: &[u8], ctx: &SessionInfo) -> Result<Self, Box<Error + Sync + Send>> {
|
||||
fn from_sql(ty: &Type,
|
||||
raw: &[u8],
|
||||
ctx: &SessionInfo)
|
||||
-> Result<Self, Box<Error + Sync + Send>> {
|
||||
match try!(types::timestamp_from_sql(raw)) {
|
||||
i64::MAX => Ok(Timestamp::PosInfinity),
|
||||
i64::MIN => Ok(Timestamp::NegInfinity),
|
||||
@ -74,7 +84,11 @@ impl<T: FromSql> FromSql for Timestamp<T> {
|
||||
}
|
||||
|
||||
impl<T: ToSql> ToSql for Timestamp<T> {
|
||||
fn to_sql(&self, ty: &Type, out: &mut Vec<u8>, ctx: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
ty: &Type,
|
||||
out: &mut Vec<u8>,
|
||||
ctx: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let value = match *self {
|
||||
Timestamp::PosInfinity => i64::MAX,
|
||||
Timestamp::NegInfinity => i64::MIN,
|
||||
|
@ -13,7 +13,10 @@ const NSEC_PER_USEC: i64 = 1_000;
|
||||
const TIME_SEC_CONVERSION: i64 = 946684800;
|
||||
|
||||
impl FromSql for Timespec {
|
||||
fn from_sql(_: &Type, raw: &[u8], _: &SessionInfo) -> Result<Timespec, Box<Error + Sync + Send>> {
|
||||
fn from_sql(_: &Type,
|
||||
raw: &[u8],
|
||||
_: &SessionInfo)
|
||||
-> Result<Timespec, Box<Error + Sync + Send>> {
|
||||
let t = try!(types::timestamp_from_sql(raw));
|
||||
let mut sec = t / USEC_PER_SEC + TIME_SEC_CONVERSION;
|
||||
let mut usec = t % USEC_PER_SEC;
|
||||
@ -30,7 +33,11 @@ impl FromSql for Timespec {
|
||||
}
|
||||
|
||||
impl ToSql for Timespec {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
let t = (self.sec - TIME_SEC_CONVERSION) * USEC_PER_SEC + self.nsec as i64 / NSEC_PER_USEC;
|
||||
types::timestamp_to_sql(t, w);
|
||||
Ok(IsNull::No)
|
||||
|
@ -16,7 +16,11 @@ impl FromSql for Uuid {
|
||||
}
|
||||
|
||||
impl ToSql for Uuid {
|
||||
fn to_sql(&self, _: &Type, w: &mut Vec<u8>, _: &SessionInfo) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
w: &mut Vec<u8>,
|
||||
_: &SessionInfo)
|
||||
-> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
types::uuid_to_sql(*self.as_bytes(), w);
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user