Skip to content

Commit

Permalink
Introduce truncation info
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Nov 24, 2023
1 parent 438d8fa commit 3e3015a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
5 changes: 3 additions & 2 deletions odbc-api/src/buffers/columnar.rs
Expand Up @@ -10,7 +10,7 @@ use crate::{
handles::{CDataMut, Statement, StatementRef},
parameter::WithDataType,
result_set_metadata::utf8_display_sizes,
Error, ResultSetMetadata, RowSetBuffer,
Error, ResultSetMetadata, RowSetBuffer, cursor::TruncationInfo,
};

use super::{Indicator, TextColumn};
Expand Down Expand Up @@ -104,10 +104,11 @@ where
Ok(())
}

fn find_truncation(&self) -> Option<Indicator> {
fn find_truncation(&self) -> Option<TruncationInfo> {
self.columns
.iter()
.find_map(|col_buffer| col_buffer.1.has_truncated_values(*self.num_rows))
.map(|indicator| TruncationInfo { indicator })
}
}

Expand Down
14 changes: 11 additions & 3 deletions odbc-api/src/cursor.rs
Expand Up @@ -390,7 +390,15 @@ pub unsafe trait RowSetBuffer {
unsafe fn bind_colmuns_to_cursor(&mut self, cursor: StatementRef<'_>) -> Result<(), Error>;

/// Find an indicator larger than the maximum element size of the buffer.
fn find_truncation(&self) -> Option<Indicator>;
fn find_truncation(&self) -> Option<TruncationInfo>;
}

/// Returned by [`RowSetBuffer::find_truncation`]. Contains information about the truncation found.
pub struct TruncationInfo {
/// Indicator of the complete untruncated value.
pub indicator: Indicator,
// /// Zero based buffer index of the column in which the truncation occurred.
// pub buffer_index: usize,
}

unsafe impl<T: RowSetBuffer> RowSetBuffer for &mut T {
Expand All @@ -410,7 +418,7 @@ unsafe impl<T: RowSetBuffer> RowSetBuffer for &mut T {
(*self).bind_colmuns_to_cursor(cursor)
}

fn find_truncation(&self) -> Option<Indicator> {
fn find_truncation(&self) -> Option<TruncationInfo> {
(**self).find_truncation()
}
}
Expand Down Expand Up @@ -733,7 +741,7 @@ fn error_handling_for_fetch(
// while we are limited in the amount we can check. The second check serves as an optimization
// for the happy path.
if error_for_truncation && result == SqlResult::SuccessWithInfo(()) {
if let Some(indicator) = buffer.find_truncation() {
if let Some(TruncationInfo { indicator }) = buffer.find_truncation() {
return Err(Error::TooLargeValueForBuffer { indicator });
}
}
Expand Down
3 changes: 2 additions & 1 deletion odbc-api/src/lib.rs
Expand Up @@ -31,7 +31,8 @@ pub use self::{
columnar_bulk_inserter::{BoundInputSlice, ColumnarBulkInserter},
connection::{escape_attribute_value, Connection, ConnectionOptions},
cursor::{
BlockCursor, BlockCursorPolling, Cursor, CursorImpl, CursorPolling, CursorRow, RowSetBuffer,
BlockCursor, BlockCursorPolling, Cursor, CursorImpl, CursorPolling, CursorRow,
RowSetBuffer, TruncationInfo,
},
driver_complete_option::DriverCompleteOption,
environment::{DataSourceInfo, DriverInfo, Environment},
Expand Down
6 changes: 3 additions & 3 deletions odbc-api/tests/common.rs
Expand Up @@ -2,9 +2,9 @@ use std::iter::repeat;

use lazy_static::lazy_static;
use odbc_api::{
buffers::{self, Indicator},
buffers,
handles::{CDataMut, Statement, StatementRef},
Connection, ConnectionOptions, Cursor, Environment, Error, RowSetBuffer,
Connection, ConnectionOptions, Cursor, Environment, Error, RowSetBuffer, TruncationInfo,
};

// Rust by default executes tests in parallel. Yet only one environment is allowed at a time.
Expand Down Expand Up @@ -209,7 +209,7 @@ where
Ok(())
}

fn find_truncation(&self) -> Option<Indicator> {
fn find_truncation(&self) -> Option<TruncationInfo> {
unimplemented!()
}
}

0 comments on commit 3e3015a

Please sign in to comment.