|
| 1 | +extern crate geo; |
| 2 | + |
| 3 | +use postgres_protocol::types; |
| 4 | +use self::geo::{Bbox, LineString, 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 | +} |
| 62 | + |
| 63 | +impl FromSql for LineString<f64> { |
| 64 | + fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> { |
| 65 | + if raw.len() < 5 { |
| 66 | + return Err("invalid message length".into()); |
| 67 | + } |
| 68 | + |
| 69 | + // let _ = types::bool_from_sql(&raw[0..1])?; // is path open or closed |
| 70 | + let n_points = types::int4_from_sql(&raw[1..5])? as usize; |
| 71 | + let raw_points = &raw[5..raw.len()]; |
| 72 | + if raw_points.len() != 16 * n_points { |
| 73 | + return Err("invalid message length".into()); |
| 74 | + } |
| 75 | + |
| 76 | + let mut offset = 0; |
| 77 | + let mut points = Vec::with_capacity(n_points); |
| 78 | + for _ in 0..n_points { |
| 79 | + let x = types::float8_from_sql(&raw_points[offset..offset+8])?; |
| 80 | + let y = types::float8_from_sql(&raw_points[offset+8..offset+16])?; |
| 81 | + points.push(Point::new(x, y)); |
| 82 | + offset += 16; |
| 83 | + } |
| 84 | + Ok(LineString(points)) |
| 85 | + } |
| 86 | + |
| 87 | + accepts!(Type::Path); |
| 88 | +} |
| 89 | + |
| 90 | +impl ToSql for LineString<f64> { |
| 91 | + fn to_sql(&self, _: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> { |
| 92 | + let closed = false; // always encode an open path from LineString |
| 93 | + types::bool_to_sql(closed, out); |
| 94 | + types::int4_to_sql(self.0.len() as i32, out); |
| 95 | + for point in &self.0 { |
| 96 | + types::float8_to_sql(point.x(), out); |
| 97 | + types::float8_to_sql(point.y(), out); |
| 98 | + } |
| 99 | + Ok(IsNull::No) |
| 100 | + } |
| 101 | + |
| 102 | + accepts!(Type::Path); |
| 103 | + to_sql_checked!(); |
| 104 | +} |
0 commit comments