Skip to content

Commit

Permalink
DataType::column_size now returns
Browse files Browse the repository at this point in the history
Option<NonZeroUSize>
  • Loading branch information
pacman82 committed Nov 23, 2023
1 parent dac8766 commit 46524b5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* `DataType::display_size` now returns `Option<NonZeroUSize>` instead of `Option<usize>`.
* `DataType::utf8_len` now returns `Option<NonZeroUSize>` instead of `Option<usize>`.
* `DataType::utf16_len` now returns `Option<NonZeroUSize>` instead of `Option<usize>`
* `DataType::column_size` now returns `Option<NonZeroUSize>` instead of `usize`
* `BufferDesc::from_data_type` now returns `None` for variadic types without upper bound instead of a zero sized buffer.

## 3.0.1
Expand Down
18 changes: 9 additions & 9 deletions odbc-api/src/handles/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub enum DataType {
/// Type of the column
data_type: SqlDataType,
/// Size of column element
column_size: usize,
column_size: Option<NonZeroUsize>,
/// Decimal digits returned for the column element. Exact meaning if any depends on the
/// `data_type` field.
decimal_digits: i16,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl DataType {
},
other => DataType::Other {
data_type: other,
column_size,
column_size: NonZeroUsize::new(column_size),
decimal_digits,
},
}
Expand Down Expand Up @@ -227,10 +227,10 @@ impl DataType {
}
}

/// Return the column size, as it is required to bind the data type as a parameter. This implies
// it can be zero for fixed sized types. See also [crates::Cursor::describe_col]. Variadic types
// without upper bound are also mapped to zero.
pub fn column_size(&self) -> usize {
// Return the column size, as it is required to bind the data type as a parameter. Fixed sized
// types are mapped to `None` and should be bound using `0`. See also
// [crates::Cursor::describe_col]. Variadic types without upper bound are also mapped to `None`.
pub fn column_size(&self) -> Option<NonZeroUsize> {
match self {
DataType::Unknown
| DataType::Integer
Expand All @@ -242,18 +242,18 @@ impl DataType {
| DataType::Timestamp { .. }
| DataType::BigInt
| DataType::TinyInt
| DataType::Bit => 0,
| DataType::Bit => None,
DataType::Char { length }
| DataType::Varchar { length }
| DataType::Varbinary { length }
| DataType::LongVarbinary { length }
| DataType::Binary { length }
| DataType::WChar { length }
| DataType::WVarchar { length }
| DataType::LongVarchar { length } => length.map(NonZeroUsize::get).unwrap_or_default(),
| DataType::LongVarchar { length } => *length,
DataType::Float { precision, .. }
| DataType::Numeric { precision, .. }
| DataType::Decimal { precision, .. } => *precision,
| DataType::Decimal { precision, .. } => NonZeroUsize::new(*precision),
DataType::Other { column_size, .. } => *column_size,
}
}
Expand Down
8 changes: 4 additions & 4 deletions odbc-api/src/handles/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use odbc_sys::{
SQLGetData, SQLMoreResults, SQLNumParams, SQLNumResultCols, SQLParamData, SQLPutData,
SQLRowCount, SqlDataType, SqlReturn, StatementAttribute, IS_POINTER,
};
use std::{ffi::c_void, marker::PhantomData, mem::ManuallyDrop, ptr::null_mut};
use std::{ffi::c_void, marker::PhantomData, mem::ManuallyDrop, ptr::null_mut, num::NonZeroUsize};

#[cfg(feature = "odbc_version_3_80")]
use odbc_sys::SQLCompleteAsync;
Expand Down Expand Up @@ -507,7 +507,7 @@ pub trait Statement: AsHandle {
ParamType::Input,
parameter.cdata_type(),
parameter_type.data_type(),
parameter_type.column_size(),
parameter_type.column_size().map(NonZeroUsize::get).unwrap_or_default(),
parameter_type.decimal_digits(),
// We cast const to mut here, but we specify the input_output_type as input.
parameter.value_ptr() as *mut c_void,
Expand Down Expand Up @@ -542,7 +542,7 @@ pub trait Statement: AsHandle {
input_output_type,
parameter.cdata_type(),
parameter_type.data_type(),
parameter_type.column_size(),
parameter_type.column_size().map(NonZeroUsize::get).unwrap_or_default(),
parameter_type.decimal_digits(),
parameter.value_ptr() as *mut c_void,
parameter.buffer_length(),
Expand Down Expand Up @@ -573,7 +573,7 @@ pub trait Statement: AsHandle {
ParamType::Input,
parameter.cdata_type(),
paramater_type.data_type(),
paramater_type.column_size(),
paramater_type.column_size().map(NonZeroUsize::get).unwrap_or_default(),
paramater_type.decimal_digits(),
parameter.stream_ptr(),
0,
Expand Down
2 changes: 1 addition & 1 deletion odbc-api/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn describe_columns(profile: &Profile) {

let kind = DataType::Other {
data_type: SqlDataType(-154),
column_size: 16,
column_size: NonZeroUsize::new(16),
decimal_digits: 7,
};
let expected = ColumnDescription::new("h", kind, Nullability::Nullable);
Expand Down

0 comments on commit 46524b5

Please sign in to comment.