Skip to content

Commit

Permalink
replace WideTextBytes with WideText
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Apr 14, 2023
1 parent c4c51e6 commit 8c366ca
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
4 changes: 2 additions & 2 deletions odbc-api/src/into_parameter.rs
Expand Up @@ -115,8 +115,8 @@ impl<'a> IntoParameter for &'a U16Str {

fn into_parameter(self) -> Self::Parameter {
let slice = self.as_slice();
let (_, bytes, _) = unsafe { slice.align_to::<u8>() };
VarWCharSlice::from_buffer(bytes, Indicator::Length(bytes.len()))
let length_in_bytes = slice.len() * 2;
VarWCharSlice::from_buffer(slice, Indicator::Length(length_in_bytes))
}
}

Expand Down
4 changes: 1 addition & 3 deletions odbc-api/src/parameter.rs
Expand Up @@ -343,12 +343,10 @@ pub use self::{
varcell::{
Binary, Text, VarBinary, VarBinaryArray, VarBinaryBox, VarBinarySlice, VarBinarySliceMut,
VarCell, VarChar, VarCharArray, VarCharBox, VarCharSlice, VarCharSliceMut, VarKind,
VarWCharSlice,
},
};

/// Currenty not made public due to unsure if it might not be better to implement this via u16
pub(crate) use self::varcell::VarWCharSlice;

use std::ffi::c_void;

use odbc_sys::CDataType;
Expand Down
27 changes: 5 additions & 22 deletions odbc-api/src/parameter/varcell.rs
Expand Up @@ -54,26 +54,9 @@ unsafe impl VarKind for Text {
}

/// Intended to be used as a generic argument for [`VariadicCell`] to declare that this buffer is
/// used to hold wide UTF-16 (as opposed to narrow ASCII or UTF-8) text. Use this to annotate binary
/// buffers (`[u8]`), rather than `[u16]` buffers.
pub struct WideTextBytes;

unsafe impl VarKind for WideTextBytes {
type Element = u8;
const ZERO: u8 = 0;
const TERMINATING_ZEROES: usize = 2;
const C_DATA_TYPE: CDataType = CDataType::WChar;

fn relational_type(length: usize) -> DataType {
// Since we might use as an input buffer, we report the full buffer length in the type and
// do not deduct 2 for the terminating zero.
//
// Also the length is in bytes and needs to be converted to characters.
DataType::WVarchar { length: length / 2 }
}
}

struct WideText;
/// used to hold wide UTF-16 (as opposed to narrow ASCII or UTF-8) text. Use this to annotate
/// `[u16]` buffers.
pub struct WideText;

unsafe impl VarKind for WideText {
type Element = u16;
Expand Down Expand Up @@ -135,7 +118,7 @@ pub struct VarCell<B, K> {

pub type VarBinary<B> = VarCell<B, Binary>;
pub type VarChar<B> = VarCell<B, Text>;
pub type VarWChar<B> = VarCell<B, WideTextBytes>;
pub type VarWChar<B> = VarCell<B, WideText>;

/// Parameter type for owned, variable sized character data.
///
Expand Down Expand Up @@ -396,7 +379,7 @@ where
/// ```
pub type VarCharSlice<'a> = VarChar<&'a [u8]>;

pub type VarWCharSlice<'a> = VarWChar<&'a [u8]>;
pub type VarWCharSlice<'a> = VarWChar<&'a [u16]>;

/// Binds a byte array as a variadic binary input parameter.
///
Expand Down

0 comments on commit 8c366ca

Please sign in to comment.