Skip to content

FIX: Fixing Level 3 and level 4 compiler warnings. #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 5 additions & 6 deletions mssql_python/pybind/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static SqlHandlePtr getEnvHandle() {
if (!SQL_SUCCEEDED(ret)) {
ThrowStdException("Failed to set environment attributes");
}
return std::make_shared<SqlHandle>(SQL_HANDLE_ENV, env);
return std::make_shared<SqlHandle>(static_cast<SQLSMALLINT>(SQL_HANDLE_ENV), env);
}();

return envHandle;
Expand All @@ -54,7 +54,7 @@ void Connection::allocateDbcHandle() {
LOG("Allocate SQL Connection Handle");
SQLRETURN ret = SQLAllocHandle_ptr(SQL_HANDLE_DBC, _envHandle->get(), &dbc);
checkError(ret);
_dbcHandle = std::make_shared<SqlHandle>(SQL_HANDLE_DBC, dbc);
_dbcHandle = std::make_shared<SqlHandle>(static_cast<SQLSMALLINT>(SQL_HANDLE_DBC), dbc);
}

void Connection::connect(const py::dict& attrs_before) {
Expand Down Expand Up @@ -91,7 +91,7 @@ void Connection::disconnect() {
void Connection::checkError(SQLRETURN ret) const{
if (!SQL_SUCCEEDED(ret)) {
ErrorInfo err = SQLCheckError_Wrap(SQL_HANDLE_DBC, _dbcHandle, ret);
std::string errorMsg = std::string(err.ddbcErrorMsg.begin(), err.ddbcErrorMsg.end());
std::string errorMsg = WideToUTF8(err.ddbcErrorMsg);
ThrowStdException(errorMsg);
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ void Connection::setAutocommit(bool enable) {
}
SQLINTEGER value = enable ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF;
LOG("Set SQL Connection Attribute");
SQLRETURN ret = SQLSetConnectAttr_ptr(_dbcHandle->get(), SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)value, 0);
SQLRETURN ret = SQLSetConnectAttr_ptr(_dbcHandle->get(), SQL_ATTR_AUTOCOMMIT, reinterpret_cast<SQLPOINTER>(static_cast<SQLULEN>(value)), 0);
checkError(ret);
_autocommit = enable;
}
Expand All @@ -148,7 +148,7 @@ SqlHandlePtr Connection::allocStatementHandle() {
SQLHANDLE stmt = nullptr;
SQLRETURN ret = SQLAllocHandle_ptr(SQL_HANDLE_STMT, _dbcHandle->get(), &stmt);
checkError(ret);
return std::make_shared<SqlHandle>(SQL_HANDLE_STMT, stmt);
return std::make_shared<SqlHandle>(static_cast<SQLSMALLINT>(SQL_HANDLE_STMT), stmt);
}


Expand Down Expand Up @@ -214,7 +214,6 @@ bool Connection::reset() {
ThrowStdException("Connection handle not allocated");
}
LOG("Resetting connection via SQL_ATTR_RESET_CONNECTION");
SQLULEN reset = SQL_TRUE;
SQLRETURN ret = SQLSetConnectAttr_ptr(
_dbcHandle->get(),
SQL_ATTR_RESET_CONNECTION,
Expand Down
57 changes: 33 additions & 24 deletions mssql_python/pybind/ddbc_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
}
// TODO: can be moved to python by registering SQL_DATE_STRUCT in pybind
SQL_DATE_STRUCT* sqlDatePtr = AllocateParamBuffer<SQL_DATE_STRUCT>(paramBuffers);
sqlDatePtr->year = param.attr("year").cast<int>();
sqlDatePtr->month = param.attr("month").cast<int>();
sqlDatePtr->day = param.attr("day").cast<int>();
sqlDatePtr->year = static_cast<SQLSMALLINT>(param.attr("year").cast<int>());
sqlDatePtr->month = static_cast<SQLUSMALLINT>(param.attr("month").cast<int>());
sqlDatePtr->day = static_cast<SQLUSMALLINT>(param.attr("day").cast<int>());
dataPtr = static_cast<void*>(sqlDatePtr);
break;
}
Expand All @@ -333,9 +333,9 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
}
// TODO: can be moved to python by registering SQL_TIME_STRUCT in pybind
SQL_TIME_STRUCT* sqlTimePtr = AllocateParamBuffer<SQL_TIME_STRUCT>(paramBuffers);
sqlTimePtr->hour = param.attr("hour").cast<int>();
sqlTimePtr->minute = param.attr("minute").cast<int>();
sqlTimePtr->second = param.attr("second").cast<int>();
sqlTimePtr->hour = static_cast<SQLUSMALLINT>(param.attr("hour").cast<int>());
sqlTimePtr->minute = static_cast<SQLUSMALLINT>(param.attr("minute").cast<int>());
sqlTimePtr->second = static_cast<SQLUSMALLINT>(param.attr("second").cast<int>());
dataPtr = static_cast<void*>(sqlTimePtr);
break;
}
Expand All @@ -346,12 +346,12 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
}
SQL_TIMESTAMP_STRUCT* sqlTimestampPtr =
AllocateParamBuffer<SQL_TIMESTAMP_STRUCT>(paramBuffers);
sqlTimestampPtr->year = param.attr("year").cast<int>();
sqlTimestampPtr->month = param.attr("month").cast<int>();
sqlTimestampPtr->day = param.attr("day").cast<int>();
sqlTimestampPtr->hour = param.attr("hour").cast<int>();
sqlTimestampPtr->minute = param.attr("minute").cast<int>();
sqlTimestampPtr->second = param.attr("second").cast<int>();
sqlTimestampPtr->year = static_cast<SQLSMALLINT>(param.attr("year").cast<int>());
sqlTimestampPtr->month = static_cast<SQLUSMALLINT>(param.attr("month").cast<int>());
sqlTimestampPtr->day = static_cast<SQLUSMALLINT>(param.attr("day").cast<int>());
sqlTimestampPtr->hour = static_cast<SQLUSMALLINT>(param.attr("hour").cast<int>());
sqlTimestampPtr->minute = static_cast<SQLUSMALLINT>(param.attr("minute").cast<int>());
sqlTimestampPtr->second = static_cast<SQLUSMALLINT>(param.attr("second").cast<int>());
// SQL server supports in ns, but python datetime supports in µs
sqlTimestampPtr->fraction = static_cast<SQLUINTEGER>(
param.attr("microsecond").cast<int>() * 1000); // Convert µs to ns
Expand All @@ -372,7 +372,7 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
decimalPtr->scale = decimalParam.scale;
decimalPtr->sign = decimalParam.sign;
// Convert the integer decimalParam.val to char array
std:memset(static_cast<void*>(decimalPtr->val), 0, sizeof(decimalPtr->val));
std::memset(static_cast<void*>(decimalPtr->val), 0, sizeof(decimalPtr->val));
std::memcpy(static_cast<void*>(decimalPtr->val),
reinterpret_cast<char*>(&decimalParam.val),
sizeof(decimalParam.val));
Expand All @@ -395,8 +395,11 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
assert(SQLBindParameter_ptr && SQLGetStmtAttr_ptr && SQLSetDescField_ptr);

