-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tison <[email protected]>
- Loading branch information
Showing
7 changed files
with
84 additions
and
28 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "sqlx-postgres")] | ||
mod postgres; |
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,69 @@ | ||
use crate::{SignedDuration, Timestamp}; | ||
use core::str::FromStr; | ||
use sqlx::encode::IsNull; | ||
use sqlx::error::BoxDynError; | ||
use sqlx::postgres::types::Oid; | ||
use sqlx::postgres::{ | ||
PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, | ||
}; | ||
use sqlx::{Database, Decode, Encode, Postgres, Type}; | ||
use std::format; | ||
|
||
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.duration_since(postgres_epoch_timestamp()).as_micros(); | ||
let micros = i64::try_from(micros).map_err(|_| { | ||
format!("Timestamp {} out of range for Postgres: {micros}", self) | ||
})?; | ||
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))?; | ||
ts | ||
} | ||
PgValueFormat::Text => { | ||
let s = value.as_str()?; | ||
let ts = Timestamp::from_str(s)?; | ||
ts | ||
} | ||
}) | ||
} | ||
} | ||
|
||
fn postgres_epoch_timestamp() -> Timestamp { | ||
Timestamp::from_str("2000-01-01T00:00:00Z") | ||
.expect("2000-01-01T00:00:00Z is a valid timestamp") | ||
} |