-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement integration as a separate crate
Signed-off-by: tison <[email protected]>
- Loading branch information
Showing
3 changed files
with
97 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,34 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[cfg(feature = "postgres")] | ||
mod postgres; | ||
mod postgres; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct Timestamp(pub jiff::Timestamp); | ||
|
||
impl From<Timestamp> for jiff::Timestamp { | ||
fn from(ts: Timestamp) -> Self { | ||
ts.0 | ||
} | ||
} | ||
|
||
impl From<jiff::Timestamp> for Timestamp { | ||
fn from(ts: jiff::Timestamp) -> Self { | ||
Self(ts) | ||
} | ||
} | ||
|
||
impl Deref for Timestamp { | ||
type Target = jiff::Timestamp; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for Timestamp { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} |
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,62 @@ | ||
use std::str::FromStr; | ||
use sqlx::{Database, Decode, Encode, Postgres, Type}; | ||
use sqlx::encode::IsNull; | ||
use sqlx::error::BoxDynError; | ||
use sqlx::postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat}; | ||
use sqlx::postgres::types::Oid; | ||
use jiff::SignedDuration; | ||
use crate::Timestamp; | ||
|
||
impl Type<Postgres> for Timestamp { | ||
fn type_info() -> PgTypeInfo { | ||
// 1184 => PgType::Timestamptz | ||
PgTypeInfo::with_oid(Oid(1184)) | ||
} | ||
} | ||
|
||
impl PgHasArrayType for Timestamp { | ||
fn array_type_info() -> PgTypeInfo { | ||
// 1185 => PgType::TimestamptzArray | ||
PgTypeInfo::with_oid(Oid(1185)) | ||
} | ||
} | ||
|
||
impl Encode<'_, Postgres> for Timestamp { | ||
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { | ||
// TIMESTAMP is encoded as the microseconds since the epoch | ||
let micros = self | ||
.0 | ||
.duration_since(postgres_epoch_timestamp()) | ||
.as_micros(); | ||
let micros = i64::try_from(micros) | ||
.map_err(|_| format!("Timestamp {} out of range for Postgres: {micros}", self.0))?; | ||
Encode::<Postgres>::encode(micros, buf) | ||
} | ||
|
||
fn size_hint(&self) -> usize { | ||
size_of::<i64>() | ||
} | ||
} | ||
|
||
impl<'r> Decode<'r, Postgres> for Timestamp { | ||
fn decode(value: <Postgres as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> { | ||
Ok(match value.format() { | ||
PgValueFormat::Binary => { | ||
// TIMESTAMP is encoded as the microseconds since the epoch | ||
let us = Decode::<Postgres>::decode(value)?; | ||
let ts = postgres_epoch_timestamp().checked_add(SignedDuration::from_micros(us))?; | ||
Timestamp(ts) | ||
} | ||
PgValueFormat::Text => { | ||
let s = value.as_str()?; | ||
let ts = jiff::Timestamp::from_str(s)?; | ||
Timestamp(ts) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
fn postgres_epoch_timestamp() -> jiff::Timestamp { | ||
jiff::Timestamp::from_str("2000-01-01T00:00:00Z") | ||
.expect("2000-01-01T00:00:00Z is a valid timestamp") | ||
} |