Last active
April 10, 2020 14:08
-
-
Save MattiasFestin/44d9068f89ecdefd877d836b4bd62b35 to your computer and use it in GitHub Desktop.
diesel custom types
This file contains hidden or 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
#[derive(Queryable, Insertable, AsChangeset)] | |
pub struct Transaction { | |
pub id: uuid::Uuid, | |
pub created: SystemTime, | |
pub modified: SystemTime, | |
pub deleted: Option<SystemTime>, | |
pub row_version: i64, | |
pub transaction_type_id: i64, | |
pub transaction_direction_id: i64, | |
pub point: PgPoint, | |
pub what: String, | |
pub priority: i64, | |
} |
This file contains hidden or 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
use std::convert::{From, Into}; | |
use std::io::prelude::*; | |
use diesel::deserialize::{self, FromSql}; | |
use diesel::pg::Pg; | |
use diesel::serialize::{self, IsNull, Output, ToSql}; | |
use postgis::ewkb::{Point}; | |
use postgis::ewkb::{EwkbRead, EwkbWrite}; | |
use postgis::ewkb::AsEwkbPoint; | |
#[derive(Debug, SqlType)] | |
#[postgres(type_name = "st_point")] | |
pub struct ST_Point; | |
#[derive(Debug, PartialEq, FromSqlRow, AsExpression)] | |
#[sql_type = "ST_Point"] | |
pub struct PgPoint(pub Point); | |
impl ToSql<PgPoint, Pg> for PgPoint { | |
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result { | |
let point = (*self).0; | |
match out.write_all(point.as_ewkb().to_hex_ewkb().as_bytes()) { | |
Ok(_data) => Ok(IsNull::No), | |
_ => Err("Could not write point to database".into()), | |
} | |
} | |
} | |
impl FromSql<PgPoint, Pg> for PgPoint { | |
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | |
match bytes { | |
Some(data) => { | |
let mut to_write = data.clone(); | |
Ok(PgPoint(Point::read_ewkb(&mut to_write).unwrap())) | |
}, | |
None => Err("Unrecognized data".into()), | |
} | |
} | |
} | |
impl From<PgPoint> for Point { | |
fn from(p: PgPoint) -> Self { | |
p.0 | |
} | |
} | |
impl From<Point> for PgPoint { | |
fn from(p: Point) -> Self { | |
PgPoint(p) | |
} | |
} |
This file contains hidden or 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
CREATE TABLE public.transactions | |
( | |
"id" uuid NOT NULL UNIQUE DEFAULT uuid_generate_v4(), | |
"transaction_type_id" int8 NOT NULL, | |
"transaction_direction_id" int8 NOT NULL, | |
"point" st_point NOT NULL, | |
"what" TEXT NOT NULL, | |
"priority" int8 NOT NULL, | |
PRIMARY KEY ("id") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment