1
1
extern crate chrono;
2
2
3
- use std:: io:: prelude:: * ;
4
- use byteorder:: { ReadBytesExt , WriteBytesExt , BigEndian } ;
3
+ use postgres_protocol:: types;
5
4
use self :: chrono:: { Duration , NaiveDate , NaiveTime , NaiveDateTime , DateTime , UTC , Local ,
6
5
FixedOffset } ;
6
+ use std:: error:: Error ;
7
7
8
- use Result ;
9
- use error:: Error ;
10
8
use types:: { FromSql , ToSql , IsNull , Type , SessionInfo } ;
11
9
12
10
fn base ( ) -> NaiveDateTime {
13
11
NaiveDate :: from_ymd ( 2000 , 1 , 1 ) . and_hms ( 0 , 0 , 0 )
14
12
}
15
13
16
14
impl FromSql for NaiveDateTime {
17
- fn from_sql < R : Read > ( _: & Type , raw : & mut R , _: & SessionInfo ) -> Result < NaiveDateTime > {
18
- let t = try!( raw . read_i64 :: < BigEndian > ( ) ) ;
15
+ fn from_sql ( _: & Type , raw : & [ u8 ] , _: & SessionInfo ) -> Result < NaiveDateTime , Box < Error + Sync + Send > > {
16
+ let t = try!( types :: timestamp_from_sql ( raw ) ) ;
19
17
Ok ( base ( ) + Duration :: microseconds ( t) )
20
18
}
21
19
22
20
accepts ! ( Type :: Timestamp ) ;
23
21
}
24
22
25
23
impl ToSql for NaiveDateTime {
26
- fn to_sql < W : Write + ?Sized > ( & self ,
27
- _: & Type ,
28
- mut w : & mut W ,
29
- _: & SessionInfo )
30
- -> Result < IsNull > {
24
+ fn to_sql ( & self , _: & Type , w : & mut Vec < u8 > , _: & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
31
25
let time = match ( * self - base ( ) ) . num_microseconds ( ) {
32
26
Some ( time) => time,
33
- None => return Err ( Error :: Conversion ( "value too large to transmit" . into ( ) ) ) ,
27
+ None => return Err ( "value too large to transmit" . into ( ) ) ,
34
28
} ;
35
- try! ( w . write_i64 :: < BigEndian > ( time) ) ;
29
+ types :: timestamp_to_sql ( time, w ) ;
36
30
Ok ( IsNull :: No )
37
31
}
38
32
@@ -41,7 +35,7 @@ impl ToSql for NaiveDateTime {
41
35
}
42
36
43
37
impl FromSql for DateTime < UTC > {
44
- fn from_sql < R : Read > ( type_ : & Type , raw : & mut R , info : & SessionInfo ) -> Result < DateTime < UTC > > {
38
+ fn from_sql ( type_ : & Type , raw : & [ u8 ] , info : & SessionInfo ) -> Result < DateTime < UTC > , Box < Error + Sync + Send > > {
45
39
let naive = try!( NaiveDateTime :: from_sql ( type_, raw, info) ) ;
46
40
Ok ( DateTime :: from_utc ( naive, UTC ) )
47
41
}
@@ -50,11 +44,7 @@ impl FromSql for DateTime<UTC> {
50
44
}
51
45
52
46
impl ToSql for DateTime < UTC > {
53
- fn to_sql < W : Write + ?Sized > ( & self ,
54
- type_ : & Type ,
55
- mut w : & mut W ,
56
- info : & SessionInfo )
57
- -> Result < IsNull > {
47
+ fn to_sql ( & self , type_ : & Type , w : & mut Vec < u8 > , info : & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
58
48
self . naive_utc ( ) . to_sql ( type_, w, info)
59
49
}
60
50
@@ -63,7 +53,7 @@ impl ToSql for DateTime<UTC> {
63
53
}
64
54
65
55
impl FromSql for DateTime < Local > {
66
- fn from_sql < R : Read > ( type_ : & Type , raw : & mut R , info : & SessionInfo ) -> Result < DateTime < Local > > {
56
+ fn from_sql ( type_ : & Type , raw : & [ u8 ] , info : & SessionInfo ) -> Result < DateTime < Local > , Box < Error + Sync + Send > > {
67
57
let utc = try!( DateTime :: < UTC > :: from_sql ( type_, raw, info) ) ;
68
58
Ok ( utc. with_timezone ( & Local ) )
69
59
}
@@ -72,11 +62,7 @@ impl FromSql for DateTime<Local> {
72
62
}
73
63
74
64
impl ToSql for DateTime < Local > {
75
- fn to_sql < W : Write + ?Sized > ( & self ,
76
- type_ : & Type ,
77
- mut w : & mut W ,
78
- info : & SessionInfo )
79
- -> Result < IsNull > {
65
+ fn to_sql ( & self , type_ : & Type , mut w : & mut Vec < u8 > , info : & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
80
66
self . with_timezone ( & UTC ) . to_sql ( type_, w, info)
81
67
}
82
68
@@ -85,10 +71,7 @@ impl ToSql for DateTime<Local> {
85
71
}
86
72
87
73
impl FromSql for DateTime < FixedOffset > {
88
- fn from_sql < R : Read > ( type_ : & Type ,
89
- raw : & mut R ,
90
- info : & SessionInfo )
91
- -> Result < DateTime < FixedOffset > > {
74
+ fn from_sql ( type_ : & Type , raw : & [ u8 ] , info : & SessionInfo ) -> Result < DateTime < FixedOffset > , Box < Error + Sync + Send > > {
92
75
let utc = try!( DateTime :: < UTC > :: from_sql ( type_, raw, info) ) ;
93
76
Ok ( utc. with_timezone ( & FixedOffset :: east ( 0 ) ) )
94
77
}
@@ -97,11 +80,7 @@ impl FromSql for DateTime<FixedOffset> {
97
80
}
98
81
99
82
impl ToSql for DateTime < FixedOffset > {
100
- fn to_sql < W : Write + ?Sized > ( & self ,
101
- type_ : & Type ,
102
- mut w : & mut W ,
103
- info : & SessionInfo )
104
- -> Result < IsNull > {
83
+ fn to_sql ( & self , type_ : & Type , w : & mut Vec < u8 > , info : & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
105
84
self . with_timezone ( & UTC ) . to_sql ( type_, w, info)
106
85
}
107
86
@@ -110,26 +89,22 @@ impl ToSql for DateTime<FixedOffset> {
110
89
}
111
90
112
91
impl FromSql for NaiveDate {
113
- fn from_sql < R : Read > ( _: & Type , raw : & mut R , _: & SessionInfo ) -> Result < NaiveDate > {
114
- let jd = try!( raw . read_i32 :: < BigEndian > ( ) ) ;
92
+ fn from_sql ( _: & Type , raw : & [ u8 ] , _: & SessionInfo ) -> Result < NaiveDate , Box < Error + Sync + Send > > {
93
+ let jd = try!( types :: date_from_sql ( raw ) ) ;
115
94
Ok ( base ( ) . date ( ) + Duration :: days ( jd as i64 ) )
116
95
}
117
96
118
97
accepts ! ( Type :: Date ) ;
119
98
}
120
99
121
100
impl ToSql for NaiveDate {
122
- fn to_sql < W : Write + ?Sized > ( & self ,
123
- _: & Type ,
124
- mut w : & mut W ,
125
- _: & SessionInfo )
126
- -> Result < IsNull > {
101
+ fn to_sql ( & self , _: & Type , w : & mut Vec < u8 > , _: & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
127
102
let jd = ( * self - base ( ) . date ( ) ) . num_days ( ) ;
128
103
if jd > i32:: max_value ( ) as i64 || jd < i32:: min_value ( ) as i64 {
129
- return Err ( Error :: Conversion ( "value too large to transmit" . into ( ) ) ) ;
104
+ return Err ( "value too large to transmit" . into ( ) ) ;
130
105
}
131
106
132
- try! ( w . write_i32 :: < BigEndian > ( jd as i32 ) ) ;
107
+ types :: date_to_sql ( jd as i32 , w ) ;
133
108
Ok ( IsNull :: No )
134
109
}
135
110
@@ -138,26 +113,22 @@ impl ToSql for NaiveDate {
138
113
}
139
114
140
115
impl FromSql for NaiveTime {
141
- fn from_sql < R : Read > ( _: & Type , raw : & mut R , _: & SessionInfo ) -> Result < NaiveTime > {
142
- let usec = try!( raw . read_i64 :: < BigEndian > ( ) ) ;
116
+ fn from_sql ( _: & Type , raw : & [ u8 ] , _: & SessionInfo ) -> Result < NaiveTime , Box < Error + Sync + Send > > {
117
+ let usec = try!( types :: time_from_sql ( raw ) ) ;
143
118
Ok ( NaiveTime :: from_hms ( 0 , 0 , 0 ) + Duration :: microseconds ( usec) )
144
119
}
145
120
146
121
accepts ! ( Type :: Time ) ;
147
122
}
148
123
149
124
impl ToSql for NaiveTime {
150
- fn to_sql < W : Write + ?Sized > ( & self ,
151
- _: & Type ,
152
- mut w : & mut W ,
153
- _: & SessionInfo )
154
- -> Result < IsNull > {
125
+ fn to_sql ( & self , _: & Type , w : & mut Vec < u8 > , _: & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
155
126
let delta = * self - NaiveTime :: from_hms ( 0 , 0 , 0 ) ;
156
127
let time = match delta. num_microseconds ( ) {
157
128
Some ( time) => time,
158
- None => return Err ( Error :: Conversion ( "value too large to transmit" . into ( ) ) ) ,
129
+ None => return Err ( "value too large to transmit" . into ( ) ) ,
159
130
} ;
160
- try! ( w . write_i64 :: < BigEndian > ( time) ) ;
131
+ types :: time_to_sql ( time, w ) ;
161
132
Ok ( IsNull :: No )
162
133
}
163
134
0 commit comments