Skip to main content

odbc_api/buffers/
text_column.rs

1use crate::{
2    DataType, Error,
3    buffers::Resize,
4    columnar_bulk_inserter::BoundInputSlice,
5    error::TooLargeBufferSize,
6    handles::{
7        ASSUMED_MAX_LENGTH_OF_VARCHAR, ASSUMED_MAX_LENGTH_OF_W_VARCHAR, CData, CDataMut,
8        HasDataType, Statement, StatementRef,
9    },
10};
11
12use super::{ColumnBuffer, Indicator};
13
14use log::trace;
15use odbc_sys::{CDataType, NULL_DATA};
16use std::{cmp::min, ffi::c_void, mem::size_of, num::NonZeroUsize, panic};
17use widestring::U16Str;
18
19/// A column buffer for character data. The actual encoding used may depend on your system locale.
20pub type CharColumn = TextColumn<u8>;
21
22/// This buffer uses wide characters which implies UTF-16 encoding. UTF-8 encoding is preferable for
23/// most applications, but contrary to its sibling [`crate::buffers::CharColumn`] this buffer type's
24/// implied encoding does not depend on the system locale.
25pub type WCharColumn = TextColumn<u16>;
26
27/// A buffer intended to be bound to a column of a cursor. Elements of the buffer will contain a
28/// variable amount of characters up to a maximum string length. Since most SQL types have a string
29/// representation this buffer can be bound to a column of almost any type, ODBC driver and driver
30/// manager should take care of the conversion. Since elements of this type have variable length, an
31/// indicator buffer needs to be bound, whether the column is nullable or not.
32///
33/// Character type `C` is intended to be either `u8` or `u16`.
34#[derive(Debug)]
35pub struct TextColumn<C> {
36    /// Maximum text length without terminating zero.
37    max_str_len: usize,
38    /// All the characters of all the elements in the buffer. The first character of the n-th
39    /// element is at index `n * (max_str_len + 1)`.
40    values: Vec<C>,
41    /// Elements in this buffer are either `NULL_DATA` or hold the length of the element in value
42    /// with the same index. Please note that this value may be larger than `max_str_len` if the
43    /// text has been truncated.
44    indicators: Vec<isize>,
45}
46
47impl<C> TextColumn<C> {
48    /// This will allocate a value and indicator buffer for `batch_size` elements. Each value may
49    /// have a maximum length of `max_str_len`. This implies that `max_str_len` is increased by
50    /// one in order to make space for the null terminating zero at the end of strings. Uses a
51    /// fallible allocation for creating the buffer. In applications often the `max_str_len` size
52    /// of the buffer, might be directly inspired by the maximum size of the type, as reported, by
53    /// ODBC. Which might get exceedingly large for types like VARCHAR(MAX)
54    pub fn try_new(batch_size: usize, max_str_len: usize) -> Result<Self, TooLargeBufferSize>
55    where
56        C: Default + Copy,
57    {
58        // Element size is +1 to account for terminating zero
59        let element_size = max_str_len + 1;
60        let len = element_size * batch_size;
61        let mut values = Vec::new();
62        values
63            .try_reserve_exact(len)
64            .map_err(|_| TooLargeBufferSize {
65                num_elements: batch_size,
66                // We want the element size in bytes
67                element_size: element_size * size_of::<C>(),
68            })?;
69        values.resize(len, C::default());
70        Ok(TextColumn {
71            max_str_len,
72            values,
73            indicators: vec![0; batch_size],
74        })
75    }
76
77    /// This will allocate a value and indicator buffer for `batch_size` elements. Each value may
78    /// have a maximum length of `max_str_len`. This implies that `max_str_len` is increased by
79    /// one in order to make space for the null terminating zero at the end of strings. All
80    /// indicators are set to [`crate::sys::NULL_DATA`] by default.
81    pub fn new(batch_size: usize, max_str_len: usize) -> Self
82    where
83        C: Default + Copy,
84    {
85        // Element size is +1 to account for terminating zero
86        let element_size = max_str_len + 1;
87        let len = element_size * batch_size;
88        let mut values = Vec::new();
89        values.reserve_exact(len);
90        values.resize(len, C::default());
91        TextColumn {
92            max_str_len,
93            values,
94            indicators: vec![NULL_DATA; batch_size],
95        }
96    }
97
98    /// Bytes of string at the specified position. Includes interior nuls, but excludes the
99    /// terminating nul.
100    ///
101    /// The column buffer does not know how many elements were in the last row group, and therefore
102    /// can not guarantee the accessed element to be valid and in a defined state. It also can not
103    /// panic on accessing an undefined element. It will panic however if `row_index` is larger or
104    /// equal to the maximum number of elements in the buffer.
105    pub fn value_at(&self, row_index: usize) -> Option<&[C]> {
106        self.content_length_at(row_index).map(|length| {
107            let offset = row_index * (self.max_str_len + 1);
108            &self.values[offset..offset + length]
109        })
110    }
111
112    /// Maximum length of elements
113    pub fn max_len(&self) -> usize {
114        self.max_str_len
115    }
116
117    /// Indicator value at the specified position. Useful to detect truncation of data.
118    ///
119    /// The column buffer does not know how many elements were in the last row group, and therefore
120    /// can not guarantee the accessed element to be valid and in a defined state. It also can not
121    /// panic on accessing an undefined element. It will panic however if `row_index` is larger or
122    /// equal to the maximum number of elements in the buffer.
123    pub fn indicator_at(&self, row_index: usize) -> Indicator {
124        Indicator::from_isize(self.indicators[row_index])
125    }
126
127    /// Length of value at the specified position. This is different from an indicator as it refers
128    /// to the length of the value in the buffer, not to the length of the value in the datasource.
129    /// The two things are different for truncated values.
130    pub fn content_length_at(&self, row_index: usize) -> Option<usize> {
131        match self.indicator_at(row_index) {
132            Indicator::Null => None,
133            // Seen no total in the wild then binding shorter buffer to fixed sized CHAR in MSSQL.
134            Indicator::NoTotal => Some(self.max_str_len),
135            Indicator::Length(length_in_bytes) => {
136                let length_in_chars = length_in_bytes / size_of::<C>();
137                let length = min(self.max_str_len, length_in_chars);
138                Some(length)
139            }
140        }
141    }
142
143    /// Finds an indiactor larger than the maximum element size in the range [0, num_rows).
144    ///
145    /// After fetching data we may want to know if any value has been truncated due to the buffer
146    /// not being able to hold elements of that size. This method checks the indicator buffer
147    /// element wise.
148    pub fn has_truncated_values(&self, num_rows: usize) -> Option<Indicator> {
149        let max_bin_length = self.max_str_len * size_of::<C>();
150        self.indicators
151            .iter()
152            .copied()
153            .take(num_rows)
154            .find_map(|indicator| {
155                let indicator = Indicator::from_isize(indicator);
156                indicator.is_truncated(max_bin_length).then_some(indicator)
157            })
158    }
159
160    /// Changes the maximum string length the buffer can hold. This operation is useful if you find
161    /// an unexpected large input string during insertion.
162    ///
163    /// This is however costly, as not only does the new buffer have to be allocated, but all values
164    /// have to copied from the old to the new buffer.
165    ///
166    /// This method could also be used to reduce the maximum string length, which would truncate
167    /// strings in the process.
168    ///
169    /// This method does not adjust indicator buffers as these might hold values larger than the
170    /// maximum string length.
171    ///
172    /// # Parameters
173    ///
174    /// * `new_max_str_len`: New maximum string length without terminating zero.
175    /// * `num_rows`: Number of valid rows currently stored in this buffer.
176    pub fn resize_max_str(&mut self, new_max_str_len: usize, num_rows: usize)
177    where
178        C: Default + Copy,
179    {
180        #[cfg(not(feature = "structured_logging"))]
181        trace!(
182            "Rebinding text column buffer with {} elements. Maximum string length {} => {}",
183            num_rows, self.max_str_len, new_max_str_len
184        );
185        #[cfg(feature = "structured_logging")]
186        trace!(
187            target: "odbc_api",
188            num_rows = num_rows,
189            old_max_str_len = self.max_str_len,
190            new_max_str_len = new_max_str_len;
191            "Text column buffer resized"
192        );
193
194        let batch_size = self.indicators.len();
195        // Allocate a new buffer large enough to hold a batch of strings with maximum length.
196        let mut new_values = vec![C::default(); (new_max_str_len + 1) * batch_size];
197        // Copy values from old to new buffer.
198        let max_copy_length = min(self.max_str_len, new_max_str_len);
199        for ((&indicator, old_value), new_value) in self
200            .indicators
201            .iter()
202            .zip(self.values.chunks_exact_mut(self.max_str_len + 1))
203            .zip(new_values.chunks_exact_mut(new_max_str_len + 1))
204            .take(num_rows)
205        {
206            match Indicator::from_isize(indicator) {
207                Indicator::Null => (),
208                Indicator::NoTotal => {
209                    // There is no good choice here in case we are expanding the buffer. Since
210                    // NO_TOTAL indicates that we use the entire buffer, but in truth it would now
211                    // be padded with 0. I currently cannot think of any use case there it would
212                    // matter.
213                    new_value[..max_copy_length].clone_from_slice(&old_value[..max_copy_length]);
214                }
215                Indicator::Length(num_bytes_len) => {
216                    let num_bytes_to_copy = min(num_bytes_len / size_of::<C>(), max_copy_length);
217                    new_value[..num_bytes_to_copy].copy_from_slice(&old_value[..num_bytes_to_copy]);
218                }
219            }
220        }
221        self.values = new_values;
222        self.max_str_len = new_max_str_len;
223    }
224
225    /// Sets the value of the buffer at index at Null or the specified binary Text. This method will
226    /// panic on out of bounds index, or if input holds a text which is larger than the maximum
227    /// allowed element length. `input` must be specified without the terminating zero.
228    pub fn set_value(&mut self, index: usize, input: Option<&[C]>)
229    where
230        C: Default + Copy,
231    {
232        if let Some(input) = input {
233            self.set_mut(index, input.len()).copy_from_slice(input);
234        } else {
235            self.indicators[index] = NULL_DATA;
236        }
237    }
238
239    /// Can be used to set a value at a specific row index without performing a memcopy on an input
240    /// slice and instead provides direct access to the underlying buffer.
241    ///
242    /// In situations there the memcopy can not be avoided anyway [`Self::set_value`] is likely to
243    /// be more convenient. This method is very useful if you want to `write!` a string value to the
244    /// buffer and the binary (**!**) length of the formatted string is known upfront.
245    ///
246    /// # Example: Write timestamp to text column.
247    ///
248    /// ```
249    /// use odbc_api::buffers::TextColumn;
250    /// use std::io::Write;
251    ///
252    /// /// Writes times formatted as hh::mm::ss.fff
253    /// fn write_time(
254    ///     col: &mut TextColumn<u8>,
255    ///     index: usize,
256    ///     hours: u8,
257    ///     minutes: u8,
258    ///     seconds: u8,
259    ///     milliseconds: u16)
260    /// {
261    ///     write!(
262    ///         col.set_mut(index, 12),
263    ///         "{:02}:{:02}:{:02}.{:03}",
264    ///         hours, minutes, seconds, milliseconds
265    ///     ).unwrap();
266    /// }
267    /// ```
268    pub fn set_mut(&mut self, index: usize, length: usize) -> &mut [C]
269    where
270        C: Default,
271    {
272        if length > self.max_str_len {
273            panic!(
274                "Tried to insert a value into a text buffer which is larger than the maximum \
275                allowed string length for the buffer."
276            );
277        }
278        self.indicators[index] = (length * size_of::<C>()).try_into().unwrap();
279        let start = (self.max_str_len + 1) * index;
280        let end = start + length;
281        // Let's insert a terminating zero at the end to be on the safe side, in case the ODBC
282        // driver would not care about the value in the index buffer and only look for the
283        // terminating zero.
284        self.values[end] = C::default();
285        &mut self.values[start..end]
286    }
287
288    /// Fills the column with NULL, between From and To
289    pub fn fill_null(&mut self, from: usize, to: usize) {
290        for index in from..to {
291            self.indicators[index] = NULL_DATA;
292        }
293    }
294
295    /// Provides access to the raw underlying value buffer. Normal applications should have little
296    /// reason to call this method. Yet it may be useful for writing bindings which copy directly
297    /// from the ODBC in memory representation into other kinds of buffers.
298    ///
299    /// The buffer contains the bytes for every non null valid element, padded to the maximum string
300    /// length. The content of the padding bytes is undefined. Usually ODBC drivers write a
301    /// terminating zero at the end of each string. For the actual value length call
302    /// [`Self::content_length_at`]. Any element starts at index * ([`Self::max_len`] + 1).
303    pub fn raw_value_buffer(&self, num_valid_rows: usize) -> &[C] {
304        &self.values[..(self.max_str_len + 1) * num_valid_rows]
305    }
306
307    /// The maximum number of rows the TextColumn can hold.
308    pub fn row_capacity(&self) -> usize {
309        self.values.len()
310    }
311}
312
313impl WCharColumn {
314    /// The string slice at the specified position as `U16Str`. Includes interior nuls, but excludes
315    /// the terminating nul.
316    ///
317    /// # Safety
318    ///
319    /// The column buffer does not know how many elements were in the last row group, and therefore
320    /// can not guarantee the accessed element to be valid and in a defined state. It also can not
321    /// panic on accessing an undefined element. It will panic however if `row_index` is larger or
322    /// equal to the maximum number of elements in the buffer.
323    pub unsafe fn ustr_at(&self, row_index: usize) -> Option<&U16Str> {
324        self.value_at(row_index).map(U16Str::from_slice)
325    }
326}
327
328unsafe impl<C: 'static> ColumnBuffer for TextColumn<C>
329where
330    TextColumn<C>: CDataMut + HasDataType,
331{
332    type View<'a> = TextColumnView<'a, C>;
333
334    fn view(&self, valid_rows: usize) -> TextColumnView<'_, C> {
335        TextColumnView {
336            num_rows: valid_rows,
337            col: self,
338        }
339    }
340
341    /// Maximum number of text strings this column may hold.
342    fn capacity(&self) -> usize {
343        self.indicators.len()
344    }
345
346    fn has_truncated_values(&self, num_rows: usize) -> Option<Indicator> {
347        let max_bin_length = self.max_str_len * size_of::<C>();
348        self.indicators
349            .iter()
350            .copied()
351            .take(num_rows)
352            .find_map(|indicator| {
353                let indicator = Indicator::from_isize(indicator);
354                indicator.is_truncated(max_bin_length).then_some(indicator)
355            })
356    }
357}
358
359/// Allows read-only access to the valid part of a text column.
360///
361/// You may ask, why is this type required, should we not just be able to use `&TextColumn`? The
362/// problem with `TextColumn` is, that it is a buffer, but it has no idea how many of its members
363/// are actually valid, and have been returned with the last row group of the result set. That
364/// number is maintained on the level of the entire column buffer. So a text column knows the number
365/// of valid rows, in addition to holding a reference to the buffer, in order to guarantee, that
366/// every element accessed through it, is valid.
367#[derive(Debug, Clone, Copy)]
368pub struct TextColumnView<'c, C> {
369    num_rows: usize,
370    col: &'c TextColumn<C>,
371}
372
373impl<'c, C> TextColumnView<'c, C> {
374    /// The number of valid elements in the text column.
375    pub fn len(&self) -> usize {
376        self.num_rows
377    }
378
379    /// True if, and only if there are no valid rows in the column buffer.
380    pub fn is_empty(&self) -> bool {
381        self.num_rows == 0
382    }
383
384    /// Slice of text at the specified row index without terminating zero. `None` if the value is
385    /// `NULL`. This method will panic if the index is larger than the number of valid rows in the
386    /// view as returned by [`Self::len`].
387    pub fn get(&self, index: usize) -> Option<&'c [C]> {
388        self.col.value_at(index)
389    }
390
391    /// Iterator over the valid elements of the text buffer
392    pub fn iter(&self) -> TextColumnIt<'c, C> {
393        TextColumnIt {
394            pos: 0,
395            num_rows: self.num_rows,
396            col: self.col,
397        }
398    }
399
400    /// Length of value at the specified position. This is different from an indicator as it refers
401    /// to the length of the value in the buffer, not to the length of the value in the datasource.
402    /// The two things are different for truncated values.
403    pub fn content_length_at(&self, row_index: usize) -> Option<usize> {
404        if row_index >= self.num_rows {
405            panic!("Row index points beyond the range of valid values.")
406        }
407        self.col.content_length_at(row_index)
408    }
409
410    /// Provides access to the raw underlying value buffer. Normal applications should have little
411    /// reason to call this method. Yet it may be useful for writing bindings which copy directly
412    /// from the ODBC in memory representation into other kinds of buffers.
413    ///
414    /// The buffer contains the bytes for every non null valid element, padded to the maximum string
415    /// length. The content of the padding bytes is undefined. Usually ODBC drivers write a
416    /// terminating zero at the end of each string. For the actual value length call
417    /// [`Self::content_length_at`]. Any element starts at index * ([`Self::max_len`] + 1).
418    pub fn raw_value_buffer(&self) -> &'c [C] {
419        self.col.raw_value_buffer(self.num_rows)
420    }
421
422    pub fn max_len(&self) -> usize {
423        self.col.max_len()
424    }
425
426    /// `Some` if any value is truncated.
427    ///
428    /// After fetching data we may want to know if any value has been truncated due to the buffer
429    /// not being able to hold elements of that size. This method checks the indicator buffer
430    /// element wise.
431    pub fn has_truncated_values(&self) -> Option<Indicator> {
432        self.col.has_truncated_values(self.num_rows)
433    }
434}
435
436unsafe impl<'a, C: 'static> BoundInputSlice<'a> for TextColumn<C> {
437    type SliceMut = TextColumnSliceMut<'a, C>;
438
439    unsafe fn as_view_mut(
440        &'a mut self,
441        parameter_index: u16,
442        stmt: StatementRef<'a>,
443    ) -> Self::SliceMut {
444        TextColumnSliceMut {
445            column: self,
446            stmt,
447            parameter_index,
448        }
449    }
450}
451
452/// A view to a mutable array parameter text buffer, which allows for filling the buffer with
453/// values.
454pub struct TextColumnSliceMut<'a, C> {
455    column: &'a mut TextColumn<C>,
456    // Needed to rebind the column in case of resize
457    stmt: StatementRef<'a>,
458    // Also needed to rebind the column in case of resize
459    parameter_index: u16,
460}
461
462impl<C> TextColumnSliceMut<'_, C>
463where
464    C: Default + Copy + Send,
465{
466    /// Sets the value of the buffer at index at Null or the specified binary Text. This method will
467    /// panic on out of bounds index, or if input holds a text which is larger than the maximum
468    /// allowed element length. `element` must be specified without the terminating zero.
469    pub fn set_cell(&mut self, row_index: usize, element: Option<&[C]>) {
470        self.column.set_value(row_index, element)
471    }
472
473    /// Ensures that the buffer is large enough to hold elements of `element_length`. Does nothing
474    /// if the buffer is already large enough. Otherwise it will reallocate and rebind the buffer.
475    /// The first `num_rows_to_copy` will be copied from the old value buffer to the new
476    /// one. This makes this an extremely expensive operation.
477    pub fn ensure_max_element_length(
478        &mut self,
479        element_length: usize,
480        num_rows_to_copy: usize,
481    ) -> Result<(), Error>
482    where
483        TextColumn<C>: HasDataType + CData,
484    {
485        // Column buffer is not large enough to hold the element. We must allocate a larger buffer
486        // in order to hold it. This invalidates the pointers previously bound to the statement. So
487        // we rebind them.
488        if element_length > self.column.max_len() {
489            let new_max_str_len = element_length;
490            self.column
491                .resize_max_str(new_max_str_len, num_rows_to_copy);
492            unsafe {
493                self.stmt
494                    .bind_input_parameter(self.parameter_index, self.column)
495                    .into_result(&self.stmt)?
496            }
497        }
498        Ok(())
499    }
500
501    /// Can be used to set a value at a specific row index without performing a memcopy on an input
502    /// slice and instead provides direct access to the underlying buffer.
503    ///
504    /// In situations there the memcopy can not be avoided anyway [`Self::set_cell`] is likely to
505    /// be more convenient. This method is very useful if you want to `write!` a string value to the
506    /// buffer and the binary (**!**) length of the formatted string is known upfront.
507    ///
508    /// # Example: Write timestamp to text column.
509    ///
510    /// ```
511    /// use odbc_api::buffers::TextColumnSliceMut;
512    /// use std::io::Write;
513    ///
514    /// /// Writes times formatted as hh::mm::ss.fff
515    /// fn write_time(
516    ///     col: &mut TextColumnSliceMut<u8>,
517    ///     index: usize,
518    ///     hours: u8,
519    ///     minutes: u8,
520    ///     seconds: u8,
521    ///     milliseconds: u16)
522    /// {
523    ///     write!(
524    ///         col.set_mut(index, 12),
525    ///         "{:02}:{:02}:{:02}.{:03}",
526    ///         hours, minutes, seconds, milliseconds
527    ///     ).unwrap();
528    /// }
529    /// ```
530    pub fn set_mut(&mut self, index: usize, length: usize) -> &mut [C] {
531        self.column.set_mut(index, length)
532    }
533}
534
535/// Iterator over a text column. See [`TextColumnView::iter`]
536#[derive(Debug)]
537pub struct TextColumnIt<'c, C> {
538    pos: usize,
539    num_rows: usize,
540    col: &'c TextColumn<C>,
541}
542
543impl<'c, C> TextColumnIt<'c, C> {
544    fn next_impl(&mut self) -> Option<Option<&'c [C]>> {
545        if self.pos == self.num_rows {
546            None
547        } else {
548            let ret = Some(self.col.value_at(self.pos));
549            self.pos += 1;
550            ret
551        }
552    }
553}
554
555impl<'c> Iterator for TextColumnIt<'c, u8> {
556    type Item = Option<&'c [u8]>;
557
558    fn next(&mut self) -> Option<Self::Item> {
559        self.next_impl()
560    }
561
562    fn size_hint(&self) -> (usize, Option<usize>) {
563        let len = self.num_rows - self.pos;
564        (len, Some(len))
565    }
566}
567
568impl ExactSizeIterator for TextColumnIt<'_, u8> {}
569
570impl<'c> Iterator for TextColumnIt<'c, u16> {
571    type Item = Option<&'c U16Str>;
572
573    fn next(&mut self) -> Option<Self::Item> {
574        self.next_impl().map(|opt| opt.map(U16Str::from_slice))
575    }
576
577    fn size_hint(&self) -> (usize, Option<usize>) {
578        let len = self.num_rows - self.pos;
579        (len, Some(len))
580    }
581}
582
583impl ExactSizeIterator for TextColumnIt<'_, u16> {}
584
585unsafe impl CData for CharColumn {
586    fn cdata_type(&self) -> CDataType {
587        CDataType::Char
588    }
589
590    fn indicator_ptr(&self) -> *const isize {
591        self.indicators.as_ptr()
592    }
593
594    fn value_ptr(&self) -> *const c_void {
595        self.values.as_ptr() as *const c_void
596    }
597
598    fn buffer_length(&self) -> isize {
599        (self.max_str_len + 1).try_into().unwrap()
600    }
601}
602
603unsafe impl CDataMut for CharColumn {
604    fn mut_indicator_ptr(&mut self) -> *mut isize {
605        self.indicators.as_mut_ptr()
606    }
607
608    fn mut_value_ptr(&mut self) -> *mut c_void {
609        self.values.as_mut_ptr() as *mut c_void
610    }
611}
612
613impl HasDataType for CharColumn {
614    fn data_type(&self) -> DataType {
615        if self.max_str_len <= ASSUMED_MAX_LENGTH_OF_VARCHAR {
616            DataType::Varchar {
617                length: NonZeroUsize::new(self.max_str_len),
618            }
619        } else {
620            DataType::LongVarchar {
621                length: NonZeroUsize::new(self.max_str_len),
622            }
623        }
624    }
625}
626
627unsafe impl CData for WCharColumn {
628    fn cdata_type(&self) -> CDataType {
629        CDataType::WChar
630    }
631
632    fn indicator_ptr(&self) -> *const isize {
633        self.indicators.as_ptr()
634    }
635
636    fn value_ptr(&self) -> *const c_void {
637        self.values.as_ptr() as *const c_void
638    }
639
640    fn buffer_length(&self) -> isize {
641        ((self.max_str_len + 1) * 2).try_into().unwrap()
642    }
643}
644
645unsafe impl CDataMut for WCharColumn {
646    fn mut_indicator_ptr(&mut self) -> *mut isize {
647        self.indicators.as_mut_ptr()
648    }
649
650    fn mut_value_ptr(&mut self) -> *mut c_void {
651        self.values.as_mut_ptr() as *mut c_void
652    }
653}
654
655impl HasDataType for WCharColumn {
656    fn data_type(&self) -> DataType {
657        if self.max_str_len <= ASSUMED_MAX_LENGTH_OF_W_VARCHAR {
658            DataType::WVarchar {
659                length: NonZeroUsize::new(self.max_str_len),
660            }
661        } else {
662            DataType::WLongVarchar {
663                length: NonZeroUsize::new(self.max_str_len),
664            }
665        }
666    }
667}
668
669impl<C> Resize for TextColumn<C>
670where
671    C: Clone + Default,
672{
673    fn resize(&mut self, new_capacity: usize) {
674        self.values
675            .resize((self.max_str_len + 1) * new_capacity, C::default());
676        self.indicators.resize(new_capacity, NULL_DATA);
677    }
678}
679
680#[cfg(test)]
681mod tests {
682    use crate::buffers::{Resize, TextColumn};
683
684    #[test]
685    fn resize_text_column_buffer() {
686        // Given a text column buffer with two elements
687        let mut col = TextColumn::<u8>::new(2, 10);
688        col.set_value(0, Some(b"Hello"));
689        col.set_value(1, Some(b"World"));
690
691        // When we resize it to hold 3 elements
692        col.resize(3);
693
694        // Then the first two elements are still there, and the third is None
695        assert_eq!(col.value_at(0), Some(b"Hello".as_ref()));
696        assert_eq!(col.value_at(1), Some(b"World".as_ref()));
697        assert_eq!(col.value_at(2), None);
698    }
699}