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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::error::Error;
use std::io::Write;
use std::ops::Add;
use expression::*;
use expression::bound::Bound;
use query_source::Queriable;
use super::option::UnexpectedNullError;
use types::{self, NativeSqlType, FromSql, ToSql, IsNull};
#[cfg(feature = "quickcheck")]
mod quickcheck_impls;
#[cfg(feature = "unstable")]
mod std_time;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct PgTimestamp(pub i64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct PgDate(pub i32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct PgTime(pub i64);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PgInterval {
pub microseconds: i64,
pub days: i32,
pub months: i32,
}
impl PgInterval {
pub fn new(microseconds: i64, days: i32, months: i32) -> Self {
PgInterval {
microseconds: microseconds,
days: days,
months: months,
}
}
pub fn from_microseconds(microseconds: i64) -> Self {
Self::new(microseconds, 0, 0)
}
pub fn from_days(days: i32) -> Self {
Self::new(0, days, 0)
}
pub fn from_months(months: i32) -> Self {
Self::new(0, 0, months)
}
}
primitive_impls! {
Date -> (PgDate, 1082, 1182),
Interval -> (PgInterval, 1186, 1187),
Time -> (PgTime, 1083, 1183),
Timestamp -> (PgTimestamp, 1114, 1115),
}
impl ToSql<types::Timestamp> for PgTimestamp {
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<Error>> {
ToSql::<types::BigInt>::to_sql(&self.0, out)
}
}
impl FromSql<types::Timestamp> for PgTimestamp {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error>> {
FromSql::<types::BigInt>::from_sql(bytes)
.map(PgTimestamp)
}
}
impl ToSql<types::Date> for PgDate {
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<Error>> {
ToSql::<types::Integer>::to_sql(&self.0, out)
}
}
impl FromSql<types::Date> for PgDate {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error>> {
FromSql::<types::Integer>::from_sql(bytes)
.map(PgDate)
}
}
impl ToSql<types::Time> for PgTime {
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<Error>> {
ToSql::<types::BigInt>::to_sql(&self.0, out)
}
}
impl FromSql<types::Time> for PgTime {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error>> {
FromSql::<types::BigInt>::from_sql(bytes)
.map(PgTime)
}
}
impl ToSql<types::Interval> for PgInterval {
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<Error>> {
try!(ToSql::<types::BigInt>::to_sql(&self.microseconds, out));
try!(ToSql::<types::Integer>::to_sql(&self.days, out));
try!(ToSql::<types::Integer>::to_sql(&self.months, out));
Ok(IsNull::No)
}
}
impl FromSql<types::Interval> for PgInterval {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error>> {
let bytes = not_none!(bytes);
Ok(PgInterval {
microseconds: try!(FromSql::<types::BigInt>::from_sql(Some(&bytes[..8]))),
days: try!(FromSql::<types::Integer>::from_sql(Some(&bytes[8..12]))),
months: try!(FromSql::<types::Integer>::from_sql(Some(&bytes[12..16]))),
})
}
}
impl Add<PgInterval> for PgInterval {
type Output = PgInterval;
fn add(self, other: PgInterval) -> Self::Output {
PgInterval {
microseconds: self.microseconds + other.microseconds,
days: self.days + other.days,
months: self.months + other.months,
}
}
}