1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use super::{BindColParameters, ColumnBuffer, TextColumn};
use crate::{Cursor, Error, RowSetBuffer};
use odbc_sys::{UInteger, ULen, USmallInt};
use std::str::Utf8Error;

/// This row set binds a string buffer to each column, which is large enough to hold the maximum

/// length string representation for each element in the row set at once.

pub struct TextRowSet {
    batch_size: UInteger,
    num_rows_fetched: ULen,
    buffers: Vec<TextColumn>,
}

impl TextRowSet {
    /// Use `cursor` to query the display size for each column of the row set and allocates the

    /// buffers accordingly.

    pub fn new(batch_size: UInteger, cursor: &Cursor) -> Result<TextRowSet, Error> {
        let num_cols = cursor.num_result_cols()?;
        let buffers = (1..(num_cols + 1))
            .map(|col_index| {
                let max_str_len = cursor.col_display_size(col_index as USmallInt)? as usize;
                Ok(TextColumn::new(batch_size as usize, max_str_len))
            })
            .collect::<Result<_, Error>>()?;
        Ok(TextRowSet {
            batch_size,
            num_rows_fetched: 0,
            buffers,
        })
    }

    /// Access the element at the specified position in the row set.

    pub fn at(&self, col_index: usize, row_index: usize) -> Option<&[u8]> {
        assert!(row_index < self.num_rows_fetched as usize);
        unsafe { self.buffers[col_index].value_at(row_index) }
    }

    /// Access the element at the specified position in the row set.

    pub fn at_as_str(&self, col_index: usize, row_index: usize) -> Result<Option<&str>, Utf8Error> {
        self.at(col_index, row_index)
            .map(std::str::from_utf8)
            .transpose()
    }

    /// Return the number of columns in the row set.

    pub fn num_cols(&self) -> usize {
        self.buffers.len()
    }

    /// Return the number of rows in the row set.

    pub fn num_rows(&self) -> usize {
        self.num_rows_fetched as usize
    }
}

unsafe impl RowSetBuffer for TextRowSet {
    unsafe fn bind_to_cursor(&mut self, cursor: &mut Cursor) -> Result<(), Error> {
        cursor.set_row_bind_type(0)?;
        cursor.set_row_array_size(self.batch_size)?;
        cursor.set_num_rows_fetched(&mut self.num_rows_fetched)?;
        for (index, column_buffer) in self.buffers.iter_mut().enumerate() {
            let column_number = (index + 1) as u16;
            let BindColParameters {
                target_type,
                target_value,
                target_length,
                indicator,
            } = column_buffer.bind_arguments();
            cursor.bind_col(
                column_number,
                target_type,
                target_value,
                target_length,
                indicator,
            )?;
        }
        Ok(())
    }
}