Skip to content

Commit 200a08d

Browse files
committed
graph: Implement FromSql for a couple time related types
1 parent 207e31f commit 200a08d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

graph/src/blockchain/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,9 @@ impl ToSql<Timestamptz, Pg> for BlockTime {
435435
<Timestamp as ToSql<Timestamptz, Pg>>::to_sql(&self.0, out)
436436
}
437437
}
438+
439+
impl FromSql<Timestamptz, Pg> for BlockTime {
440+
fn from_sql(bytes: diesel::pg::PgValue) -> diesel::deserialize::Result<Self> {
441+
<Timestamp as FromSql<Timestamptz, Pg>>::from_sql(bytes).map(|ts| Self(ts))
442+
}
443+
}

graph/src/data/store/scalar/timestamp.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use chrono::{DateTime, Utc};
22
use diesel::deserialize::FromSql;
33
use diesel::pg::PgValue;
44
use diesel::serialize::ToSql;
5+
use diesel::sql_types::Timestamptz;
56
use serde::{self, Deserialize, Serialize};
67
use stable_hash::StableHash;
78

@@ -95,12 +96,19 @@ impl Display for Timestamp {
9596
}
9697
}
9798

98-
impl ToSql<diesel::sql_types::Timestamptz, diesel::pg::Pg> for Timestamp {
99+
impl ToSql<Timestamptz, diesel::pg::Pg> for Timestamp {
99100
fn to_sql<'b>(
100101
&'b self,
101102
out: &mut diesel::serialize::Output<'b, '_, diesel::pg::Pg>,
102103
) -> diesel::serialize::Result {
103-
<_ as ToSql<diesel::sql_types::Timestamptz, _>>::to_sql(&self.0, &mut out.reborrow())
104+
<_ as ToSql<Timestamptz, _>>::to_sql(&self.0, &mut out.reborrow())
105+
}
106+
}
107+
108+
impl FromSql<Timestamptz, diesel::pg::Pg> for Timestamp {
109+
fn from_sql(bytes: PgValue) -> diesel::deserialize::Result<Self> {
110+
let dt = <_ as FromSql<Timestamptz, _>>::from_sql(bytes)?;
111+
Ok(Timestamp(dt))
104112
}
105113
}
106114

0 commit comments

Comments
 (0)