Skip to content

Commit

Permalink
arr support
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Mar 9, 2024
1 parent 84e972e commit 2defcbe
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/impl_sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Monotonic, Timestamp};
use sqlx::{
encode::IsNull,
error::BoxDynError,
postgres::{PgArgumentBuffer, PgTypeInfo, PgValueRef},
postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef},
sqlite::{SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef},
Decode, Encode, Postgres, Sqlite, Type,
};
Expand Down Expand Up @@ -35,6 +35,8 @@ impl<'r> Decode<'r, Sqlite> for Timestamp {
}
}

const J2000_EPOCH_US: i64 = 946_684_800_000_000;

impl Type<Postgres> for Timestamp {
fn type_info() -> PgTypeInfo {
PgTypeInfo::with_name("TIMESTAMPTZ")
Expand All @@ -43,7 +45,16 @@ impl Type<Postgres> for Timestamp {
*ty == PgTypeInfo::with_name("TIMESTAMPTZ") || *ty == PgTypeInfo::with_name("TIMESTAMP")
}
}
const J2000_EPOCH_US: i64 = 946_684_800_000_000;

impl PgHasArrayType for Timestamp {
fn array_type_info() -> PgTypeInfo {
PgTypeInfo::with_name("_TIMESTAMPTZ")
}

fn array_compatible(ty: &PgTypeInfo) -> bool {
*ty == PgTypeInfo::with_name("_TIMESTAMPTZ") || *ty == PgTypeInfo::with_name("_TIMESTAMP")
}
}

impl Encode<'_, Postgres> for Timestamp {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
Expand All @@ -61,6 +72,8 @@ impl<'r> Decode<'r, Postgres> for Timestamp {
}
}

// Monotonic

impl Type<Sqlite> for Monotonic {
fn type_info() -> SqliteTypeInfo {
<i64 as Type<Sqlite>>::type_info()
Expand All @@ -87,16 +100,18 @@ impl<'r> Decode<'r, Sqlite> for Monotonic {
}
}

// Monotonic

impl Type<Postgres> for Monotonic {
fn type_info() -> PgTypeInfo {
PgTypeInfo::with_name("INT8")
}
fn compatible(ty: &PgTypeInfo) -> bool {
*ty == PgTypeInfo::with_name("INT8") || *ty == PgTypeInfo::with_name("BIGINT")
}

impl PgHasArrayType for Monotonic {
fn array_type_info() -> PgTypeInfo {
PgTypeInfo::with_name("_INT8")
}
}

impl Encode<'_, Postgres> for Monotonic {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
let us = i64::try_from(self.as_nanos()).expect("timestamp too large");
Expand All @@ -106,6 +121,7 @@ impl Encode<'_, Postgres> for Monotonic {
std::mem::size_of::<i64>()
}
}

impl<'r> Decode<'r, Postgres> for Monotonic {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
let ns: i64 = Decode::<Postgres>::decode(value)?;
Expand Down

0 comments on commit 2defcbe

Please sign in to comment.