| 1 |
use bytes::BytesMut; |
| 2 |
use postgres_protocol::types; |
| 3 |
use std::error::Error; |
| 4 |
use time_02::{Date, Duration, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, date, time}; |
| 5 |
|
| 6 |
use crate::{FromSql, IsNull, ToSql, Type}; |
| 7 |
|
| 8 |
#[rustfmt::skip] |
| 9 |
const fn base() -> PrimitiveDateTime { |
| 10 |
PrimitiveDateTime::new(date!(2000-01-01), time!(00:00:00)) |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
fn date_in_range(julian_day: i64) -> bool { |
| 19 |
let min = Date::try_from_ymd(-100_000, 1, 1) |
| 20 |
.expect("year is in range") |
| 21 |
.julian_day(); |
| 22 |
let max = Date::try_from_ymd(100_000, 12, 31) |
| 23 |
.expect("year is in range") |
| 24 |
.julian_day(); |
| 25 |
(min..=max).contains(&julian_day) |
| 26 |
} |
| 27 |
|
| 28 |
impl<'a> FromSql<'a> for PrimitiveDateTime { |
| 29 |
fn from_sql(_: &Type, raw: &[u8]) -> Result<PrimitiveDateTime, Box<dyn Error + Sync + Send>> { |
| 30 |
let t = types::timestamp_from_sql(raw)?; |
| 31 |
|
| 32 |
|
| 33 |
let julian_day = base().date().julian_day() + Duration::microseconds(t).whole_days(); |
| 34 |
if !date_in_range(julian_day - 1) || !date_in_range(julian_day + 1) { |
| 35 |
return Err("value too large to decode".into()); |
| 36 |
} |
| 37 |
Ok(base() + Duration::microseconds(t)) |
| 38 |
} |
| 39 |
|
| 40 |
accepts!(TIMESTAMP); |
| 41 |
} |
| 42 |
|
| 43 |
impl ToSql for PrimitiveDateTime { |
| 44 |
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> { |
| 45 |
let time = match i64::try_from((*self - base()).whole_microseconds()) { |
| 46 |
Ok(time) => time, |
| 47 |
Err(_) => return Err("value too large to transmit".into()), |
| 48 |
}; |
| 49 |
types::timestamp_to_sql(time, w); |
| 50 |
Ok(IsNull::No) |
| 51 |
} |
| 52 |
|
| 53 |
accepts!(TIMESTAMP); |
| 54 |
to_sql_checked!(); |
| 55 |
} |
| 56 |
|
| 57 |
impl<'a> FromSql<'a> for OffsetDateTime { |
| 58 |
fn from_sql(type_: &Type, raw: &[u8]) -> Result<OffsetDateTime, Box<dyn Error + Sync + Send>> { |
| 59 |
let primitive = PrimitiveDateTime::from_sql(type_, raw)?; |
| 60 |
Ok(primitive.assume_utc()) |
| 61 |
} |
| 62 |
|
| 63 |
accepts!(TIMESTAMPTZ); |
| 64 |
} |
| 65 |
|
| 66 |
impl ToSql for OffsetDateTime { |
| 67 |
fn to_sql( |
| 68 |
&self, |
| 69 |
type_: &Type, |
| 70 |
w: &mut BytesMut, |
| 71 |
) -> Result<IsNull, Box<dyn Error + Sync + Send>> { |
| 72 |
let utc_datetime = self.to_offset(UtcOffset::UTC); |
| 73 |
let date = utc_datetime.date(); |
| 74 |
let time = utc_datetime.time(); |
| 75 |
let primitive = PrimitiveDateTime::new(date, time); |
| 76 |
primitive.to_sql(type_, w) |
| 77 |
} |
| 78 |
|
| 79 |
accepts!(TIMESTAMPTZ); |
| 80 |
to_sql_checked!(); |
| 81 |
} |
| 82 |
|
| 83 |
impl<'a> FromSql<'a> for Date { |
| 84 |
fn from_sql(_: &Type, raw: &[u8]) -> Result<Date, Box<dyn Error + Sync + Send>> { |
| 85 |
let jd = types::date_from_sql(raw)?; |
| 86 |
let julian_day = base().date().julian_day() + i64::from(jd); |
| 87 |
if !date_in_range(julian_day) { |
| 88 |
return Err("value too large to decode".into()); |
| 89 |
} |
| 90 |
Ok(base().date() + Duration::days(i64::from(jd))) |
| 91 |
} |
| 92 |
|
| 93 |
accepts!(DATE); |
| 94 |
} |
| 95 |
|
| 96 |
impl ToSql for Date { |
| 97 |
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> { |
| 98 |
let jd = (*self - base().date()).whole_days(); |
| 99 |
let jd = i32::try_from(jd).map_err(|_| "value too large to transmit")?; |
| 100 |
|
| 101 |
types::date_to_sql(jd, w); |
| 102 |
Ok(IsNull::No) |
| 103 |
} |
| 104 |
|
| 105 |
accepts!(DATE); |
| 106 |
to_sql_checked!(); |
| 107 |
} |
| 108 |
|
| 109 |
impl<'a> FromSql<'a> for Time { |
| 110 |
fn from_sql(_: &Type, raw: &[u8]) -> Result<Time, Box<dyn Error + Sync + Send>> { |
| 111 |
let usec = types::time_from_sql(raw)?; |
| 112 |
Ok(time!(00:00:00) + Duration::microseconds(usec)) |
| 113 |
} |
| 114 |
|
| 115 |
accepts!(TIME); |
| 116 |
} |
| 117 |
|
| 118 |
impl ToSql for Time { |
| 119 |
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> { |
| 120 |
let delta = *self - time!(00:00:00); |
| 121 |
let time = match i64::try_from(delta.whole_microseconds()) { |
| 122 |
Ok(time) => time, |
| 123 |
Err(_) => return Err("value too large to transmit".into()), |
| 124 |
}; |
| 125 |
types::time_to_sql(time, w); |
| 126 |
Ok(IsNull::No) |
| 127 |
} |
| 128 |
|
| 129 |
accepts!(TIME); |
| 130 |
to_sql_checked!(); |
| 131 |
} |
| 132 |
|
| 133 |
#[cfg(test)] |
| 134 |
mod test { |
| 135 |
use super::*; |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn date_out_of_range_errors() { |
| 139 |
|
| 140 |
|
| 141 |
let raw = i32::MAX.to_be_bytes(); |
| 142 |
assert!(<Date as FromSql>::from_sql(&Type::DATE, &raw).is_err()); |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn date_in_range_decodes() { |
| 147 |
let raw = 1_000i32.to_be_bytes(); |
| 148 |
assert!(<Date as FromSql>::from_sql(&Type::DATE, &raw).is_ok()); |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn timestamp_out_of_range_errors() { |
| 153 |
let raw = 9_000_000_000_000_000_000i64.to_be_bytes(); |
| 154 |
assert!(<PrimitiveDateTime as FromSql>::from_sql(&Type::TIMESTAMP, &raw).is_err()); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn timestamp_in_range_decodes() { |
| 159 |
let raw = 0i64.to_be_bytes(); |
| 160 |
assert!(<PrimitiveDateTime as FromSql>::from_sql(&Type::TIMESTAMP, &raw).is_ok()); |
| 161 |
} |
| 162 |
} |