odbc_api/buffers/
column_with_indicator.rs

1use crate::{
2    buffers::Resize,
3    fixed_sized::{Bit, Pod},
4    handles::{CData, CDataMut},
5};
6use odbc_sys::{Date, NULL_DATA, Time, Timestamp};
7use std::{
8    ffi::c_void,
9    mem::size_of,
10    ptr::{null, null_mut},
11};
12
13pub type OptF64Column = ColumnWithIndicator<f64>;
14pub type OptF32Column = ColumnWithIndicator<f32>;
15pub type OptDateColumn = ColumnWithIndicator<Date>;
16pub type OptTimestampColumn = ColumnWithIndicator<Timestamp>;
17pub type OptTimeColumn = ColumnWithIndicator<Time>;
18pub type OptI8Column = ColumnWithIndicator<i8>;
19pub type OptI16Column = ColumnWithIndicator<i16>;
20pub type OptI32Column = ColumnWithIndicator<i32>;
21pub type OptI64Column = ColumnWithIndicator<i64>;
22pub type OptU8Column = ColumnWithIndicator<u8>;
23pub type OptBitColumn = ColumnWithIndicator<Bit>;
24
25/// Column buffer for fixed-size type, also binding an indicator buffer to handle NULL.
26#[derive(Debug)]
27pub struct ColumnWithIndicator<T> {
28    values: Vec<T>,
29    indicators: Vec<isize>,
30}
31
32impl<T> ColumnWithIndicator<T>
33where
34    T: Default + Clone,
35{
36    pub fn new(batch_size: usize) -> Self {
37        Self {
38            values: vec![T::default(); batch_size],
39            indicators: vec![NULL_DATA; batch_size],
40        }
41    }
42
43    /// Access the value at a specific row index.
44    ///
45    /// The buffer size is not automatically adjusted to the size of the last row set. It is the
46    /// callers responsibility to ensure, a value has been written to the indexed position by
47    /// [`crate::Cursor::fetch`] using the value bound to the cursor with
48    /// [`crate::Cursor::set_num_result_rows_fetched`].
49    pub fn iter(&self, num_rows: usize) -> NullableSlice<'_, T> {
50        NullableSlice {
51            indicators: &self.indicators[0..num_rows],
52            values: &self.values[0..num_rows],
53        }
54    }
55
56    /// Fills the column with NULL, between From and To
57    pub fn fill_null(&mut self, from: usize, to: usize) {
58        for index in from..to {
59            self.indicators[index] = NULL_DATA;
60        }
61    }
62
63    /// Create a writer which writes to the first `n` elements of the buffer.
64    pub fn writer_n(&mut self, n: usize) -> NullableSliceMut<'_, T> {
65        NullableSliceMut {
66            indicators: &mut self.indicators[0..n],
67            values: &mut self.values[0..n],
68        }
69    }
70
71    /// Maximum number elements which the column may hold.
72    pub fn capacity(&self) -> usize {
73        self.indicators.len()
74    }
75}
76
77/// Iterates over the elements of a column buffer. Returned by
78/// [`crate::buffers::ColumnarBuffer::column`] as part of an [`crate::buffers::AnySlice`].
79#[derive(Debug, Clone, Copy)]
80pub struct NullableSlice<'a, T> {
81    indicators: &'a [isize],
82    values: &'a [T],
83}
84
85impl<'a, T> NullableSlice<'a, T> {
86    /// `true` if the slice has a length of `0`.
87    pub fn is_empty(&self) -> bool {
88        self.values.is_empty()
89    }
90
91    /// Number of entries in this slice of the buffer
92    pub fn len(&self) -> usize {
93        self.values.len()
94    }
95
96    /// Read access to the underlying raw value and indicator buffer.
97    ///
98    /// The number of elements in the buffer is equal to the number of rows returned in the current
99    /// result set. Yet the content of any value, those associated value in the indicator buffer is
100    /// [`crate::sys::NULL_DATA`] is undefined.
101    ///
102    /// This method is useful for writing performant bindings to datastructures with similar binary
103    /// layout, as it allows for using memcopy rather than iterating over individual values.
104    ///
105    /// # Example
106    ///
107    /// ```
108    /// use odbc_api::{buffers::NullableSlice, sys::NULL_DATA};
109    ///
110    /// // Memcopy the values out of the buffer, and make a mask of bools indicating the NULL
111    /// // values.
112    /// fn copy_values_and_make_mask(odbc_slice: NullableSlice<i32>) -> (Vec<i32>, Vec<bool>) {
113    ///     let (values, indicators) = odbc_slice.raw_values();
114    ///     let values = values.to_vec();
115    ///     // Create array of bools indicating null values.
116    ///     let mask: Vec<bool> = indicators
117    ///         .iter()
118    ///         .map(|&indicator| indicator != NULL_DATA)
119    ///         .collect();
120    ///     (values, mask)
121    /// }
122    /// ```
123    pub fn raw_values(&self) -> (&'a [T], &'a [isize]) {
124        (self.values, self.indicators)
125    }
126
127    /// Access the n-th element. `None` if the indicater is `NULL_DATA`.
128    pub fn get(&self, index: usize) -> Option<&'a T> {
129        if self.indicators[index] == NULL_DATA {
130            None
131        } else {
132            Some(&self.values[index])
133        }
134    }
135}
136
137impl<'a, T> Iterator for NullableSlice<'a, T> {
138    type Item = Option<&'a T>;
139
140    fn next(&mut self) -> Option<Self::Item> {
141        if let Some(&ind) = self.indicators.first() {
142            let item = if ind == NULL_DATA {
143                None
144            } else {
145                Some(&self.values[0])
146            };
147            self.indicators = &self.indicators[1..];
148            self.values = &self.values[1..];
149            Some(item)
150        } else {
151            None
152        }
153    }
154}
155
156unsafe impl<T> CData for ColumnWithIndicator<T>
157where
158    T: Pod,
159{
160    fn cdata_type(&self) -> odbc_sys::CDataType {
161        T::C_DATA_TYPE
162    }
163
164    fn indicator_ptr(&self) -> *const isize {
165        self.indicators.as_ptr()
166    }
167
168    fn value_ptr(&self) -> *const c_void {
169        self.values.as_ptr() as *const c_void
170    }
171
172    fn buffer_length(&self) -> isize {
173        size_of::<T>().try_into().unwrap()
174    }
175}
176
177unsafe impl<T> CDataMut for ColumnWithIndicator<T>
178where
179    T: Pod,
180{
181    fn mut_indicator_ptr(&mut self) -> *mut isize {
182        self.indicators.as_mut_ptr()
183    }
184
185    fn mut_value_ptr(&mut self) -> *mut c_void {
186        self.values.as_mut_ptr() as *mut c_void
187    }
188}
189
190unsafe impl<T> CData for Vec<T>
191where
192    T: Pod,
193{
194    fn cdata_type(&self) -> odbc_sys::CDataType {
195        T::C_DATA_TYPE
196    }
197
198    fn indicator_ptr(&self) -> *const isize {
199        null()
200    }
201
202    fn value_ptr(&self) -> *const c_void {
203        self.as_ptr() as *const c_void
204    }
205
206    fn buffer_length(&self) -> isize {
207        size_of::<T>().try_into().unwrap()
208    }
209}
210
211unsafe impl<T> CDataMut for Vec<T>
212where
213    T: Pod,
214{
215    fn mut_indicator_ptr(&mut self) -> *mut isize {
216        null_mut()
217    }
218
219    fn mut_value_ptr(&mut self) -> *mut c_void {
220        self.as_mut_ptr() as *mut c_void
221    }
222}
223
224/// Used to fill a column buffer with an iterator. Returned by
225/// [`crate::ColumnarBulkInserter::column_mut`] as part of an [`crate::buffers::AnySliceMut`].
226#[derive(Debug)]
227pub struct NullableSliceMut<'a, T> {
228    indicators: &'a mut [isize],
229    values: &'a mut [T],
230}
231
232impl<T> NullableSliceMut<'_, T> {
233    /// `true` if the slice has a length of `0`.
234    pub fn is_empty(&self) -> bool {
235        self.values.is_empty()
236    }
237
238    /// Number of entries in this slice of the buffer
239    pub fn len(&self) -> usize {
240        self.values.len()
241    }
242
243    /// Sets the value at the specified index. Use `None` to specify a `NULL` value.
244    pub fn set_cell(&mut self, index: usize, cell: Option<T>) {
245        if let Some(value) = cell {
246            self.indicators[index] = 0;
247            self.values[index] = value;
248        } else {
249            self.indicators[index] = NULL_DATA;
250        }
251    }
252
253    /// Write access to the underlying raw value and indicator buffer.
254    ///
255    /// The number of elements in the buffer is equal to `len`.
256    ///
257    /// This method is useful for writing performant bindings to datastructures with similar binary
258    /// layout, as it allows for using memcopy rather than iterating over individual values.
259    ///
260    /// # Example
261    ///
262    /// ```
263    /// use odbc_api::{buffers::NullableSliceMut, sys::NULL_DATA};
264    ///
265    /// // Memcopy the values into the buffer, and set indicators according to mask
266    /// // values.
267    /// fn copy_values_and_make_mask(
268    ///     new_values: &[i32],
269    ///     mask: &[bool],
270    ///     odbc_slice: &mut NullableSliceMut<i32>)
271    /// {
272    ///     let (values, indicators) = odbc_slice.raw_values();
273    ///     values.copy_from_slice(new_values);
274    ///     // Create array of bools indicating null values.
275    ///     indicators.iter_mut().zip(mask.iter()).for_each(|(indicator, &mask)| {
276    ///         *indicator = if mask {
277    ///             0
278    ///         } else {
279    ///             NULL_DATA
280    ///         }
281    ///     });
282    /// }
283    /// ```
284    pub fn raw_values(&mut self) -> (&mut [T], &mut [isize]) {
285        (self.values, self.indicators)
286    }
287}
288
289impl<T> NullableSliceMut<'_, T> {
290    /// Writes the elements returned by the iterator into the buffer, starting at the beginning.
291    /// Writes elements until the iterator returns `None` or the buffer can not hold more elements.
292    pub fn write(&mut self, it: impl Iterator<Item = Option<T>>) {
293        for (index, item) in it.enumerate().take(self.values.len()) {
294            self.set_cell(index, item)
295        }
296    }
297}
298
299impl<T> Resize for ColumnWithIndicator<T>
300where
301    T: Default + Clone,
302{
303    fn resize(&mut self, new_size: usize) {
304        self.values.resize(new_size, T::default());
305        self.indicators.resize(new_size, NULL_DATA);
306    }
307}
308
309#[cfg(test)]
310mod tests {
311    use crate::buffers::Resize;
312
313    use super::ColumnWithIndicator;
314
315    #[test]
316    fn column_with_indicator_is_resize() {
317        // Given a column with indicator with two elements `1` and `2`
318        let mut column = ColumnWithIndicator::<i32>::new(2);
319        let mut writer = column.writer_n(2);
320        writer.set_cell(0, Some(1));
321        writer.set_cell(1, Some(2));
322
323        // When we resize it to 3 elements
324        column.resize(3);
325
326        // Then the first two elements are still `1` and `2`, and the third is NULL
327        let reader = column.iter(3);
328        assert_eq!(reader.get(0), Some(&1));
329        assert_eq!(reader.get(1), Some(&2));
330        assert_eq!(reader.get(2), None);
331    }
332}