|
| 1 | +use std::io::prelude::*; |
| 2 | +use std::{i32, i64}; |
| 3 | + |
| 4 | +use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian}; |
| 5 | + |
| 6 | +use Result; |
| 7 | +use error::Error; |
| 8 | +use types::{Type, FromSql, ToSql, IsNull, SessionInfo}; |
| 9 | + |
| 10 | +/// A wrapper that can be used to represent infinity with `Type::Date` types. |
| 11 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 12 | +pub enum Date<T> { |
| 13 | + /// Represents `infinity`, a date that is later than all other dates. |
| 14 | + PosInfinity, |
| 15 | + /// Represents `-infinity`, a date that is earlier than all other dates. |
| 16 | + NegInfinity, |
| 17 | + /// The wrapped date. |
| 18 | + Value(T), |
| 19 | +} |
| 20 | + |
| 21 | +impl<T: FromSql> FromSql for Date<T> { |
| 22 | + fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Self> { |
| 23 | + if *ty != Type::Date { |
| 24 | + return Err(Error::Conversion("expected date type".into())); |
| 25 | + } |
| 26 | + |
| 27 | + let mut buf = [0; 4]; |
| 28 | + try!(raw.read_exact(buf.as_mut())); |
| 29 | + |
| 30 | + match try!(buf.as_ref().read_i32::<BigEndian>()) { |
| 31 | + i32::MAX => Ok(Date::PosInfinity), |
| 32 | + i32::MIN => Ok(Date::NegInfinity), |
| 33 | + _ => T::from_sql(ty, &mut &mut buf.as_ref(), ctx).map(Date::Value), |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + fn accepts(ty: &Type) -> bool { |
| 38 | + *ty == Type::Date && T::accepts(ty) |
| 39 | + } |
| 40 | +} |
| 41 | +impl<T: ToSql> ToSql for Date<T> { |
| 42 | + fn to_sql<W: Write+?Sized>(&self, ty: &Type, out: &mut W, ctx: &SessionInfo) -> Result<IsNull> { |
| 43 | + if *ty != Type::Date { |
| 44 | + return Err(Error::Conversion("expected date type".into())); |
| 45 | + } |
| 46 | + |
| 47 | + let value = match *self { |
| 48 | + Date::PosInfinity => i32::MAX, |
| 49 | + Date::NegInfinity => i32::MIN, |
| 50 | + Date::Value(ref v) => return v.to_sql(ty, out, ctx), |
| 51 | + }; |
| 52 | + |
| 53 | + try!(out.write_i32::<BigEndian>(value)); |
| 54 | + Ok(IsNull::No) |
| 55 | + } |
| 56 | + |
| 57 | + fn accepts(ty: &Type) -> bool { |
| 58 | + *ty == Type::Date && T::accepts(ty) |
| 59 | + } |
| 60 | + |
| 61 | + to_sql_checked!(); |
| 62 | +} |
| 63 | + |
| 64 | +/// A wrapper that can be used to represent infinity with `Type::Timestamp` and `Type::TimestampTZ` |
| 65 | +/// types. |
| 66 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 67 | +pub enum Timestamp<T> { |
| 68 | + /// Represents `infinity`, a timestamp that is later than all other timestamps. |
| 69 | + PosInfinity, |
| 70 | + /// Represents `-infinity`, a timestamp that is earlier than all other timestamps. |
| 71 | + NegInfinity, |
| 72 | + /// The wrapped timestamp. |
| 73 | + Value(T), |
| 74 | +} |
| 75 | + |
| 76 | +impl<T: FromSql> FromSql for Timestamp<T> { |
| 77 | + fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Self> { |
| 78 | + if *ty != Type::Timestamp && *ty != Type::TimestampTZ { |
| 79 | + return Err(Error::Conversion("expected timestamp or timestamptz type".into())); |
| 80 | + } |
| 81 | + |
| 82 | + let mut buf = [0; 8]; |
| 83 | + try!(raw.read_exact(buf.as_mut())); |
| 84 | + |
| 85 | + match try!(buf.as_ref().read_i64::<BigEndian>()) { |
| 86 | + i64::MAX => Ok(Timestamp::PosInfinity), |
| 87 | + i64::MIN => Ok(Timestamp::NegInfinity), |
| 88 | + _ => T::from_sql(ty, &mut &mut buf.as_ref(), ctx).map(Timestamp::Value), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + fn accepts(ty: &Type) -> bool { |
| 93 | + (*ty == Type::Timestamp || *ty == Type::TimestampTZ) && T::accepts(ty) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl<T: ToSql> ToSql for Timestamp<T> { |
| 98 | + fn to_sql<W: Write+?Sized>(&self, ty: &Type, out: &mut W, ctx: &SessionInfo) -> Result<IsNull> { |
| 99 | + if *ty != Type::Timestamp && *ty != Type::TimestampTZ { |
| 100 | + return Err(Error::Conversion("expected timestamp or timestamptz type".into())); |
| 101 | + } |
| 102 | + |
| 103 | + let value = match *self { |
| 104 | + Timestamp::PosInfinity => i64::MAX, |
| 105 | + Timestamp::NegInfinity => i64::MIN, |
| 106 | + Timestamp::Value(ref v) => return v.to_sql(ty, out, ctx), |
| 107 | + }; |
| 108 | + |
| 109 | + try!(out.write_i64::<BigEndian>(value)); |
| 110 | + Ok(IsNull::No) |
| 111 | + } |
| 112 | + |
| 113 | + fn accepts(ty: &Type) -> bool { |
| 114 | + (*ty == Type::Timestamp || *ty == Type::TimestampTZ) && T::accepts(ty) |
| 115 | + } |
| 116 | + |
| 117 | + to_sql_checked!(); |
| 118 | +} |
0 commit comments