Skip to main content

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