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
use crate::{pg_sys, AnyNumeric, FromDatum, IntoDatum, Numeric, PgMemoryContexts};

impl FromDatum for AnyNumeric {
    #[inline]
    unsafe fn from_polymorphic_datum(
        datum: pg_sys::Datum,
        is_null: bool,
        _typoid: pg_sys::Oid,
    ) -> Option<Self>
    where
        Self: Sized,
    {
        if is_null {
            None
        } else {
            let numeric = pg_sys::pg_detoast_datum(datum.cast_mut_ptr()) as pg_sys::Numeric;
            let is_copy = !std::ptr::eq(
                numeric.cast::<pg_sys::NumericData>(),
                datum.cast_mut_ptr::<pg_sys::NumericData>(),
            );
            Some(AnyNumeric { inner: numeric, need_pfree: is_copy })
        }
    }

    unsafe fn from_datum_in_memory_context(
        mut memory_context: PgMemoryContexts,
        datum: pg_sys::Datum,
        is_null: bool,
        _typoid: pg_sys::Oid,
    ) -> Option<Self>
    where
        Self: Sized,
    {
        if is_null {
            None
        } else {
            memory_context.switch_to(|_| {
                // copy the Datum into this MemoryContext and then create the AnyNumeric over that
                let copy = pg_sys::pg_detoast_datum_copy(datum.cast_mut_ptr());
                Some(AnyNumeric { inner: copy.cast(), need_pfree: true })
            })
        }
    }
}

impl IntoDatum for AnyNumeric {
    #[inline]
    fn into_datum(mut self) -> Option<pg_sys::Datum> {
        // we're giving it to Postgres so we don't want our drop impl to free the inner pointer
        self.need_pfree = false;
        Some(pg_sys::Datum::from(self.inner))
    }

    #[inline]
    fn type_oid() -> pg_sys::Oid {
        pg_sys::NUMERICOID
    }
}

impl<const P: u32, const S: u32> FromDatum for Numeric<P, S> {
    #[inline]
    unsafe fn from_polymorphic_datum(
        datum: pg_sys::Datum,
        is_null: bool,
        typoid: pg_sys::Oid,
    ) -> Option<Self>
    where
        Self: Sized,
    {
        if is_null {
            None
        } else {
            Some(Numeric(AnyNumeric::from_polymorphic_datum(datum, false, typoid).unwrap()))
        }
    }
}

impl<const P: u32, const S: u32> IntoDatum for Numeric<P, S> {
    #[inline]
    fn into_datum(self) -> Option<pg_sys::Datum> {
        self.0.into_datum()
    }

    #[inline]
    fn type_oid() -> pg_sys::Oid {
        pg_sys::NUMERICOID
    }
}