@@ -6,49 +6,51 @@ use types::{Point, LineString, Polygon};
66use ewkb:: { self , EwkbRead , EwkbWrite , AsEwkbPoint , AsEwkbLineString , AsEwkbPolygon , AsEwkbMultiPoint , AsEwkbMultiLineString , AsEwkbMultiPolygon } ;
77use twkb:: { self , TwkbGeom } ;
88use std:: io:: Cursor ;
9- use postgres:: types:: { Type , IsNull , ToSql , FromSql } ;
9+ use postgres:: types:: { Type , IsNull , ToSql , FromSql , BYTEA } ;
1010use std:: error:: Error ;
1111
1212
1313macro_rules! accepts_geography {
1414 ( ) => (
1515 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 ,
2019 }
2120 }
2221 )
2322}
2423
2524
2625impl < ' a > ToSql for ewkb:: EwkbPoint < ' a > {
27- to_sql_checked ! ( ) ;
28- accepts_geography ! ( ) ;
2926 fn to_sql ( & self , _: & Type , out : & mut Vec < u8 > ) -> Result < IsNull , Box < Error + Sync + Send > > {
3027 self . write_ewkb ( out) ?;
3128 Ok ( IsNull :: No )
3229 }
30+
31+ accepts_geography ! ( ) ;
32+ to_sql_checked ! ( ) ;
3333}
3434
3535macro_rules! impl_sql_for_point_type {
3636 ( $ptype: ident) => (
3737 impl FromSql for ewkb:: $ptype {
38- accepts_geography!( ) ;
3938 fn from_sql( ty: & Type , raw: & [ u8 ] ) -> Result <Self , Box <Error + Sync + Send >> {
4039 let mut rdr = Cursor :: new( raw) ;
4140 ewkb:: $ptype:: read_ewkb( & mut rdr) . map_err( |_| format!( "cannot convert {} to {}" , ty, stringify!( $ptype) ) . into( ) )
4241 }
42+
43+ accepts_geography!( ) ;
4344 }
4445
4546 impl ToSql for ewkb:: $ptype {
46- to_sql_checked!( ) ;
47- accepts_geography!( ) ;
4847 fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
4948 self . as_ewkb( ) . write_ewkb( out) ?;
5049 Ok ( IsNull :: No )
5150 }
51+
52+ to_sql_checked!( ) ;
53+ accepts_geography!( ) ;
5254 }
5355 )
5456}
@@ -64,22 +66,24 @@ macro_rules! impl_sql_for_geom_type {
6466 impl <' a, T > FromSql for ewkb:: $geotype<T >
6567 where T : ' a + Point + EwkbRead
6668 {
67- accepts_geography!( ) ;
6869 fn from_sql( ty: & Type , raw: & [ u8 ] ) -> Result <Self , Box <Error + Sync + Send >> {
6970 let mut rdr = Cursor :: new( raw) ;
7071 ewkb:: $geotype:: <T >:: read_ewkb( & mut rdr) . map_err( |_| format!( "cannot convert {} to {}" , ty, stringify!( $geotype) ) . into( ) )
7172 }
73+
74+ accepts_geography!( ) ;
7275 }
7376
7477 impl <' a, T > ToSql for ewkb:: $geotype<T >
7578 where T : ' a + Point + EwkbRead
7679 {
77- to_sql_checked!( ) ;
78- accepts_geography!( ) ;
7980 fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
8081 self . as_ewkb( ) . write_ewkb( out) ?;
8182 Ok ( IsNull :: No )
8283 }
84+
85+ to_sql_checked!( ) ;
86+ accepts_geography!( ) ;
8387 }
8488 )
8589}
@@ -97,12 +101,13 @@ macro_rules! impl_sql_for_ewkb_type {
97101 where T : ' a + Point ,
98102 I : ' a + Iterator <Item =& ' a T > + ExactSizeIterator <Item =& ' a T >
99103 {
100- to_sql_checked!( ) ;
101- accepts_geography!( ) ;
102104 fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
103105 self . write_ewkb( out) ?;
104106 Ok ( IsNull :: No )
105107 }
108+
109+ to_sql_checked!( ) ;
110+ accepts_geography!( ) ;
106111 }
107112 ) ;
108113 ( $ewkbtype: ident contains $itemtypetrait: ident) => (
@@ -112,12 +117,13 @@ macro_rules! impl_sql_for_ewkb_type {
112117 T : ' a + $itemtypetrait<' a, ItemType =P , Iter =I >,
113118 J : ' a + Iterator <Item =& ' a T > + ExactSizeIterator <Item =& ' a T >
114119 {
115- to_sql_checked!( ) ;
116- accepts_geography!( ) ;
117120 fn to_sql( & self , _: & Type , out: & mut Vec <u8 >) -> Result <IsNull , Box <Error + Sync + Send >> {
118121 self . write_ewkb( out) ?;
119122 Ok ( IsNull :: No )
120123 }
124+
125+ to_sql_checked!( ) ;
126+ accepts_geography!( ) ;
121127 }
122128 ) ;
123129 ( multipoly $ewkbtype: ident contains $itemtypetrait: ident) => (
@@ -149,84 +155,79 @@ impl_sql_for_ewkb_type!(multipoly EwkbMultiPolygon contains Polygon);
149155impl < P > FromSql for ewkb:: GeometryT < P >
150156 where P : Point + EwkbRead
151157{
152- accepts_geography ! ( ) ;
153158 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
154159 let mut rdr = Cursor :: new ( raw) ;
155160 ewkb:: GeometryT :: < P > :: read_ewkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to {}" , ty, stringify!( P ) ) . into ( ) )
156161 }
162+
163+ accepts_geography ! ( ) ;
157164}
158165
159166impl < P > FromSql for ewkb:: GeometryCollectionT < P >
160167 where P : Point + EwkbRead
161168{
162- accepts_geography ! ( ) ;
163169 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
164170 let mut rdr = Cursor :: new ( raw) ;
165171 ewkb:: GeometryCollectionT :: < P > :: read_ewkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to {}" , ty, stringify!( P ) ) . into ( ) )
166172 }
173+
174+ accepts_geography ! ( ) ;
167175}
168176
169177
170178// --- TWKB ---
171179
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-
184180impl FromSql for twkb:: Point {
185- accepts_bytea ! ( ) ;
186181 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
187182 let mut rdr = Cursor :: new ( raw) ;
188183 twkb:: Point :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to Point" , ty) . into ( ) )
189184 }
185+
186+ accepts ! ( BYTEA ) ;
190187}
191188
192189impl FromSql for twkb:: LineString {
193- accepts_bytea ! ( ) ;
194190 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
195191 let mut rdr = Cursor :: new ( raw) ;
196192 twkb:: LineString :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to LineString" , ty) . into ( ) )
197193 }
194+
195+ accepts ! ( BYTEA ) ;
198196}
199197
200198impl FromSql for twkb:: Polygon {
201- accepts_bytea ! ( ) ;
202199 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
203200 let mut rdr = Cursor :: new ( raw) ;
204201 twkb:: Polygon :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to Polygon" , ty) . into ( ) )
205202 }
203+
204+ accepts ! ( BYTEA ) ;
206205}
207206
208207impl FromSql for twkb:: MultiPoint {
209- accepts_bytea ! ( ) ;
208+ accepts ! ( BYTEA ) ;
210209 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
211210 let mut rdr = Cursor :: new ( raw) ;
212211 twkb:: MultiPoint :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPoint" , ty) . into ( ) )
213212 }
214213}
215214
216215impl FromSql for twkb:: MultiLineString {
217- accepts_bytea ! ( ) ;
218216 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
219217 let mut rdr = Cursor :: new ( raw) ;
220218 twkb:: MultiLineString :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiLineString" , ty) . into ( ) )
221219 }
220+
221+ accepts ! ( BYTEA ) ;
222222}
223223
224224impl FromSql for twkb:: MultiPolygon {
225- accepts_bytea ! ( ) ;
226225 fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Self , Box < Error + Sync + Send > > {
227226 let mut rdr = Cursor :: new ( raw) ;
228227 twkb:: MultiPolygon :: read_twkb ( & mut rdr) . map_err ( |_| format ! ( "cannot convert {} to MultiPolygon" , ty) . into ( ) )
229228 }
229+
230+ accepts ! ( BYTEA ) ;
230231}
231232
232233
0 commit comments