Skip to content

Commit efbf30c

Browse files
committed
Support geo types with georust/geo
1 parent 2536131 commit efbf30c

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,20 @@ types. The driver currently supports the following conversions:
253253
(<a href="#optional-features">optional</a>)
254254
</td>
255255
<td>MACADDR</td>
256+
</tr
257+
<tr>
258+
<td>
259+
<a href="https://github.com/georust/rust-geo">geo::Point&lt;f64&gt;</a>
260+
(<a href="#optional-features">optional</a>)
261+
</td>
262+
<td>POINT</td>
263+
</tr>
264+
<tr>
265+
<td>
266+
<a href="https://github.com/georust/rust-geo">geo::Bbox&lt;f64&gt;</a>
267+
(<a href="#optional-features">optional</a>)
268+
</td>
269+
<td>BOX</td>
256270
</tr>
257271
</tbody>
258272
</table>
@@ -316,3 +330,13 @@ and `FromSql` implementations for `bit-vec`'s `BitVec` type.
316330
[MACADDR](http://www.postgresql.org/docs/9.4/static/datatype-net-types.html#DATATYPE-MACADDR)
317331
support is provided optionally by the `with-eui48` feature, which adds `ToSql`
318332
and `FromSql` implementations for `eui48`'s `MacAddress` type.
333+
334+
### POINT type
335+
336+
[POINT](https://www.postgresql.org/docs/9.4/static/datatype-geometric.html#AEN6799)
337+
support is provided optionally by the `with-geo` feature, which adds `ToSql` and `FromSql` implementations for `geo`'s `Point` type.
338+
339+
### BOX type
340+
341+
[BOX](https://www.postgresql.org/docs/9.4/static/datatype-geometric.html#AEN6883)
342+
support is provided optionally by the `with-geo` feature, which adds `ToSql` and `FromSql` implementations for `geo`'s `Bbox` type.

postgres-shared/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repository = "https://github.com/sfackler/rust-postgres"
1010
with-bit-vec = ["bit-vec"]
1111
with-chrono = ["chrono"]
1212
with-eui48 = ["eui48"]
13+
with-geo = ["geo"]
1314
with-rustc-serialize = ["rustc-serialize"]
1415
with-serde_json = ["serde_json"]
1516
with-time = ["time"]
@@ -24,6 +25,7 @@ postgres-protocol = "0.2"
2425
bit-vec = { version = "0.4", optional = true }
2526
chrono = { version = "0.3", optional = true }
2627
eui48 = { version = "0.1", optional = true }
28+
geo = { version = "0.4", optional = true }
2729
rustc-serialize = { version = "0.3", optional = true }
2830
serde_json = { version = "0.9", optional = true }
2931
time = { version = "0.1.14", optional = true }

postgres-shared/src/types/geo.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
extern crate geo;
2+
3+
use postgres_protocol::types;
4+
use self::geo::{Bbox, Point};
5+
use std::error::Error;
6+
7+
use types::{FromSql, ToSql, IsNull, Type};
8+
9+
impl FromSql for Point<f64> {
10+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> {
11+
if raw.len() != 16 {
12+
return Err("invalid message length".into());
13+
}
14+
15+
let x = types::float8_from_sql(&raw[0..8])?;
16+
let y = types::float8_from_sql(&raw[8..16])?;
17+
Ok(Point::new(x, y))
18+
}
19+
20+
accepts!(Type::Point);
21+
}
22+
23+
impl ToSql for Point<f64> {
24+
fn to_sql(&self, _: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> {
25+
types::float8_to_sql(self.x(), out);
26+
types::float8_to_sql(self.y(), out);
27+
Ok(IsNull::No)
28+
}
29+
30+
accepts!(Type::Point);
31+
to_sql_checked!();
32+
}
33+
34+
impl FromSql for Bbox<f64> {
35+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> {
36+
if raw.len() != 32 {
37+
return Err("invalid message length".into());
38+
}
39+
40+
let xmax = types::float8_from_sql(&raw[0..8])?;
41+
let ymax = types::float8_from_sql(&raw[8..16])?;
42+
let xmin = types::float8_from_sql(&raw[16..24])?;
43+
let ymin = types::float8_from_sql(&raw[24..32])?;
44+
Ok(Bbox{xmax: xmax, ymax: ymax, xmin: xmin, ymin: ymin})
45+
}
46+
47+
accepts!(Type::Box);
48+
}
49+
50+
impl ToSql for Bbox<f64> {
51+
fn to_sql(&self, _: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> {
52+
types::float8_to_sql(self.xmax, out);
53+
types::float8_to_sql(self.ymax, out);
54+
types::float8_to_sql(self.xmin, out);
55+
types::float8_to_sql(self.ymin, out);
56+
Ok(IsNull::No)
57+
}
58+
59+
accepts!(Type::Box);
60+
to_sql_checked!();
61+
}

postgres-shared/src/types/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ mod serde_json;
7373
mod chrono;
7474
#[cfg(feature = "with-eui48")]
7575
mod eui48;
76+
#[cfg(feature = "with-geo")]
77+
mod geo;
7678

7779
mod special;
7880
mod type_gen;

postgres/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ path = "tests/test.rs"
2424
with-bit-vec = ["postgres-shared/with-bit-vec"]
2525
with-chrono = ["postgres-shared/with-chrono"]
2626
with-eui48 = ["postgres-shared/with-eui48"]
27+
with-geo = ["postgres-shared/with-geo"]
2728
with-rustc-serialize = ["postgres-shared/with-rustc-serialize"]
2829
with-serde_json = ["postgres-shared/with-serde_json"]
2930
with-time = ["postgres-shared/with-time"]

tokio-postgres/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ keywords = ["database", "postgres", "postgresql", "sql", "async"]
1313
with-bit-vec = ["postgres-shared/with-bit-vec"]
1414
with-chrono = ["postgres-shared/with-chrono"]
1515
with-eui48 = ["postgres-shared/with-eui48"]
16+
with-geo = ["postgres-shared/with-geo"]
1617
with-rustc-serialize = ["postgres-shared/with-rustc-serialize"]
1718
with-serde_json = ["postgres-shared/with-serde_json"]
1819
with-time = ["postgres-shared/with-time"]

0 commit comments

Comments
 (0)