Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from myaple/dev/diesel-postgis
Adds diesel support for Ewkb type for postgis
- Loading branch information
Showing
5 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::postgis::postgis_diesel::sql_types::*; | ||
use crate::wkb::Ewkb; | ||
|
||
use byteorder::WriteBytesExt; | ||
|
||
use diesel::deserialize::{self, FromSql}; | ||
use diesel::pg::{self, Pg}; | ||
use diesel::serialize::{self, IsNull, Output, ToSql}; | ||
|
||
pub mod sql_types { | ||
use diesel::query_builder::QueryId; | ||
use diesel::sql_types::SqlType; | ||
|
||
#[derive(SqlType, QueryId)] | ||
#[diesel(postgres_type(name = "geometry"))] | ||
pub struct Geometry; | ||
|
||
#[derive(SqlType, QueryId)] | ||
#[diesel(postgres_type(name = "geography"))] | ||
pub struct Geography; | ||
} | ||
|
||
impl ToSql<Geometry, Pg> for Ewkb { | ||
fn to_sql(&self, out: &mut Output<Pg>) -> serialize::Result { | ||
for ewkb_byte in &self.0 { | ||
out.write_u8(*ewkb_byte)?; | ||
} | ||
Ok(IsNull::No) | ||
} | ||
} | ||
|
||
impl ToSql<Geography, Pg> for Ewkb { | ||
fn to_sql(&self, out: &mut Output<Pg>) -> serialize::Result { | ||
for ewkb_byte in &self.0 { | ||
out.write_u8(*ewkb_byte)?; | ||
} | ||
Ok(IsNull::No) | ||
} | ||
} | ||
|
||
impl FromSql<Geometry, Pg> for Ewkb { | ||
fn from_sql(bytes: pg::PgValue) -> deserialize::Result<Self> { | ||
Ok(Self(bytes.as_bytes().to_vec())) | ||
} | ||
} | ||
|
||
impl FromSql<Geography, Pg> for Ewkb { | ||
fn from_sql(bytes: pg::PgValue) -> deserialize::Result<Self> { | ||
Ok(Self(bytes.as_bytes().to_vec())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters