diesel/pg/types/
multirange.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use std::io::Write;
use std::ops::Bound;

use crate::deserialize::{self, Defaultable, FromSql};
use crate::expression::bound::Bound as SqlBound;
use crate::expression::AsExpression;
use crate::pg::{Pg, PgTypeMetadata, PgValue};
use crate::query_builder::bind_collector::ByteWrapper;
use crate::serialize::{self, IsNull, Output, ToSql};
use crate::sql_types::*;

// from `SELECT oid, typname FROM pg_catalog.pg_type where typname LIKE '%multirange'`;
macro_rules! multirange_has_sql_type {
    ($ty:ty, $oid:expr, $array_oid:expr) => {
        #[cfg(feature = "postgres_backend")]
        impl HasSqlType<$ty> for Pg {
            fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
                PgTypeMetadata::new($oid, $array_oid)
            }
        }
    };
}
multirange_has_sql_type!(Datemultirange, 4535, 6155);
multirange_has_sql_type!(Int4multirange, 4451, 6150);
multirange_has_sql_type!(Int8multirange, 4536, 6157);
multirange_has_sql_type!(Nummultirange, 4532, 6151);
multirange_has_sql_type!(Tsmultirange, 4533, 6152);
multirange_has_sql_type!(Tstzmultirange, 4534, 6153);

macro_rules! multirange_as_expression {
    ($ty:ty, $sql_type:ty) => {
        #[cfg(feature = "postgres_backend")]
        // this simplifies the macro implementation
        // as some macro calls use this lifetime
        #[allow(clippy::extra_unused_lifetimes)]
        impl<'a, 'b, ST: 'static, T> AsExpression<$sql_type> for $ty {
            type Expression = SqlBound<$sql_type, Self>;
            fn as_expression(self) -> Self::Expression {
                SqlBound::new(self)
            }
        }
    };
}

macro_rules! multirange_as_expressions {
    ($ty:ty) => {
        multirange_as_expression!(&'a [$ty], Multirange<ST>);
        multirange_as_expression!(&'a [$ty], Nullable<Multirange<ST>>);
        multirange_as_expression!(&'a &'b [$ty], Multirange<ST>);
        multirange_as_expression!(&'a &'b [$ty], Nullable<Multirange<ST>>);
        multirange_as_expression!(Vec<$ty>, Multirange<ST>);
        multirange_as_expression!(Vec<$ty>, Nullable<Multirange<ST>>);
        multirange_as_expression!(&'a Vec<$ty>, Multirange<ST>);
        multirange_as_expression!(&'a Vec<$ty>, Nullable<Multirange<ST>>);
        multirange_as_expression!(&'a &'b Vec<$ty>, Multirange<ST>);
        multirange_as_expression!(&'a &'b Vec<$ty>, Nullable<Multirange<ST>>);
    };
}

multirange_as_expressions!((Bound<T>, Bound<T>));
multirange_as_expressions!(std::ops::Range<T>);
multirange_as_expressions!(std::ops::RangeInclusive<T>);
multirange_as_expressions!(std::ops::RangeToInclusive<T>);
multirange_as_expressions!(std::ops::RangeFrom<T>);
multirange_as_expressions!(std::ops::RangeTo<T>);

#[cfg(feature = "postgres_backend")]
impl<T, ST> FromSql<Multirange<ST>, Pg> for Vec<(Bound<T>, Bound<T>)>
where
    T: FromSql<ST, Pg> + Defaultable,
{
    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
        let mut bytes = value.as_bytes();
        let len = bytes.read_u32::<NetworkEndian>()?;

        (0..len)
            .map(|_| {
                let range_size: usize = bytes.read_i32::<NetworkEndian>()?.try_into()?;
                let (range_bytes, new_bytes) = bytes.split_at(range_size);
                bytes = new_bytes;
                FromSql::from_sql(PgValue::new_internal(range_bytes, &value))
            })
            .collect()
    }
}

fn to_sql<'c, ST, T, I>(iter: I, out: &mut Output<'_, '_, Pg>) -> serialize::Result
where
    ST: 'static,
    T: ToSql<ST, Pg> + 'c,
    I: Iterator<Item = (Bound<&'c T>, Bound<&'c T>)> + ExactSizeIterator,
{
    out.write_u32::<NetworkEndian>(iter.len().try_into()?)?;

    let mut buffer = Vec::new();
    for value in iter {
        {
            let mut inner_buffer = Output::new(ByteWrapper(&mut buffer), out.metadata_lookup());
            ToSql::<Range<ST>, Pg>::to_sql(&value, &mut inner_buffer)?;
        }
        let buffer_len: i32 = buffer.len().try_into()?;
        out.write_i32::<NetworkEndian>(buffer_len)?;
        out.write_all(&buffer)?;
        buffer.clear();
    }

    Ok(IsNull::No)
}

#[cfg(feature = "postgres_backend")]
impl<T, ST> ToSql<Multirange<ST>, Pg> for [(Bound<T>, Bound<T>)]
where
    T: ToSql<ST, Pg>,
{
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
        to_sql(self.iter().map(|r| (r.0.as_ref(), r.1.as_ref())), out)
    }
}

#[cfg(feature = "postgres_backend")]
impl<T, ST> ToSql<Multirange<ST>, Pg> for Vec<(Bound<T>, Bound<T>)>
where
    T: ToSql<ST, Pg>,
    [(Bound<T>, Bound<T>)]: ToSql<Multirange<ST>, Pg>,
{
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
        ToSql::<Multirange<ST>, Pg>::to_sql(self.as_slice(), out)
    }
}

use std::ops::RangeBounds;
macro_rules! multirange_std_to_sql {
    ($ty:ty) => {
        #[cfg(feature = "postgres_backend")]
        impl<ST, T> ToSql<Multirange<ST>, Pg> for [$ty]
        where
            ST: 'static,
            T: ToSql<ST, Pg>,
        {
            fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
                to_sql(
                    self.into_iter().map(|r| (r.start_bound(), r.end_bound())),
                    out,
                )
            }
        }

        #[cfg(feature = "postgres_backend")]
        impl<T, ST> ToSql<Multirange<ST>, Pg> for Vec<$ty>
        where
            T: ToSql<ST, Pg>,
            [$ty]: ToSql<Multirange<ST>, Pg>,
        {
            fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
                ToSql::<Multirange<ST>, Pg>::to_sql(self.as_slice(), out)
            }
        }
    };
}

multirange_std_to_sql!(std::ops::Range<T>);
multirange_std_to_sql!(std::ops::RangeInclusive<T>);
multirange_std_to_sql!(std::ops::RangeFrom<T>);
multirange_std_to_sql!(std::ops::RangeTo<T>);
multirange_std_to_sql!(std::ops::RangeToInclusive<T>);

macro_rules! multirange_to_sql_nullable {
    ($ty:ty) => {
        impl<T, ST> ToSql<Nullable<Multirange<ST>>, Pg> for [$ty]
        where
            ST: 'static,
            [$ty]: ToSql<ST, Pg>,
            T: ToSql<ST, Pg>,
        {
            fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
                ToSql::<Multirange<ST>, Pg>::to_sql(self, out)
            }
        }

        impl<T, ST> ToSql<Nullable<Multirange<ST>>, Pg> for Vec<$ty>
        where
            ST: 'static,
            Vec<$ty>: ToSql<ST, Pg>,
            T: ToSql<ST, Pg>,
        {
            fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
                ToSql::<Multirange<ST>, Pg>::to_sql(self, out)
            }
        }
    };
}

multirange_to_sql_nullable!((Bound<T>, Bound<T>));
multirange_to_sql_nullable!(std::ops::Range<T>);
multirange_to_sql_nullable!(std::ops::RangeInclusive<T>);
multirange_to_sql_nullable!(std::ops::RangeFrom<T>);
multirange_to_sql_nullable!(std::ops::RangeTo<T>);
multirange_to_sql_nullable!(std::ops::RangeToInclusive<T>);