RETCODE rc = SQLBindParameter_ptr(
hStmt, paramIndex + 1 /* 1-based indexing */, paramInfo.inputOutputType,
paramInfo.paramCType, paramInfo.paramSQLType, paramInfo.columnSize,
hStmt,
static_cast<SQLUSMALLINT>(paramIndex + 1), /* 1-based indexing */
static_cast<SQLUSMALLINT>(paramInfo.inputOutputType),
static_cast<SQLSMALLINT>(paramInfo.paramCType),
static_cast<SQLSMALLINT>(paramInfo.paramSQLType), paramInfo.columnSize,
paramInfo.decimalDigits, dataPtr, bufferLength, strLenOrIndPtr);
if (!SQL_SUCCEEDED(rc)) {
LOG("Error when binding parameter - {}", paramIndex);
Expand All @@ -406,7 +409,7 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
// https://learn.microsoft.com/en-us/sql/odbc/reference/appendixes/retrieve-numeric-data-sql-numeric-struct-kb222831?view=sql-server-ver16#sql_c_numeric-overview
if (paramInfo.paramCType == SQL_C_NUMERIC) {
SQLHDESC hDesc = nullptr;
RETCODE rc = SQLGetStmtAttr_ptr(hStmt, SQL_ATTR_APP_PARAM_DESC, &hDesc, 0, NULL);
rc = SQLGetStmtAttr_ptr(hStmt, SQL_ATTR_APP_PARAM_DESC, &hDesc, 0, NULL);
if(!SQL_SUCCEEDED(rc)) {
LOG("Error when getting statement attribute - {}", paramIndex);
return rc;
Expand Down Expand Up @@ -469,6 +472,14 @@ void LOG(const std::string& formatString, Args&&... args) {
logging.attr("debug")(message);
}

std::string WideToUTF8(const std::wstring& wstr) {
if (wstr.empty()) return {};
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
std::string result(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), result.data(), size_needed, nullptr, nullptr);
return result;
}

// TODO: Add more nuanced exception classes
void ThrowStdException(const std::string& message) { throw std::runtime_error(message); }

Expand Down Expand Up @@ -523,8 +534,7 @@ std::wstring LoadDriverOrThrowException() {
}

// Convert wstring to string for logging
std::string dllDirStr(dllDir.begin(), dllDir.end());
LOG("Attempting to load driver from - {}", dllDirStr);
LOG("Attempting to load driver from - {}", WideToUTF8(dllDir));

HMODULE hModule = LoadLibraryW(dllDir.c_str());
if (!hModule) {
Expand Down Expand Up @@ -597,7 +607,6 @@ std::wstring LoadDriverOrThrowException() {
SQLDisconnect_ptr && SQLFreeStmt_ptr && SQLGetDiagRec_ptr;

if (!success) {
LOG("Failed to load required function pointers from driver - {}", dllDirStr);
ThrowStdException("Failed to load required function pointers from driver");
}
LOG("Successfully loaded function pointers from driver");
Expand Down Expand Up @@ -860,7 +869,7 @@ SQLRETURN SQLGetData_wrap(SqlHandlePtr StatementHandle, SQLUSMALLINT colCount, p
DriverLoader::getInstance().loadDriver(); // Load the driver
}

SQLRETURN ret;
SQLRETURN ret = SQL_SUCCESS;
SQLHSTMT hStmt = StatementHandle->get();
for (SQLSMALLINT i = 1; i <= colCount; ++i) {
SQLWCHAR columnName[256];
Expand Down Expand Up @@ -1137,7 +1146,7 @@ SQLRETURN SQLGetData_wrap(SqlHandlePtr StatementHandle, SQLUSMALLINT colCount, p
if (SQL_SUCCEEDED(ret)) {
// TODO: Refactor these if's across other switches to avoid code duplication
if (dataLen > 0) {
if (dataLen <= columnSize) {
if (static_cast<size_t>(dataLen) <= columnSize) {
row.append(py::bytes(reinterpret_cast<const char*>(
dataBuffer.get()), dataLen));
} else {
Expand Down Expand Up @@ -1551,7 +1560,7 @@ SQLRETURN FetchBatchData(SQLHSTMT hStmt, ColumnBuffers& buffers, py::list& colum
// TODO: variable length data needs special handling, this logic wont suffice
SQLULEN columnSize = columnMeta["ColumnSize"].cast<SQLULEN>();
HandleZeroColumnSizeAtFetch(columnSize);
if (dataLen <= columnSize) {
if (static_cast<size_t>(dataLen) <= columnSize) {
row.append(py::bytes(reinterpret_cast<const char*>(
&buffers.charBuffers[col - 1][i * columnSize]),
dataLen));
Expand Down Expand Up @@ -1702,7 +1711,7 @@ SQLRETURN FetchMany_wrap(SqlHandlePtr StatementHandle, py::list& rows, int fetch
}

SQLULEN numRowsFetched;
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)fetchSize, 0);
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)(intptr_t)fetchSize, 0);
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, &numRowsFetched, 0);

ret = FetchBatchData(hStmt, buffers, columnNames, rows, numCols, numRowsFetched);
Expand Down Expand Up @@ -1790,7 +1799,7 @@ SQLRETURN FetchAll_wrap(SqlHandlePtr StatementHandle, py::list& rows) {
}

SQLULEN numRowsFetched;
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)fetchSize, 0);
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)(intptr_t)fetchSize, 0);
SQLSetStmtAttr_ptr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, &numRowsFetched, 0);

while (ret != SQL_NO_DATA) {
Expand Down
2 changes: 2 additions & 0 deletions mssql_python/pybind/ddbc_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,5 @@ struct ErrorInfo {
std::wstring ddbcErrorMsg;
};
ErrorInfo SQLCheckError_Wrap(SQLSMALLINT handleType, SqlHandlePtr handle, SQLRETURN retcode);

std::string WideToUTF8(const std::wstring& wstr);
Loading