| 1 |
use std::num::NonZeroU32; |
| 2 |
use std::ops::Range; |
| 3 |
|
| 4 |
/// Raw postgres value as received from the database |
| 5 |
#[derive(Clone, Copy)] |
| 6 |
#[allow(missing_debug_implementations)] |
| 7 |
#[cfg(feature = "postgres_backend")] |
| 8 |
pub struct PgValue<'a> { |
| 9 |
raw_value: &'a [u8], |
| 10 |
type_oid_lookup: &'a dyn TypeOidLookup, |
| 11 |
} |
| 12 |
|
| 13 |
/// This is a helper trait to defer a type oid |
| 14 |
/// lookup to a later point in time |
| 15 |
/// |
| 16 |
/// This is mainly used in the `PgConnection` |
| 17 |
/// implementation so that we do not need to call |
| 18 |
/// into libpq if we do not need the type oid. |
| 19 |
/// |
| 20 |
/// Backend implementations based on pure rustc |
| 21 |
/// database connection crates can likely reuse |
| 22 |
/// the implementation for `NonZeroU32` here instead |
| 23 |
/// of providing their own custom implementation |
| 24 |
#[cfg_attr( |
| 25 |
diesel_docsrs, |
| 26 |
doc(cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")) |
| 27 |
)] |
| 28 |
#[allow(unreachable_pub)] |
| 29 |
pub trait TypeOidLookup { |
| 30 |
/// Lookup the type oid for the current value |
| 31 |
fn lookup(&self) -> NonZeroU32; |
| 32 |
} |
| 33 |
|
| 34 |
impl<F> TypeOidLookup for F |
| 35 |
where |
| 36 |
F: Fn() -> NonZeroU32, |
| 37 |
{ |
| 38 |
fn lookup(&self) -> NonZeroU32 { |
| 39 |
(self)() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl TypeOidLookup for PgValue<'_> { |
| 44 |
fn lookup(&self) -> NonZeroU32 { |
| 45 |
self.type_oid_lookup.lookup() |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
impl TypeOidLookup for NonZeroU32 { |
| 50 |
fn lookup(&self) -> NonZeroU32 { |
| 51 |
*self |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl<'a> PgValue<'a> { |
| 56 |
#[cfg(test)] |
| 57 |
pub(crate) fn for_test(raw_value: &'a [u8]) -> Self { |
| 58 |
static FAKE_OID: NonZeroU32 = NonZeroU32::new(42).unwrap(); |
| 59 |
Self { |
| 60 |
raw_value, |
| 61 |
type_oid_lookup: &FAKE_OID, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/// Create a new instance of `PgValue` based on a byte buffer |
| 66 |
/// and a way to receive information about the type of the value |
| 67 |
/// represented by the buffer |
| 68 |
#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")] |
| 69 |
pub fn new(raw_value: &'a [u8], type_oid_lookup: &'a dyn TypeOidLookup) -> Self { |
| 70 |
Self::new_internal(raw_value, type_oid_lookup) |
| 71 |
} |
| 72 |
|
| 73 |
pub(in crate::pg) fn new_internal( |
| 74 |
raw_value: &'a [u8], |
| 75 |
type_oid_lookup: &'a dyn TypeOidLookup, |
| 76 |
) -> Self { |
| 77 |
Self { |
| 78 |
raw_value, |
| 79 |
type_oid_lookup, |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/// Get the underlying raw byte representation |
| 84 |
pub fn as_bytes(&self) -> &'a [u8] { |
| 85 |
self.raw_value |
| 86 |
} |
| 87 |
|
| 88 |
/// Get the type oid of this value |
| 89 |
pub fn get_oid(&self) -> NonZeroU32 { |
| 90 |
self.type_oid_lookup.lookup() |
| 91 |
} |
| 92 |
|
| 93 |
pub(crate) fn subslice(&self, range: Range<usize>) -> Self { |
| 94 |
Self { |
| 95 |
raw_value: &self.raw_value[range], |
| 96 |
..*self |
| 97 |
} |
| 98 |
} |
| 99 |
} |