From dbf1ac15c8c698f34a1be6d0f00e62571ad4d1f9 Mon Sep 17 00:00:00 2001 From: proller Date: Fri, 1 Feb 2019 19:12:59 +0300 Subject: [PATCH] UNICODE fixes (#154) --- driver/config_cmake.h.in | 3 +++ driver/environment.cpp | 2 +- driver/odbc.cpp | 2 +- driver/utils.h | 10 ++++++++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/driver/config_cmake.h.in b/driver/config_cmake.h.in index 78f2aebaf..b59298772 100644 --- a/driver/config_cmake.h.in +++ b/driver/config_cmake.h.in @@ -13,6 +13,9 @@ #cmakedefine01 ODBC_IODBC #cmakedefine01 ODBC_UNIXODBC #cmakedefine01 SQL_WCHART_CONVERT +#if !SQL_WCHART_CONVERT +# undef SQL_WCHART_CONVERT +#endif #cmakedefine ODBC_LIBRARIES "@ODBC_LIBRARIES@" #cmakedefine ODBC_INCLUDE_DIRECTORIES "@ODBC_INCLUDE_DIRECTORIES@" diff --git a/driver/environment.cpp b/driver/environment.cpp index 43da176ab..511a2ceda 100644 --- a/driver/environment.cpp +++ b/driver/environment.cpp @@ -86,7 +86,7 @@ Environment::Environment() { # endif report += " sizeof(SQLTCHAR)=" + std::to_string(sizeof(SQLTCHAR)) + " sizeof(wchar_t)=" + std::to_string(sizeof(wchar_t)); #endif -#if SQL_WCHART_CONVERT +#if defined(SQL_WCHART_CONVERT) report += " SQL_WCHART_CONVERT"; #endif #if ODBCVER diff --git a/driver/odbc.cpp b/driver/odbc.cpp index 6019649b2..3fb331458 100644 --- a/driver/odbc.cpp +++ b/driver/odbc.cpp @@ -484,7 +484,7 @@ RETCODE SQL_API SQLBindCol(HSTMT statement_handle, return doWith(statement_handle, [&](Statement & statement) { if (column_number < 1 || column_number > statement.result.getNumColumns()) - throw SqlException("Column number is out of range", "07009"); + throw SqlException("Column number " + std::to_string(column_number) + " is out of range: " + std::to_string(statement.result.getNumColumns()), "07009"); if (out_value_max_size < 0) throw SqlException("Invalid string or buffer length", "HY090"); diff --git a/driver/utils.h b/driver/utils.h index d5f8cba01..07433bc22 100644 --- a/driver/utils.h +++ b/driver/utils.h @@ -134,11 +134,13 @@ void stringToTCHAR(const std::string & data, STRING (&result)[Len]) #if defined(UNICODE) # if ODBC_WCHAR using CharType = wchar_t; + using StringType = std::wstring; # else using CharType = char16_t; + using StringType = std::u16string; # endif - std::wstring tmp = std::wstring_convert, CharType>().from_bytes(data); + StringType tmp = std::wstring_convert, CharType>().from_bytes(data); //using type_to = wchar_t*; #else const auto & tmp = data; @@ -146,7 +148,11 @@ void stringToTCHAR(const std::string & data, STRING (&result)[Len]) #endif const size_t len = std::min(Len - 1, data.size()); #if defined(UNICODE) - wcsncpy(reinterpret_cast(result), tmp.c_str(), len); +# if ODBC_WCHAR + wcsncpy(reinterpret_cast(result), tmp.c_str(), len); +#else + strncpy(reinterpret_cast(result), reinterpret_cast(tmp.c_str()), len * sizeof(CharType)); // WRONG TODO +#endif #else strncpy(reinterpret_cast(result), tmp.c_str(), len); #endif