Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions mssql_python/pybind/ddbc_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "connection/connection_pool.h"

#include <cstdint>
#include <cstring> // For std::memcpy
#include <iomanip> // std::setw, std::setfill
#include <iostream>
#include <utility> // std::forward
Expand Down Expand Up @@ -2463,10 +2464,11 @@ static py::object FetchLobColumnData(SQLHSTMT hStmt,
} else {
// Wide characters
size_t wcharSize = sizeof(SQLWCHAR);
if (bytesRead >= wcharSize) {
auto sqlwBuf = reinterpret_cast<const SQLWCHAR*>(chunk.data());
if (bytesRead >= wcharSize && (bytesRead % wcharSize == 0)) {
size_t wcharCount = bytesRead / wcharSize;
while (wcharCount > 0 && sqlwBuf[wcharCount - 1] == 0) {
std::vector<SQLWCHAR> alignedBuf(wcharCount);
std::memcpy(alignedBuf.data(), chunk.data(), bytesRead);
while (wcharCount > 0 && alignedBuf[wcharCount - 1] == 0) {
--wcharCount;
bytesRead -= wcharSize;
}
Expand Down Expand Up @@ -2495,14 +2497,18 @@ static py::object FetchLobColumnData(SQLHSTMT hStmt,
}
if (isWideChar) {
#if defined(_WIN32)
std::wstring wstr(reinterpret_cast<const wchar_t*>(buffer.data()), buffer.size() / sizeof(wchar_t));
size_t wcharCount = buffer.size() / sizeof(wchar_t);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add some comments around the changes? also, can add a pointer referencing a small description of the s360 issue.

std::vector<wchar_t> alignedBuf(wcharCount);
std::memcpy(alignedBuf.data(), buffer.data(), buffer.size());
std::wstring wstr(alignedBuf.data(), wcharCount);
std::string utf8str = WideToUTF8(wstr);
return py::str(utf8str);
#else
// Linux/macOS handling
size_t wcharCount = buffer.size() / sizeof(SQLWCHAR);
const SQLWCHAR* sqlwBuf = reinterpret_cast<const SQLWCHAR*>(buffer.data());
std::wstring wstr = SQLWCHARToWString(sqlwBuf, wcharCount);
std::vector<SQLWCHAR> alignedBuf(wcharCount);
std::memcpy(alignedBuf.data(), buffer.data(), buffer.size());
std::wstring wstr = SQLWCHARToWString(alignedBuf.data(), wcharCount);
std::string utf8str = WideToUTF8(wstr);
return py::str(utf8str);
#endif
Expand Down Expand Up @@ -2623,8 +2629,7 @@ SQLRETURN SQLGetData_wrap(SqlHandlePtr StatementHandle, SQLUSMALLINT colCount, p
uint64_t numCharsInData = dataLen / sizeof(SQLWCHAR);
if (numCharsInData < dataBuffer.size()) {
#if defined(__APPLE__) || defined(__linux__)
const SQLWCHAR* sqlwBuf = reinterpret_cast<const SQLWCHAR*>(dataBuffer.data());
std::wstring wstr = SQLWCHARToWString(sqlwBuf, numCharsInData);
std::wstring wstr = SQLWCHARToWString(dataBuffer.data(), numCharsInData);
std::string utf8str = WideToUTF8(wstr);
row.append(py::str(utf8str));
#else
Expand Down