5
5
use types:: { Point , LineString , Polygon } ;
6
6
use ewkb:: { self , EwkbRead , EwkbWrite , AsEwkbPoint , AsEwkbLineString , AsEwkbPolygon , AsEwkbMultiPoint , AsEwkbMultiLineString , AsEwkbMultiPolygon } ;
7
7
use twkb:: { self , TwkbGeom } ;
8
- use std;
9
- use std:: io:: prelude:: * ;
10
- use postgres;
8
+ use std:: io:: Cursor ;
11
9
use postgres:: types:: { Type , IsNull , ToSql , FromSql , SessionInfo } ;
12
- use error:: Error ;
13
- use std:: convert:: From ;
14
-
15
-
16
- impl From < Error > for postgres:: error:: Error {
17
- fn from ( e : Error ) -> postgres:: error:: Error {
18
- postgres:: error:: Error :: Conversion ( Box :: new ( e) )
19
- }
20
- }
10
+ use std:: error:: Error ;
21
11
22
12
23
13
macro_rules! accepts_geography {
@@ -36,7 +26,7 @@ macro_rules! accepts_geography {
36
26
impl < ' a > ToSql for ewkb:: EwkbPoint < ' a > {
37
27
to_sql_checked ! ( ) ;
38
28
accepts_geography ! ( ) ;
39
- fn to_sql < W : Write +? Sized > ( & self , _: & Type , out : & mut W , _ctx : & SessionInfo ) -> postgres :: Result < IsNull > {
29
+ fn to_sql ( & self , _: & Type , out : & mut Vec < u8 > , _ctx : & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
40
30
try!( self . write_ewkb ( out) ) ;
41
31
Ok ( IsNull :: No )
42
32
}
@@ -46,15 +36,16 @@ macro_rules! impl_sql_for_point_type {
46
36
( $ptype: ident) => (
47
37
impl FromSql for ewkb:: $ptype {
48
38
accepts_geography!( ) ;
49
- fn from_sql<R : Read >( ty: & Type , raw: & mut R , _ctx: & SessionInfo ) -> postgres:: Result <ewkb:: $ptype> {
50
- ewkb:: $ptype:: read_ewkb( raw) . map_err( |_| { let err: Box <std:: error:: Error + Sync + Send > = format!( "cannot convert {} to POINT" , ty) . into( ) ; postgres:: error:: Error :: Conversion ( err) } )
39
+ fn from_sql( ty: & Type , raw: & [ u8 ] , _ctx: & SessionInfo ) -> Result <Self , Box <Error + Sync + Send >> {
40
+ let mut rdr = Cursor :: new( raw) ;
41
+ ewkb:: $ptype:: read_ewkb( & mut rdr) . map_err( |_| format!( "cannot convert {} to POINT" , ty) . into( ) )
51
42
}
52
43
}
53
44
54
45
impl ToSql for ewkb:: $ptype {
55
46
to_sql_checked!( ) ;
56
47
accepts_geography!( ) ;
57
- fn to_sql< W : Write +? Sized > ( & self , _: & Type , out: & mut W , _ctx: & SessionInfo ) -> postgres :: Result <IsNull > {
48
+ fn to_sql( & self , _: & Type , out: & mut Vec < u8 > , _ctx: & SessionInfo ) -> Result <IsNull , Box < Error + Sync + Send > > {
58
49
try!( self . as_ewkb( ) . write_ewkb( out) ) ;
59
50
Ok ( IsNull :: No )
60
51
}
@@ -74,8 +65,9 @@ macro_rules! impl_sql_for_geom_type {
74
65
where T : ' a + Point + EwkbRead
75
66
{
76
67
accepts_geography!( ) ;
77
- fn from_sql<R : Read >( ty: & Type , raw: & mut R , _ctx: & SessionInfo ) -> postgres:: Result <ewkb:: $geotype<T >> {
78
- ewkb:: $geotype:: <T >:: read_ewkb( raw) . map_err( |_| { let err: Box <std:: error:: Error + Sync + Send > = format!( "cannot convert {} to LINESTRING" , ty) . into( ) ; postgres:: error:: Error :: Conversion ( err) } )
68
+ fn from_sql( ty: & Type , raw: & [ u8 ] , _ctx: & SessionInfo ) -> Result <Self , Box <Error + Sync + Send >> {
69
+ let mut rdr = Cursor :: new( raw) ;
70
+ ewkb:: $geotype:: <T >:: read_ewkb( & mut rdr) . map_err( |_| format!( "cannot convert {} to LINESTRING" , ty) . into( ) )
79
71
}
80
72
}
81
73
@@ -84,7 +76,7 @@ macro_rules! impl_sql_for_geom_type {
84
76
{
85
77
to_sql_checked!( ) ;
86
78
accepts_geography!( ) ;
87
- fn to_sql< W : Write +? Sized > ( & self , _: & Type , out: & mut W , _ctx: & SessionInfo ) -> postgres :: Result <IsNull > {
79
+ fn to_sql( & self , _: & Type , out: & mut Vec < u8 > , _ctx: & SessionInfo ) -> Result <IsNull , Box < Error + Sync + Send > > {
88
80
try!( self . as_ewkb( ) . write_ewkb( out) ) ;
89
81
Ok ( IsNull :: No )
90
82
}
@@ -107,7 +99,7 @@ macro_rules! impl_sql_for_ewkb_type {
107
99
{
108
100
to_sql_checked!( ) ;
109
101
accepts_geography!( ) ;
110
- fn to_sql< W : Write +? Sized > ( & self , _: & Type , out: & mut W , _ctx: & SessionInfo ) -> postgres :: Result <IsNull > {
102
+ fn to_sql( & self , _: & Type , out: & mut Vec < u8 > , _ctx: & SessionInfo ) -> Result <IsNull , Box < Error + Sync + Send > > {
111
103
try!( self . write_ewkb( out) ) ;
112
104
Ok ( IsNull :: No )
113
105
}
@@ -122,7 +114,7 @@ macro_rules! impl_sql_for_ewkb_type {
122
114
{
123
115
to_sql_checked!( ) ;
124
116
accepts_geography!( ) ;
125
- fn to_sql< W : Write +? Sized > ( & self , _: & Type , out: & mut W , _ctx: & SessionInfo ) -> postgres :: Result <IsNull > {
117
+ fn to_sql( & self , _: & Type , out: & mut Vec < u8 > , _ctx: & SessionInfo ) -> Result <IsNull , Box < Error + Sync + Send > > {
126
118
try!( self . write_ewkb( out) ) ;
127
119
Ok ( IsNull :: No )
128
120
}
@@ -139,7 +131,7 @@ macro_rules! impl_sql_for_ewkb_type {
139
131
{
140
132
to_sql_checked!( ) ;
141
133
accepts_geography!( ) ;
142
- fn to_sql< W : Write +? Sized > ( & self , _: & Type , out: & mut W , _ctx: & SessionInfo ) -> postgres :: Result <IsNull > {
134
+ fn to_sql( & self , _: & Type , out: & mut Vec < u8 > , _ctx: & SessionInfo ) -> Result <IsNull , Box < Error + Sync + Send > > {
143
135
try!( self . write_ewkb( out) ) ;
144
136
Ok ( IsNull :: No )
145
137
}
@@ -158,17 +150,19 @@ impl<P> FromSql for ewkb::GeometryT<P>
158
150
where P : Point + EwkbRead
159
151
{
160
152
accepts_geography ! ( ) ;
161
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < ewkb:: GeometryT < P > > {
162
- ewkb:: GeometryT :: < P > :: read_ewkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
153
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
154
+ let mut rdr = Cursor :: new ( raw) ;
155
+ ewkb:: GeometryT :: < P > :: read_ewkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) )
163
156
}
164
157
}
165
158
166
159
impl < P > FromSql for ewkb:: GeometryCollectionT < P >
167
160
where P : Point + EwkbRead
168
161
{
169
162
accepts_geography ! ( ) ;
170
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < ewkb:: GeometryCollectionT < P > > {
171
- ewkb:: GeometryCollectionT :: < P > :: read_ewkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
163
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
164
+ let mut rdr = Cursor :: new ( raw) ;
165
+ ewkb:: GeometryCollectionT :: < P > :: read_ewkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) )
172
166
}
173
167
}
174
168
@@ -189,43 +183,49 @@ macro_rules! accepts_bytea {
189
183
190
184
impl FromSql for twkb:: Point {
191
185
accepts_bytea ! ( ) ;
192
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < twkb:: Point > {
193
- twkb:: Point :: read_twkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to Point" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
186
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
187
+ let mut rdr = Cursor :: new ( raw) ;
188
+ twkb:: Point :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to Point" , ty) . into ( ) )
194
189
}
195
190
}
196
191
197
192
impl FromSql for twkb:: LineString {
198
193
accepts_bytea ! ( ) ;
199
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < twkb:: LineString > {
200
- twkb:: LineString :: read_twkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to LineString" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
194
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
195
+ let mut rdr = Cursor :: new ( raw) ;
196
+ twkb:: LineString :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to LineString" , ty) . into ( ) )
201
197
}
202
198
}
203
199
204
200
impl FromSql for twkb:: Polygon {
205
201
accepts_bytea ! ( ) ;
206
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < twkb:: Polygon > {
207
- twkb:: Polygon :: read_twkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to Polygon" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
202
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
203
+ let mut rdr = Cursor :: new ( raw) ;
204
+ twkb:: Polygon :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to Polygon" , ty) . into ( ) )
208
205
}
209
206
}
210
207
211
208
impl FromSql for twkb:: MultiPoint {
212
209
accepts_bytea ! ( ) ;
213
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < twkb:: MultiPoint > {
214
- twkb:: MultiPoint :: read_twkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
210
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
211
+ let mut rdr = Cursor :: new ( raw) ;
212
+ twkb:: MultiPoint :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) )
215
213
}
216
214
}
217
215
218
216
impl FromSql for twkb:: MultiLineString {
219
217
accepts_bytea ! ( ) ;
220
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < twkb:: MultiLineString > {
221
- twkb:: MultiLineString :: read_twkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to MultiLineString" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
218
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
219
+ let mut rdr = Cursor :: new ( raw) ;
220
+ twkb:: MultiLineString :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiLineString" , ty) . into ( ) )
222
221
}
223
222
}
224
223
225
224
impl FromSql for twkb:: MultiPolygon {
226
225
accepts_bytea ! ( ) ;
227
- fn from_sql < R : Read > ( ty : & Type , raw : & mut R , _ctx : & SessionInfo ) -> postgres:: Result < twkb:: MultiPolygon > {
228
- twkb:: MultiPolygon :: read_twkb ( raw) . map_err ( |_| { let err: Box < std:: error:: Error + Sync + Send > = format ! ( "cannot convert {} to MultiPolygon" , ty) . into ( ) ; postgres:: error:: Error :: Conversion ( err) } )
226
+ fn from_sql ( ty : & Type , raw : & [ u8 ] , _ctx : & SessionInfo ) -> Result < Self , Box < Error + Sync + Send > > {
227
+ let mut rdr = Cursor :: new ( raw) ;
228
+ twkb:: MultiPolygon :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPolygon" , ty) . into ( ) )
229
229
}
230
230
}
231
231
@@ -251,7 +251,7 @@ mod tests {
251
251
252
252
fn connect ( ) -> Connection {
253
253
match env:: var ( "DBCONN" ) {
254
- Result :: Ok ( val) => Connection :: connect ( & val as & str , postgres:: SslMode :: None ) ,
254
+ Result :: Ok ( val) => Connection :: connect ( & val as & str , postgres:: TlsMode :: None ) ,
255
255
Result :: Err ( err) => { panic ! ( "{:#?}" , err) }
256
256
} . unwrap ( )
257
257
}
@@ -543,7 +543,7 @@ mod tests {
543
543
#[ ignore]
544
544
#[ allow( unused_imports, unused_variables) ]
545
545
fn test_examples ( ) {
546
- use postgres:: { Connection , SslMode } ;
546
+ use postgres:: { Connection , TlsMode } ;
547
547
//use postgis::ewkb;
548
548
//use postgis::LineString;
549
549
0 commit comments