Skip to content

Commit 90398fc

Browse files
committed
graph: Implement FromSql for a couple time related types
1 parent 60d36fe commit 90398fc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use chrono::{DateTime, Utc};
2+
use diesel::deserialize::FromSql;
3+
use diesel::pg::PgValue;
24
use diesel::serialize::ToSql;
5+
use diesel::sql_types::Timestamptz;
36
use serde::{self, Deserialize, Serialize};
47
use stable_hash::StableHash;
58

@@ -93,12 +96,19 @@ impl Display for Timestamp {
9396
}
9497
}
9598

96-
impl ToSql<diesel::sql_types::Timestamptz, diesel::pg::Pg> for Timestamp {
99+
impl ToSql<Timestamptz, diesel::pg::Pg> for Timestamp {
97100
fn to_sql<'b>(
98101
&'b self,
99102
out: &mut diesel::serialize::Output<'b, '_, diesel::pg::Pg>,
100103
) -> diesel::serialize::Result {
101-
<_ 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))
102112
}
103113
}
104114

0 commit comments

Comments
 (0)