@@ -6,49 +6,51 @@ 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
8
use std:: io:: Cursor ;
9
- use postgres:: types:: { Type , IsNull , ToSql , FromSql } ;
9
+ use postgres:: types:: { Type , IsNull , ToSql , FromSql , BYTEA } ;
10
10
use std:: error:: Error ;
11
11
12
12
13
13
macro_rules! accepts_geography {
14
14
( ) => (
15
15
fn accepts( ty: & Type ) -> bool {
16
- match ty {
17
- & Type :: Other ( ref t) if t. name( ) == "geography" => true ,
18
- & Type :: Other ( ref t) if t. name( ) == "geometry" => true ,
19
- _ => false
16
+ match ty. name( ) {
17
+ "geography" | "geometry" => true ,
18
+ _ => false ,
20
19
}
21
20
}
22
21
)
23
22
}
24
23
25
24
26
25
impl < ' a > ToSql for ewkb:: EwkbPoint < ' a > {
27
- to_sql_checked ! ( ) ;
28
- accepts_geography ! ( ) ;
29
26
fn to_sql ( & self , _: & Type , out : & mut Vec < u8 > ) -> Result < IsNull , Box < Error + Sync + Send > > {
30
27
self . write_ewkb ( out) ?;
31
28
Ok ( IsNull :: No )
32
29
}
30
+
31
+ accepts_geography ! ( ) ;
32
+ to_sql_checked ! ( ) ;
33
33
}
34
34
35
35
macro_rules! impl_sql_for_point_type {
36
36
( $ptype: ident) => (
37
37
impl FromSql for ewkb:: $ptype {
38
- accepts_geography!( ) ;
39
38
fn from_sql( ty: & Type , raw: & [ u8 ] ) -> Result <Self , Box <Error + Sync + Send >> {
40
39
let mut rdr = Cursor :: new( raw) ;
41
40
ewkb:: $ptype:: read_ewkb( & mut rdr) . map_err( |_| format!( "cannot convert {} to {}" , ty, stringify!( $ptype) ) . into( ) )
42
41
}
42
+
43
+ accepts_geography!( ) ;
43
44
}
44
45
45
46
impl ToSql for ewkb:: $ptype {
46
- to_sql_checked!( ) ;
47
- accepts_geography!( ) ;
48
47
fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
49
48
self . as_ewkb( ) . write_ewkb( out) ?;
50
49
Ok ( IsNull :: No )
51
50
}
51
+
52
+ to_sql_checked!( ) ;
53
+ accepts_geography!( ) ;
52
54
}
53
55
)
54
56
}
@@ -64,22 +66,24 @@ macro_rules! impl_sql_for_geom_type {
64
66
impl <' a, T > FromSql for ewkb:: $geotype<T >
65
67
where T : ' a + Point + EwkbRead
66
68
{
67
- accepts_geography!( ) ;
68
69
fn from_sql( ty: & Type , raw: & [ u8 ] ) -> Result <Self , Box <Error + Sync + Send >> {
69
70
let mut rdr = Cursor :: new( raw) ;
70
71
ewkb:: $geotype:: <T >:: read_ewkb( & mut rdr) . map_err( |_| format!( "cannot convert {} to {}" , ty, stringify!( $geotype) ) . into( ) )
71
72
}
73
+
74
+ accepts_geography!( ) ;
72
75
}
73
76
74
77
impl <' a, T > ToSql for ewkb:: $geotype<T >
75
78
where T : ' a + Point + EwkbRead
76
79
{
77
- to_sql_checked!( ) ;
78
- accepts_geography!( ) ;
79
80
fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
80
81
self . as_ewkb( ) . write_ewkb( out) ?;
81
82
Ok ( IsNull :: No )
82
83
}
84
+
85
+ to_sql_checked!( ) ;
86
+ accepts_geography!( ) ;
83
87
}
84
88
)
85
89
}
@@ -97,12 +101,13 @@ macro_rules! impl_sql_for_ewkb_type {
97
101
where T : ' a + Point ,
98
102
I : ' a + Iterator <Item =& ' a T > + ExactSizeIterator <Item =& ' a T >
99
103
{
100
- to_sql_checked!( ) ;
101
- accepts_geography!( ) ;
102
104
fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
103
105
self . write_ewkb( out) ?;
104
106
Ok ( IsNull :: No )
105
107
}
108
+
109
+ to_sql_checked!( ) ;
110
+ accepts_geography!( ) ;
106
111
}
107
112
) ;
108
113
( $ewkbtype: ident contains $itemtypetrait: ident) => (
@@ -112,12 +117,13 @@ macro_rules! impl_sql_for_ewkb_type {
112
117
T : ' a + $itemtypetrait<' a, ItemType =P , Iter =I >,
113
118
J : ' a + Iterator <Item =& ' a T > + ExactSizeIterator <Item =& ' a T >
114
119
{
115
- to_sql_checked!( ) ;
116
- accepts_geography!( ) ;
117
120
fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
118
121
self . write_ewkb( out) ?;
119
122
Ok ( IsNull :: No )
120
123
}
124
+
125
+ to_sql_checked!( ) ;
126
+ accepts_geography!( ) ;
121
127
}
122
128
) ;
123
129
( multipoly $ewkbtype: ident contains $itemtypetrait: ident) => (
@@ -149,84 +155,79 @@ impl_sql_for_ewkb_type!(multipoly EwkbMultiPolygon contains Polygon);
149
155
impl < P > FromSql for ewkb:: GeometryT < P >
150
156
where P : Point + EwkbRead
151
157
{
152
- accepts_geography ! ( ) ;
153
158
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
154
159
let mut rdr = Cursor :: new ( raw) ;
155
160
ewkb:: GeometryT :: < P > :: read_ewkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to {}" , ty, stringify!( P ) ) . into ( ) )
156
161
}
162
+
163
+ accepts_geography ! ( ) ;
157
164
}
158
165
159
166
impl < P > FromSql for ewkb:: GeometryCollectionT < P >
160
167
where P : Point + EwkbRead
161
168
{
162
- accepts_geography ! ( ) ;
163
169
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
164
170
let mut rdr = Cursor :: new ( raw) ;
165
171
ewkb:: GeometryCollectionT :: < P > :: read_ewkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to {}" , ty, stringify!( P ) ) . into ( ) )
166
172
}
173
+
174
+ accepts_geography ! ( ) ;
167
175
}
168
176
169
177
170
178
// --- TWKB ---
171
179
172
- macro_rules! accepts_bytea {
173
- ( ) => (
174
- fn accepts( ty: & Type ) -> bool {
175
- match ty {
176
- & Type :: Bytea => true ,
177
- _ => false
178
- }
179
- }
180
- )
181
- }
182
-
183
-
184
180
impl FromSql for twkb:: Point {
185
- accepts_bytea ! ( ) ;
186
181
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
187
182
let mut rdr = Cursor :: new ( raw) ;
188
183
twkb:: Point :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to Point" , ty) . into ( ) )
189
184
}
185
+
186
+ accepts ! ( BYTEA ) ;
190
187
}
191
188
192
189
impl FromSql for twkb:: LineString {
193
- accepts_bytea ! ( ) ;
194
190
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
195
191
let mut rdr = Cursor :: new ( raw) ;
196
192
twkb:: LineString :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to LineString" , ty) . into ( ) )
197
193
}
194
+
195
+ accepts ! ( BYTEA ) ;
198
196
}
199
197
200
198
impl FromSql for twkb:: Polygon {
201
- accepts_bytea ! ( ) ;
202
199
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
203
200
let mut rdr = Cursor :: new ( raw) ;
204
201
twkb:: Polygon :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to Polygon" , ty) . into ( ) )
205
202
}
203
+
204
+ accepts ! ( BYTEA ) ;
206
205
}
207
206
208
207
impl FromSql for twkb:: MultiPoint {
209
- accepts_bytea ! ( ) ;
208
+ accepts ! ( BYTEA ) ;
210
209
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
211
210
let mut rdr = Cursor :: new ( raw) ;
212
211
twkb:: MultiPoint :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) )
213
212
}
214
213
}
215
214
216
215
impl FromSql for twkb:: MultiLineString {
217
- accepts_bytea ! ( ) ;
218
216
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
219
217
let mut rdr = Cursor :: new ( raw) ;
220
218
twkb:: MultiLineString :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiLineString" , ty) . into ( ) )
221
219
}
220
+
221
+ accepts ! ( BYTEA ) ;
222
222
}
223
223
224
224
impl FromSql for twkb:: MultiPolygon {
225
- accepts_bytea ! ( ) ;
226
225
fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
227
226
let mut rdr = Cursor :: new ( raw) ;
228
227
twkb:: MultiPolygon :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPolygon" , ty) . into ( ) )
229
228
}
229
+
230
+ accepts ! ( BYTEA ) ;
230
231
}
231
232
232
233
0 commit comments