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