sqlx_xugu/
value.rs

1use sqlx_core::bytes::Bytes;
2pub(crate) use sqlx_core::value::*;
3use std::borrow::Cow;
4use std::str::from_utf8;
5
6use crate::error::{BoxDynError, UnexpectedNullError};
7use crate::protocol::text::ColumnType;
8use crate::{Xugu, XuguTypeInfo};
9
10/// Implementation of [`Value`] for Xugu.
11#[derive(Clone)]
12pub struct XuguValue {
13    value: Option<Bytes>,
14    type_info: XuguTypeInfo,
15}
16
17/// Implementation of [`ValueRef`] for Xugu.
18#[derive(Clone)]
19pub struct XuguValueRef<'r> {
20    pub(crate) value: Option<&'r [u8]>,
21    pub(crate) row: Option<&'r Vec<Bytes>>,
22    pub(crate) type_info: XuguTypeInfo,
23}
24
25impl<'r> XuguValueRef<'r> {
26    pub(crate) fn as_bytes(&self) -> Result<&'r [u8], BoxDynError> {
27        match &self.value {
28            Some(v) => Ok(v),
29            None => Err(UnexpectedNullError.into()),
30        }
31    }
32
33    pub(crate) fn as_str(&self) -> Result<&'r str, BoxDynError> {
34        Ok(from_utf8(self.as_bytes()?)?)
35    }
36}
37
38impl Value for XuguValue {
39    type Database = Xugu;
40
41    fn as_ref(&self) -> XuguValueRef<'_> {
42        XuguValueRef {
43            value: self.value.as_deref(),
44            row: None,
45            type_info: self.type_info.clone(),
46        }
47    }
48
49    fn type_info(&self) -> Cow<'_, XuguTypeInfo> {
50        Cow::Borrowed(&self.type_info)
51    }
52
53    fn is_null(&self) -> bool {
54        is_null(self.value.as_deref(), &self.type_info)
55    }
56}
57
58/// Returns a slice of self that is equivalent to the given `subset`.
59///
60/// When processing a `Bytes` buffer with other tools, one often gets a
61/// `&[u8]` which is in fact a slice of the `Bytes`, i.e. a subset of it.
62/// This function turns that `&[u8]` into another `Bytes`, as if one had
63/// called `self.slice()` with the offsets that correspond to `subset`.
64///
65/// This operation is `O(1)`.
66///
67/// see [`Bytes::slice_ref`]
68fn slice_ref(bytes: &Bytes, subset: &[u8]) -> Option<Bytes> {
69    // Empty slice and empty Bytes may have their pointers reset
70    // so explicitly allow empty slice to be a subslice of any slice.
71    if subset.is_empty() {
72        return Some(Bytes::new());
73    }
74
75    let bytes_p = bytes.as_ptr() as usize;
76    let bytes_len = bytes.len();
77
78    let sub_p = subset.as_ptr() as usize;
79    let sub_len = subset.len();
80
81    // subset pointer is smaller than self pointer
82    if sub_p < bytes_p {
83        return None;
84    }
85
86    // subset is out of bounds
87    if sub_p + sub_len > bytes_p + bytes_len {
88        return None;
89    }
90
91    let sub_offset = sub_p - bytes_p;
92
93    let sub = bytes.slice(sub_offset..(sub_offset + sub_len));
94    Some(sub)
95}
96
97fn slice_ref_or_copy(row: &[Bytes], value: &[u8]) -> Bytes {
98    for x in row {
99        if let Some(v) = slice_ref(x, value) {
100            return v;
101        }
102    }
103
104    Bytes::copy_from_slice(value)
105}
106
107impl<'r> ValueRef<'r> for XuguValueRef<'r> {
108    type Database = Xugu;
109
110    fn to_owned(&self) -> XuguValue {
111        let value = match (self.row, self.value) {
112            (Some(row), Some(value)) => Some(slice_ref_or_copy(row, value)),
113
114            (None, Some(value)) => Some(Bytes::copy_from_slice(value)),
115
116            _ => None,
117        };
118
119        XuguValue {
120            value,
121            type_info: self.type_info.clone(),
122        }
123    }
124
125    fn type_info(&self) -> Cow<'_, XuguTypeInfo> {
126        Cow::Borrowed(&self.type_info)
127    }
128
129    #[inline]
130    fn is_null(&self) -> bool {
131        is_null(self.value, &self.type_info)
132    }
133}
134
135fn is_null(value: Option<&[u8]>, ty: &XuguTypeInfo) -> bool {
136    if let Some(value) = value {
137        if value.is_empty() {
138            return true;
139        }
140        // zero dates and date times should be treated the same as NULL
141        if matches!(
142            ty.r#type,
143            ColumnType::DATE | ColumnType::TIME | ColumnType::DATETIME
144        ) && value.starts_with(b"\0")
145        {
146            return true;
147        }
148    }
149
150    value.is_none()
151}