1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#[macro_use]
extern crate diesel;

use std::fmt::Debug;
use std::io::Cursor;

use diesel::backend::Backend;
use diesel::deserialize::{self, FromSql};
use diesel::pg::Pg;
use diesel::serialize::{self, IsNull, Output, ToSql};
use postgis::ewkb::{AsEwkbLineString, AsEwkbPoint, AsEwkbPolygon, EwkbRead, EwkbWrite};
use postgis::*;

use sql_types::*;

pub mod sql_types;

/// Container for a `postgis::ewkb::Point`, use that structure in `Insertable` or `Queryable` struct.
/// ```
/// #[macro_use] extern crate diesel;
/// use postgis_diesel::PointC;
/// use postgis::ewkb::Point;
/// #[derive(Queryable)]
/// struct PointExample {
///     id: i32,
///     point: PointC<Point>,
/// }
/// ```
#[derive(Copy, Clone, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type = "Geometry"]
pub struct PointC<T> {
    pub v: T,
}

/// Container for a `postgis::ewkb::LineStringT`, use that structure in `Insertable` or `Queryable` struct.
/// ```
/// #[macro_use] extern crate diesel;
/// use postgis_diesel::LineStringC;
/// use postgis::ewkb::{Point, LineStringT};
/// #[derive(Queryable)]
/// struct PointExample {
///     id: i32,
///     point: LineStringC<LineStringT<Point>>,
/// }
/// ```
#[derive(Clone, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type = "Geometry"]
pub struct LineStringC<T> {
    pub v: T,
}

/// Container for a `postgis::ewkb::PolygonT`, use that structure in `Insertable` or `Queryable` struct.
/// ```
/// #[macro_use] extern crate diesel;
/// use postgis_diesel::PolygonC;
/// use postgis::ewkb::{Point, PolygonT};
/// #[derive(Queryable)]
/// struct PointExample {
///     id: i32,
///     point: PolygonC<PolygonT<Point>>,
/// }
/// ```
#[derive(Clone, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type = "Geometry"]
pub struct PolygonC<T> {
    pub v: T,
}

/// Container for a `postgis::ewkb::MultiPointT`, use that structure in `Insertable` or `Queryable` struct.
/// ```
/// #[macro_use] extern crate diesel;
/// use postgis_diesel::MultiPointC;
/// use postgis::ewkb::{Point, MultiPointT};
/// #[derive(Queryable)]
/// struct PointExample {
///     id: i32,
///     point: MultiPointC<MultiPointT<Point>>,
/// }
/// ```
#[derive(Clone, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type = "Geometry"]
pub struct MultiPointC<T> {
    pub v: T,
}

/// Container for a `postgis::ewkb::MultiLineStringT`, use that structure in `Insertable` or `Queryable` struct.
/// ```
/// #[macro_use] extern crate diesel;
/// use postgis_diesel::MultiLineStringC;
/// use postgis::ewkb::{Point, MultiLineStringT};
/// #[derive(Queryable)]
/// struct PointExample {
///     id: i32,
///     point: MultiLineStringC<MultiLineStringT<Point>>,
/// }
/// ```
#[derive(Clone, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type = "Geometry"]
pub struct MultiLineStringC<T> {
    pub v: T,
}

/// Container for a `postgis::ewkb::MultiPolygonT`, use that structure in `Insertable` or `Queryable` struct.
/// ```
/// #[macro_use] extern crate diesel;
/// use postgis_diesel::MultiPolygonC;
/// use postgis::ewkb::{Point, MultiPolygonT};
/// #[derive(Queryable)]
/// struct PointExample {
///     id: i32,
///     point: MultiPolygonC<MultiPolygonT<Point>>,
/// }
/// ```
#[derive(Clone, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type = "Geometry"]
pub struct MultiPolygonC<T> {
    pub v: T,
}

/// Container for a `postgis::ewkb::GeometryCollectionT`, use that structure in `Insertable` or `Queryable` struct.
/// ```
/// #[macro_use] extern crate diesel;
/// use postgis_diesel::GeometryCollectionC;
/// use postgis::ewkb::{Point, GeometryCollectionT};
/// #[derive(Queryable)]
/// struct PointExample {
///     id: i32,
///     point: GeometryCollectionC<GeometryCollectionT<Point>>,
/// }
/// ```
#[derive(Clone, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type = "Geometry"]
pub struct GeometryCollectionC<T> {
    pub v: T,
}

macro_rules! impl_from_sql {
    ($p:ident, $ps:literal, $s:ident) => {
        fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
            let bytes = not_none!(bytes);
            let mut r = Cursor::new(bytes);
            let geom = ewkb::GeometryT::read_ewkb(&mut r)?;
            return match geom {
                postgis::ewkb::GeometryT::$p(v) => Ok($s { v }),
                _ => Err(format!("Geometry is not a {}", $ps).into()),
            };
        }
    };
}

macro_rules! impl_to_sql {
    () => {
        fn to_sql<W: std::io::Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
            self.v.as_ewkb().write_ewkb(out)?;
            Ok(IsNull::No)
        }
    };
}

impl<P> FromSql<Geometry, Pg> for PointC<P>
where
    P: postgis::Point + EwkbRead + Debug,
{
    impl_from_sql!(Point, "Point", PointC);
}

impl<P> ToSql<Geometry, Pg> for PointC<P>
where
    P: postgis::Point + for<'a> AsEwkbPoint<'a> + Debug,
{
    impl_to_sql!();
}

macro_rules! impl_from_sql_trait {
    ($p:ident, $pb:ident, $ps:literal, $s:ident) => {
        impl<P> FromSql<Geometry, Pg> for $s<ewkb::$pb<P>>
        where
            P: postgis::Point + EwkbRead + Debug,
        {
            impl_from_sql!($p, $ps, $s);
        }
    };
}

macro_rules! impl_to_sql_trait {
    ($pb:ident, $s:ident) => {
        impl<P> ToSql<Geometry, Pg> for $s<ewkb::$pb<P>>
        where
            P: postgis::Point + for<'a> AsEwkbPoint<'a> + EwkbRead + Debug,
        {
            impl_to_sql!();
        }
    };
}

impl_to_sql_trait!(PolygonT, PolygonC);
impl_to_sql_trait!(LineStringT, LineStringC);

impl_from_sql_trait!(LineString, LineStringT, "LineString", LineStringC);
impl_from_sql_trait!(Polygon, PolygonT, "Polygon", PolygonC);
impl_from_sql_trait!(MultiPoint, MultiPointT, "MultiPoint", MultiPointC);
impl_from_sql_trait!(
    MultiLineString,
    MultiLineStringT,
    "MultiLineString",
    MultiLineStringC
);
impl_from_sql_trait!(MultiPolygon, MultiPolygonT, "MultiPolygon", MultiPolygonC);
impl_from_sql_trait!(
    GeometryCollection,
    GeometryCollectionT,
    "GeometryCollection",
    GeometryCollectionC
);