odbc_api/buffers/text_column.rs
1use crate::{
2 DataType, Error,
3 buffers::{Resize, columnar::Slice},
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::{any::Any, 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> ColumnBuffer for TextColumn<C>
329where
330 TextColumn<C>: CDataMut + HasDataType + Any,
331{
332 /// Maximum number of text strings this column may hold.
333 fn capacity(&self) -> usize {
334 self.indicators.len()
335 }
336
337 fn has_truncated_values(&self, num_rows: usize) -> Option<Indicator> {
338 let max_bin_length = self.max_str_len * size_of::<C>();
339 self.indicators
340 .iter()
341 .copied()
342 .take(num_rows)
343 .find_map(|indicator| {
344 let indicator = Indicator::from_isize(indicator);
345 indicator.is_truncated(max_bin_length).then_some(indicator)
346 })
347 }
348}
349
350unsafe impl<C: 'static> Slice for TextColumn<C> {
351 type Slice<'a> = TextColumnSlice<'a, C>;
352
353 fn slice(&self, valid_rows: usize) -> TextColumnSlice<'_, C> {
354 TextColumnSlice {
355 num_rows: valid_rows,
356 col: self,
357 }
358 }
359}
360
361/// Allows read-only access to the valid part of a text column.
362///
363/// You may ask, why is this type required, should we not just be able to use `&TextColumn`? The
364/// problem with `TextColumn` is, that it is a buffer, but it has no idea how many of its members
365/// are actually valid, and have been returned with the last row group of the result set. That
366/// number is maintained on the level of the entire column buffer. So a text column knows the number
367/// of valid rows, in addition to holding a reference to the buffer, in order to guarantee, that
368/// every element accessed through it, is valid.
369#[derive(Debug, Clone, Copy)]
370pub struct TextColumnSlice<'c, C> {
371 num_rows: usize,
372 col: &'c TextColumn<C>,
373}
374
375impl<'c, C> TextColumnSlice<'c, C> {
376 /// The number of valid elements in the text column.
377 pub fn len(&self) -> usize {
378 self.num_rows
379 }
380
381 /// True if, and only if there are no valid rows in the column buffer.
382 pub fn is_empty(&self) -> bool {
383 self.num_rows == 0
384 }
385
386 /// Slice of text at the specified row index without terminating zero. `None` if the value is
387 /// `NULL`. This method will panic if the index is larger than the number of valid rows in the
388 /// view as returned by [`Self::len`].
389 pub fn get(&self, index: usize) -> Option<&'c [C]> {
390 self.col.value_at(index)
391 }
392
393 /// Iterator over the valid elements of the text buffer
394 pub fn iter(&self) -> TextColumnIt<'c, C> {
395 TextColumnIt {
396 pos: 0,
397 num_rows: self.num_rows,
398 col: self.col,
399 }
400 }
401
402 /// Length of value at the specified position. This is different from an indicator as it refers
403 /// to the length of the value in the buffer, not to the length of the value in the datasource.
404 /// The two things are different for truncated values.
405 pub fn content_length_at(&self, row_index: usize) -> Option<usize> {
406 if row_index >= self.num_rows {
407 panic!("Row index points beyond the range of valid values.")
408 }
409 self.col.content_length_at(row_index)
410 }
411
412 /// Provides access to the raw underlying value buffer. Normal applications should have little
413 /// reason to call this method. Yet it may be useful for writing bindings which copy directly
414 /// from the ODBC in memory representation into other kinds of buffers.
415 ///
416 /// The buffer contains the bytes for every non null valid element, padded to the maximum string
417 /// length. The content of the padding bytes is undefined. Usually ODBC drivers write a
418 /// terminating zero at the end of each string. For the actual value length call
419 /// [`Self::content_length_at`]. Any element starts at index * ([`Self::max_len`] + 1).
420 pub fn raw_value_buffer(&self) -> &'c [C] {
421 self.col.raw_value_buffer(self.num_rows)
422 }
423
424 pub fn max_len(&self) -> usize {
425 self.col.max_len()
426 }
427
428 /// `Some` if any value is truncated.
429 ///
430 /// After fetching data we may want to know if any value has been truncated due to the buffer
431 /// not being able to hold elements of that size. This method checks the indicator buffer
432 /// element wise.
433 pub fn has_truncated_values(&self) -> Option<Indicator> {
434 self.col.has_truncated_values(self.num_rows)
435 }
436}
437
438unsafe impl<'a, C: 'static> BoundInputSlice<'a> for TextColumn<C> {
439 type SliceMut = TextColumnSliceMut<'a, C>;
440
441 unsafe fn as_view_mut(
442 &'a mut self,
443 parameter_index: u16,
444 stmt: StatementRef<'a>,
445 ) -> Self::SliceMut {
446 TextColumnSliceMut {
447 column: self,
448 stmt,
449 parameter_index,
450 }
451 }
452}
453
454/// A view to a mutable array parameter text buffer, which allows for filling the buffer with
455/// values.
456pub struct TextColumnSliceMut<'a, C> {
457 column: &'a mut TextColumn<C>,
458 // Needed to rebind the column in case of resize
459 stmt: StatementRef<'a>,
460 // Also needed to rebind the column in case of resize
461 parameter_index: u16,
462}
463
464impl<C> TextColumnSliceMut<'_, C>
465where
466 C: Default + Copy + Send,
467{
468 /// Sets the value of the buffer at index at Null or the specified binary Text. This method will
469 /// panic on out of bounds index, or if input holds a text which is larger than the maximum
470 /// allowed element length. `element` must be specified without the terminating zero.
471 pub fn set_cell(&mut self, row_index: usize, element: Option<&[C]>) {
472 self.column.set_value(row_index, element)
473 }
474
475 /// Ensures that the buffer is large enough to hold elements of `element_length`. Does nothing
476 /// if the buffer is already large enough. Otherwise it will reallocate and rebind the buffer.
477 /// The first `num_rows_to_copy` will be copied from the old value buffer to the new
478 /// one. This makes this an extremely expensive operation.
479 pub fn ensure_max_element_length(
480 &mut self,
481 element_length: usize,
482 num_rows_to_copy: usize,
483 ) -> Result<(), Error>
484 where
485 TextColumn<C>: HasDataType + CData,
486 {
487 // Column buffer is not large enough to hold the element. We must allocate a larger buffer
488 // in order to hold it. This invalidates the pointers previously bound to the statement. So
489 // we rebind them.
490 if element_length > self.column.max_len() {
491 let new_max_str_len = element_length;
492 self.column
493 .resize_max_str(new_max_str_len, num_rows_to_copy);
494 unsafe {
495 self.stmt
496 .bind_input_parameter(self.parameter_index, self.column)
497 .into_result(&self.stmt)?
498 }
499 }
500 Ok(())
501 }
502
503 /// Can be used to set a value at a specific row index without performing a memcopy on an input
504 /// slice and instead provides direct access to the underlying buffer.
505 ///
506 /// In situations there the memcopy can not be avoided anyway [`Self::set_cell`] is likely to
507 /// be more convenient. This method is very useful if you want to `write!` a string value to the
508 /// buffer and the binary (**!**) length of the formatted string is known upfront.
509 ///
510 /// # Example: Write timestamp to text column.
511 ///
512 /// ```
513 /// use odbc_api::buffers::TextColumnSliceMut;
514 /// use std::io::Write;
515 ///
516 /// /// Writes times formatted as hh::mm::ss.fff
517 /// fn write_time(
518 /// col: &mut TextColumnSliceMut<u8>,
519 /// index: usize,
520 /// hours: u8,
521 /// minutes: u8,
522 /// seconds: u8,
523 /// milliseconds: u16)
524 /// {
525 /// write!(
526 /// col.set_mut(index, 12),
527 /// "{:02}:{:02}:{:02}.{:03}",
528 /// hours, minutes, seconds, milliseconds
529 /// ).unwrap();
530 /// }
531 /// ```
532 pub fn set_mut(&mut self, index: usize, length: usize) -> &mut [C] {
533 self.column.set_mut(index, length)
534 }
535}
536
537/// Iterator over a text column. See [`TextColumnView::iter`]
538#[derive(Debug)]
539pub struct TextColumnIt<'c, C> {
540 pos: usize,
541 num_rows: usize,
542 col: &'c TextColumn<C>,
543}
544
545impl<'c, C> TextColumnIt<'c, C> {
546 fn next_impl(&mut self) -> Option<Option<&'c [C]>> {
547 if self.pos == self.num_rows {
548 None
549 } else {
550 let ret = Some(self.col.value_at(self.pos));
551 self.pos += 1;
552 ret
553 }
554 }
555}
556
557impl<'c> Iterator for TextColumnIt<'c, u8> {
558 type Item = Option<&'c [u8]>;
559
560 fn next(&mut self) -> Option<Self::Item> {
561 self.next_impl()
562 }
563
564 fn size_hint(&self) -> (usize, Option<usize>) {
565 let len = self.num_rows - self.pos;
566 (len, Some(len))
567 }
568}
569
570impl ExactSizeIterator for TextColumnIt<'_, u8> {}
571
572impl<'c> Iterator for TextColumnIt<'c, u16> {
573 type Item = Option<&'c U16Str>;
574
575 fn next(&mut self) -> Option<Self::Item> {
576 self.next_impl().map(|opt| opt.map(U16Str::from_slice))
577 }
578
579 fn size_hint(&self) -> (usize, Option<usize>) {
580 let len = self.num_rows - self.pos;
581 (len, Some(len))
582 }
583}
584
585impl ExactSizeIterator for TextColumnIt<'_, u16> {}
586
587unsafe impl CData for CharColumn {
588 fn cdata_type(&self) -> CDataType {
589 CDataType::Char
590 }
591
592 fn indicator_ptr(&self) -> *const isize {
593 self.indicators.as_ptr()
594 }
595
596 fn value_ptr(&self) -> *const c_void {
597 self.values.as_ptr() as *const c_void
598 }
599
600 fn buffer_length(&self) -> isize {
601 (self.max_str_len + 1).try_into().unwrap()
602 }
603}
604
605unsafe impl CDataMut for CharColumn {
606 fn mut_indicator_ptr(&mut self) -> *mut isize {
607 self.indicators.as_mut_ptr()
608 }
609
610 fn mut_value_ptr(&mut self) -> *mut c_void {
611 self.values.as_mut_ptr() as *mut c_void
612 }
613}
614
615impl HasDataType for CharColumn {
616 fn data_type(&self) -> DataType {
617 if self.max_str_len <= ASSUMED_MAX_LENGTH_OF_VARCHAR {
618 DataType::Varchar {
619 length: NonZeroUsize::new(self.max_str_len),
620 }
621 } else {
622 DataType::LongVarchar {
623 length: NonZeroUsize::new(self.max_str_len),
624 }
625 }
626 }
627}
628
629unsafe impl CData for WCharColumn {
630 fn cdata_type(&self) -> CDataType {
631 CDataType::WChar
632 }
633
634 fn indicator_ptr(&self) -> *const isize {
635 self.indicators.as_ptr()
636 }
637
638 fn value_ptr(&self) -> *const c_void {
639 self.values.as_ptr() as *const c_void
640 }
641
642 fn buffer_length(&self) -> isize {
643 ((self.max_str_len + 1) * 2).try_into().unwrap()
644 }
645}
646
647unsafe impl CDataMut for WCharColumn {
648 fn mut_indicator_ptr(&mut self) -> *mut isize {
649 self.indicators.as_mut_ptr()
650 }
651
652 fn mut_value_ptr(&mut self) -> *mut c_void {
653 self.values.as_mut_ptr() as *mut c_void
654 }
655}
656
657impl HasDataType for WCharColumn {
658 fn data_type(&self) -> DataType {
659 if self.max_str_len <= ASSUMED_MAX_LENGTH_OF_W_VARCHAR {
660 DataType::WVarchar {
661 length: NonZeroUsize::new(self.max_str_len),
662 }
663 } else {
664 DataType::WLongVarchar {
665 length: NonZeroUsize::new(self.max_str_len),
666 }
667 }
668 }
669}
670
671impl<C> Resize for TextColumn<C>
672where
673 C: Clone + Default,
674{
675 fn resize(&mut self, new_capacity: usize) {
676 self.values
677 .resize((self.max_str_len + 1) * new_capacity, C::default());
678 self.indicators.resize(new_capacity, NULL_DATA);
679 }
680}
681
682#[cfg(test)]
683mod tests {
684 use crate::buffers::{Resize, TextColumn};
685
686 #[test]
687 fn resize_text_column_buffer() {
688 // Given a text column buffer with two elements
689 let mut col = TextColumn::<u8>::new(2, 10);
690 col.set_value(0, Some(b"Hello"));
691 col.set_value(1, Some(b"World"));
692
693 // When we resize it to hold 3 elements
694 col.resize(3);
695
696 // Then the first two elements are still there, and the third is None
697 assert_eq!(col.value_at(0), Some(b"Hello".as_ref()));
698 assert_eq!(col.value_at(1), Some(b"World".as_ref()));
699 assert_eq!(col.value_at(2), None);
700 }
701}