mz_pgrepr/value/
interval.rs1use std::error::Error;
11use std::fmt;
12
13use byteorder::{NetworkEndian, ReadBytesExt};
14use bytes::{BufMut, BytesMut};
15use mz_repr::adt::interval::Interval as ReprInterval;
16use postgres_types::{FromSql, IsNull, ToSql, Type, to_sql_checked};
17
18#[derive(Debug, Clone)]
22pub struct Interval(pub ReprInterval);
23
24impl fmt::Display for Interval {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 self.0.fmt(f)
27 }
28}
29
30impl ToSql for Interval {
31 fn to_sql(
32 &self,
33 _: &Type,
34 out: &mut BytesMut,
35 ) -> Result<IsNull, Box<dyn Error + 'static + Send + Sync>> {
36 out.put_i64(self.0.micros);
43 out.put_i32(self.0.days);
44 out.put_i32(self.0.months);
45 Ok(IsNull::No)
46 }
47
48 fn accepts(ty: &Type) -> bool {
49 matches!(*ty, Type::INTERVAL)
50 }
51
52 to_sql_checked!();
53}
54
55impl<'a> FromSql<'a> for Interval {
56 fn from_sql(_: &Type, mut raw: &'a [u8]) -> Result<Interval, Box<dyn Error + Sync + Send>> {
57 let micros = raw.read_i64::<NetworkEndian>()?;
58 let days = raw.read_i32::<NetworkEndian>()?;
59 let months = raw.read_i32::<NetworkEndian>()?;
60 Ok(Interval(ReprInterval::new(months, days, micros)))
61 }
62
63 fn accepts(ty: &Type) -> bool {
64 matches!(*ty, Type::INTERVAL)
65 }
66}