Skip to content

Commit

Permalink
working prototype IntoParameter UString
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Apr 14, 2023
1 parent 8c366ca commit 22fdcd8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
12 changes: 10 additions & 2 deletions odbc-api/src/into_parameter.rs
@@ -1,10 +1,10 @@
use widestring::U16Str;
use widestring::{U16Str, U16String};

use crate::{
buffers::Indicator,
fixed_sized::Pod,
parameter::{
InputParameter, VarBinaryBox, VarBinarySlice, VarCharBox, VarCharSlice, VarWCharSlice,
InputParameter, VarBinaryBox, VarBinarySlice, VarCharBox, VarCharSlice, VarWCharSlice, VarWCharBox,
},
Nullable,
};
Expand Down Expand Up @@ -131,6 +131,14 @@ impl<'a> IntoParameter for Option<&'a U16Str> {
}
}

impl IntoParameter for U16String {
type Parameter = VarWCharBox;

fn into_parameter(self) -> Self::Parameter {
VarWCharBox::from_vec(self.into_vec())
}
}

impl<T> IntoParameter for Option<T>
where
T: Pod + InputParameter,
Expand Down
2 changes: 1 addition & 1 deletion odbc-api/src/parameter.rs
Expand Up @@ -343,7 +343,7 @@ pub use self::{
varcell::{
Binary, Text, VarBinary, VarBinaryArray, VarBinaryBox, VarBinarySlice, VarBinarySliceMut,
VarCell, VarChar, VarCharArray, VarCharBox, VarCharSlice, VarCharSliceMut, VarKind,
VarWCharSlice,
VarWCharSlice, VarWCharBox
},
};

Expand Down
8 changes: 7 additions & 1 deletion odbc-api/src/parameter/varcell.rs
Expand Up @@ -120,12 +120,18 @@ pub type VarBinary<B> = VarCell<B, Binary>;
pub type VarChar<B> = VarCell<B, Text>;
pub type VarWChar<B> = VarCell<B, WideText>;

/// Parameter type for owned, variable sized character data.
/// Parameter type for owned, variable sized narrow character data.
///
/// We use `Box<[u8]>` rather than `Vec<u8>` as a buffer type since the indicator pointer already
/// has the role of telling us how many bytes in the buffer are part of the payload.
pub type VarCharBox = VarChar<Box<[u8]>>;

/// Parameter type for owned, variable sized wide character data.
///
/// We use `Box<[u16]>` rather than `Vec<u16>` as a buffer type since the indicator pointer already
/// has the role of telling us how many characters in the buffer are part of the payload.
pub type VarWCharBox = VarWChar<Box<[u16]>>;

/// Parameter type for owned, variable sized binary data.
///
/// We use `Box<[u8]>` rather than `Vec<u8>` as a buffer type since the indicator pointer already
Expand Down
20 changes: 19 additions & 1 deletion odbc-api/tests/integration.rs
Expand Up @@ -1447,18 +1447,36 @@ fn bind_str_parameter_to_char(profile: &Profile) {
#[test_case(MARIADB; "Maria DB")]
#[test_case(SQLITE_3; "SQLite 3")]
#[test_case(POSTGRES; "PostgreSQL")]
fn bind_u16str_parameter_to_char(profile: &Profile) {
fn bind_u16_str_parameter_to_char(profile: &Profile) {
let table_name = table_name!();
let (conn, table) = profile.given(&table_name, &["CHAR(5)"]).unwrap();
let insert_sql = table.sql_insert();

let hello = U16String::from_str("Hello");
let hello = hello.as_ustr();
conn.execute(&insert_sql, &hello.into_parameter()).unwrap();

let actual = table.content_as_string(&conn);
assert_eq!("Hello", &actual);
}

#[test_case(MSSQL; "Microsoft SQL Server")]
#[test_case(MARIADB; "Maria DB")]
#[test_case(SQLITE_3; "SQLite 3")]
#[test_case(POSTGRES; "PostgreSQL")]
fn bind_u16_string_parameter_to_char(profile: &Profile) {
let table_name = table_name!();
let (conn, table) = profile.given(&table_name, &["CHAR(5)"]).unwrap();
let insert_sql = table.sql_insert();

// Usecase: Create an owned parameter from a UTF-16 string
let hello = U16String::from_str("Hello").into_parameter();
conn.execute(&insert_sql, &hello).unwrap();

let actual = table.content_as_string(&conn);
assert_eq!("Hello", &actual);
}

#[test_case(MSSQL; "Microsoft SQL Server")]
#[test_case(MARIADB; "Maria DB")]
#[test_case(SQLITE_3; "SQLite 3")]
Expand Down

0 comments on commit 22fdcd8

Please sign in to comment.