Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support jiff v0.2 #1217

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions postgres-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ with-eui48-1 = ["eui48-1"]
with-geo-types-0_6 = ["geo-types-06"]
with-geo-types-0_7 = ["geo-types-0_7"]
with-jiff-0_1 = ["jiff-01"]
with-jiff-0_2 = ["jiff-02"]
with-serde_json-1 = ["serde-1", "serde_json-1"]
with-smol_str-01 = ["smol_str-01"]
with-uuid-0_8 = ["uuid-08"]
Expand Down Expand Up @@ -50,6 +51,7 @@ eui48-1 = { version = "1.0", package = "eui48", optional = true, default-feature
geo-types-06 = { version = "0.6", package = "geo-types", optional = true }
geo-types-0_7 = { version = "0.7", package = "geo-types", optional = true }
jiff-01 = { version = "0.1", package = "jiff", optional = true }
jiff-02 = { version = "0.2", package = "jiff", optional = true }
serde-1 = { version = "1.0", package = "serde", optional = true }
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
uuid-08 = { version = "0.8", package = "uuid", optional = true }
Expand Down
141 changes: 141 additions & 0 deletions postgres-types/src/jiff_02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use bytes::BytesMut;
use jiff_02::{
civil::{Date, DateTime, Time},
Span, SpanRound, Timestamp, Unit,
};
use postgres_protocol::types;
use std::error::Error;

use crate::{FromSql, IsNull, ToSql, Type};

const fn base() -> DateTime {
DateTime::constant(2000, 1, 1, 0, 0, 0, 0)
}

/// The number of seconds from the Unix epoch to 2000-01-01 00:00:00 UTC.
const PG_EPOCH: i64 = 946684800;

fn base_ts() -> Timestamp {
Timestamp::new(PG_EPOCH, 0).unwrap()
}

fn round_us<'a>() -> SpanRound<'a> {
SpanRound::new().largest(Unit::Microsecond)
}

fn decode_err<E>(_e: E) -> Box<dyn Error + Sync + Send>
where
E: Error,
{
"value too large to decode".into()
}

fn transmit_err<E>(_e: E) -> Box<dyn Error + Sync + Send>
where
E: Error,
{
"value too large to transmit".into()
}

impl<'a> FromSql<'a> for DateTime {
fn from_sql(_: &Type, raw: &[u8]) -> Result<DateTime, Box<dyn Error + Sync + Send>> {
let v = types::timestamp_from_sql(raw)?;
Span::new()
.try_microseconds(v)
.and_then(|s| base().checked_add(s))
.map_err(decode_err)
}

accepts!(TIMESTAMP);
}

impl ToSql for DateTime {
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
let v = self
.since(base())
.and_then(|s| s.round(round_us()))
.map_err(transmit_err)?
.get_microseconds();
types::timestamp_to_sql(v, w);
Ok(IsNull::No)
}

accepts!(TIMESTAMP);
to_sql_checked!();
}

impl<'a> FromSql<'a> for Timestamp {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Timestamp, Box<dyn Error + Sync + Send>> {
let v = types::timestamp_from_sql(raw)?;
Span::new()
.try_microseconds(v)
.and_then(|s| base_ts().checked_add(s))
.map_err(decode_err)
}

accepts!(TIMESTAMPTZ);
}

impl ToSql for Timestamp {
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
let v = self
.since(base_ts())
.and_then(|s| s.round(round_us()))
.map_err(transmit_err)?
.get_microseconds();
types::timestamp_to_sql(v, w);
Ok(IsNull::No)
}

accepts!(TIMESTAMPTZ);
to_sql_checked!();
}

impl<'a> FromSql<'a> for Date {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Date, Box<dyn Error + Sync + Send>> {
let v = types::date_from_sql(raw)?;
Span::new()
.try_days(v)
.and_then(|s| base().date().checked_add(s))
.map_err(decode_err)
}
accepts!(DATE);
}

impl ToSql for Date {
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
let v = self.since(base().date()).map_err(transmit_err)?.get_days();
types::date_to_sql(v, w);
Ok(IsNull::No)
}

accepts!(DATE);
to_sql_checked!();
}

impl<'a> FromSql<'a> for Time {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Time, Box<dyn Error + Sync + Send>> {
let v = types::time_from_sql(raw)?;
Span::new()
.try_microseconds(v)
.and_then(|s| Time::midnight().checked_add(s))
.map_err(decode_err)
}

accepts!(TIME);
}

impl ToSql for Time {
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
let v = self
.since(Time::midnight())
.and_then(|s| s.round(round_us()))
.map_err(transmit_err)?
.get_microseconds();
types::time_to_sql(v, w);
Ok(IsNull::No)
}

accepts!(TIME);
to_sql_checked!();
}
2 changes: 2 additions & 0 deletions postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ mod geo_types_06;
mod geo_types_07;
#[cfg(feature = "with-jiff-0_1")]
mod jiff_01;
#[cfg(feature = "with-jiff-0_2")]
mod jiff_02;
#[cfg(feature = "with-serde_json-1")]
mod serde_json_1;
#[cfg(feature = "with-smol_str-01")]
Expand Down
2 changes: 2 additions & 0 deletions tokio-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ with-eui48-1 = ["postgres-types/with-eui48-1"]
with-geo-types-0_6 = ["postgres-types/with-geo-types-0_6"]
with-geo-types-0_7 = ["postgres-types/with-geo-types-0_7"]
with-jiff-0_1 = ["postgres-types/with-jiff-0_1"]
with-jiff-0_2 = ["postgres-types/with-jiff-0_2"]
with-serde_json-1 = ["postgres-types/with-serde_json-1"]
with-smol_str-01 = ["postgres-types/with-smol_str-01"]
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
Expand Down Expand Up @@ -85,6 +86,7 @@ eui48-1 = { version = "1.0", package = "eui48", default-features = false }
geo-types-06 = { version = "0.6", package = "geo-types" }
geo-types-07 = { version = "0.7", package = "geo-types" }
jiff-01 = { version = "0.1", package = "jiff" }
jiff-02 = { version = "0.2", package = "jiff" }
serde-1 = { version = "1.0", package = "serde" }
serde_json-1 = { version = "1.0", package = "serde_json" }
smol_str-01 = { version = "0.1", package = "smol_str" }
Expand Down