Skip to content

Commit

Permalink
add db_encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
yuana1 authored and Konstantin Salikhov committed Jun 27, 2019
1 parent df4c2c9 commit 9b6cdaa
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ where
native_error: result.native_error,
message_length,
message,
message_string: unsafe { ::environment::ENCODING.decode(&message[0..(message_length as usize + 1)]).0.into_owned()},
message_string: unsafe { ::environment::OS_ENCODING.decode(&message[0..(message_length as usize + 1)]).0.into_owned()},
})
}
NoData(()) => None,
Expand Down
6 changes: 4 additions & 2 deletions src/environment/list_data_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ impl Environment<Version3> {
match try_into_option(result, self)? {
Some((len1, len2)) => unsafe {
Ok(Some((
::environment::ENCODING.decode(&buf1[0..(len1 as usize)]).0,
::environment::ENCODING.decode(&buf2[0..(len2 as usize)]).0,
::environment::DB_ENCODING.decode(&buf1[0..(len1 as usize)]).0,
::environment::DB_ENCODING.decode(&buf2[0..(len2 as usize)]).0,
)))
}
None => Ok(None),
Expand All @@ -191,6 +191,7 @@ impl Environment<Version3> {
);

loop {

match result {
safe::ReturnOption::Success((buf1_length_out, buf2_length_out)) |
safe::ReturnOption::Info((buf1_length_out, buf2_length_out)) => {
Expand Down Expand Up @@ -220,6 +221,7 @@ impl Environment<Version3> {

#[cfg(test)]
mod test {

use super::*;

#[test]
Expand Down
8 changes: 5 additions & 3 deletions src/environment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std;
/// Environment state used to represent that environment has been set to odbc version 3
pub type Version3 = safe::Odbc3;

pub static mut ENCODING : &encoding_rs::Encoding = encoding_rs::UTF_8;
pub static mut OS_ENCODING: &encoding_rs::Encoding = encoding_rs::UTF_8;
pub static mut DB_ENCODING: &encoding_rs::Encoding = encoding_rs::UTF_8;

/// Handle to an ODBC Environment
///
Expand Down Expand Up @@ -98,11 +99,12 @@ pub fn create_environment_v3()
}


pub fn create_environment_v3_with_os_encoding(encoding: String)
pub fn create_environment_v3_with_os_db_encoding(os_encoding: &str, db_encoding: &str)
-> std::result::Result<Environment<Version3>, Option<DiagnosticRecord>>
{
unsafe {
ENCODING = encoding_rs::Encoding::for_label(encoding.as_bytes()).unwrap();
OS_ENCODING = encoding_rs::Encoding::for_label(os_encoding.as_bytes()).unwrap();
DB_ENCODING = encoding_rs::Encoding::for_label(db_encoding.as_bytes()).unwrap();
}
Environment::new()
}
6 changes: 3 additions & 3 deletions src/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl Raii<ffi::Stmt> {
&mut nullable as *mut ffi::Nullable,
) {
SQL_SUCCESS => Return::Success(ColumnDescriptor {
name: ::environment::ENCODING.decode(&name_buffer[..(name_length as usize)]).0
name: ::environment::DB_ENCODING.decode(&name_buffer[..(name_length as usize)]).0
.to_string(),
data_type: data_type,
column_size: if column_size == 0 {
Expand All @@ -309,7 +309,7 @@ impl Raii<ffi::Stmt> {
},
}),
SQL_SUCCESS_WITH_INFO => Return::SuccessWithInfo(ColumnDescriptor {
name: ::environment::ENCODING.decode(&name_buffer[..(name_length as usize)]).0
name: ::environment::DB_ENCODING.decode(&name_buffer[..(name_length as usize)]).0
.to_string(),
data_type: data_type,
column_size: if column_size == 0 {
Expand All @@ -336,7 +336,7 @@ impl Raii<ffi::Stmt> {
}

fn exec_direct(&mut self, statement_text: &str) -> Return<bool> {
let bytes = unsafe { crate::environment::ENCODING }.encode(statement_text).0;
let bytes = unsafe { crate::environment::DB_ENCODING }.encode(statement_text).0;

let length = bytes.len();
if length > ffi::SQLINTEGER::max_value() as usize {
Expand Down
3 changes: 1 addition & 2 deletions src/statement/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ impl<'a, 'b> Statement<'a, 'b, Prepared, NoResult> {

impl Raii<ffi::Stmt> {
fn prepare(&mut self, sql_text: &str) -> Return<()> {

let bytes = unsafe { crate::environment::ENCODING }.encode(sql_text).0;
let bytes = unsafe { crate::environment::DB_ENCODING }.encode(sql_text).0;
match unsafe {
ffi::SQLPrepare(
self.handle(),
Expand Down
14 changes: 6 additions & 8 deletions src/statement/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@ unsafe impl<'a> OdbcType<'a> for String {
}

fn convert(buffer: &'a [u8]) -> Self {
unsafe {::environment::ENCODING.decode(buffer).0}
.to_string()
unsafe { ::environment::DB_ENCODING }.decode(buffer).0.to_string()
}

fn column_size(&self) -> ffi::SQLULEN {
self.as_bytes().len() as ffi::SQLULEN
unsafe { ::environment::DB_ENCODING }.encode(&self).0.len() as ffi::SQLULEN
}

fn value_ptr(&self) -> ffi::SQLPOINTER {
self.as_bytes().as_ptr() as *const Self as ffi::SQLPOINTER
unsafe { ::environment::DB_ENCODING }.encode(&self).0.as_ptr() as *const Self as ffi::SQLPOINTER
}

fn null_bytes_count() -> usize {
Expand All @@ -170,16 +169,15 @@ unsafe impl<'a> OdbcType<'a> for ::std::borrow::Cow<'a, str> {
}

fn convert(buffer: &'a [u8]) -> Self {
let aa = unsafe {::environment::ENCODING.decode(buffer).0};
aa
unsafe {::environment::DB_ENCODING.decode(buffer).0}
}

fn column_size(&self) -> ffi::SQLULEN {
self.as_bytes().len() as ffi::SQLULEN
unsafe { ::environment::DB_ENCODING }.encode(self).0.len() as ffi::SQLULEN
}

fn value_ptr(&self) -> ffi::SQLPOINTER {
self.as_bytes().as_ptr() as *const Self as ffi::SQLPOINTER
unsafe { ::environment::DB_ENCODING }.encode(self).0.as_ptr() as *const Self as ffi::SQLPOINTER
}

fn null_bytes_count() -> usize {
Expand Down

0 comments on commit 9b6cdaa

Please sign in to comment.