-
Notifications
You must be signed in to change notification settings - Fork 25
FEAT: Adding set_attr in connection class #177
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
base: main
Are you sure you want to change the base?
Conversation
… jahnvi/conn_setencoding
…t/mssql-python into jahnvi/conn_setdecoding
…t/mssql-python into jahnvi/conn_set_attr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's discuss the current implementation. Want to understand what we plan to do here.
Also, has this been thoroughly tested?
Parking this API for now need to validate the behavior of few attributes before giving support. (AUTOCOMMIT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request introduces comprehensive support for setting ODBC connection attributes through a new set_attr
method, providing pyodbc-compatible functionality for configuring connection behavior. The implementation includes robust input validation, error handling, and proper timing constraints for different attribute types.
- Added
set_attr
method to Connection class with input validation and error handling - Implemented comprehensive connection attribute constants and transaction isolation levels
- Enhanced C++ backend with improved attribute setting capabilities and error reporting
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
File | Description |
---|---|
tests/test_003_connection.py | Added extensive test cases for setencoding, setdecoding, and set_attr functionality |
mssql_python/pybind/ddbc_bindings.cpp | Registered set_attr method with Python bindings |
mssql_python/pybind/connection/connection.h | Added setAttribute as public method and setAttr to ConnectionHandle |
mssql_python/pybind/connection/connection.cpp | Enhanced setAttribute implementation with better error handling and type support |
mssql_python/helpers.py | Added validate_attribute_value function for input validation |
mssql_python/constants.py | Added connection attribute constants and timing validation helpers |
mssql_python/connection.py | Implemented set_attr method in Python Connection class |
mssql_python/init.py | Exported connection attribute constants for public API |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
} | ||
else if (py::isinstance<py::str>(value)) { | ||
try { | ||
static std::vector<std::wstring> wstr_buffers; // Keep buffers alive |
Copilot
AI
Oct 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using static vectors for buffer management can lead to memory leaks and thread safety issues. Consider using a more controlled approach like thread-local storage or instance-based buffer management.
Copilot uses AI. Check for mistakes.
static std::vector<std::string> buffers; | ||
std::string binary_data = value.cast<std::string>(); | ||
|
||
// Limit static buffer growth | ||
constexpr size_t MAX_BUFFER_COUNT = 100; | ||
if (buffers.size() >= MAX_BUFFER_COUNT) { | ||
// Remove oldest 50% of entries when limit reached | ||
buffers.erase(buffers.begin(), buffers.begin() + (MAX_BUFFER_COUNT / 2)); | ||
} | ||
|
||
buffers.emplace_back(std::move(binary_data)); | ||
SQLPOINTER ptr = const_cast<char*>(buffers.back().c_str()); | ||
SQLINTEGER length = static_cast<SQLINTEGER>(buffers.back().size()); | ||
|
Copilot
AI
Oct 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same static buffer issue as with wstr_buffers. This pattern duplicates the memory management problem for binary data.
static std::vector<std::string> buffers; | |
std::string binary_data = value.cast<std::string>(); | |
// Limit static buffer growth | |
constexpr size_t MAX_BUFFER_COUNT = 100; | |
if (buffers.size() >= MAX_BUFFER_COUNT) { | |
// Remove oldest 50% of entries when limit reached | |
buffers.erase(buffers.begin(), buffers.begin() + (MAX_BUFFER_COUNT / 2)); | |
} | |
buffers.emplace_back(std::move(binary_data)); | |
SQLPOINTER ptr = const_cast<char*>(buffers.back().c_str()); | |
SQLINTEGER length = static_cast<SQLINTEGER>(buffers.back().size()); | |
std::string binary_data = value.cast<std::string>(); | |
SQLPOINTER ptr = const_cast<char*>(binary_data.c_str()); | |
SQLINTEGER length = static_cast<SQLINTEGER>(binary_data.size()); | |
Copilot uses AI. Check for mistakes.
Work Item / Issue Reference
Summary
This pull request introduces comprehensive support for setting ODBC connection attributes in the
mssql_python
library, aligning its functionality with pyodbc'sset_attr
API. The changes include new constants for connection attributes, transaction isolation levels, and related options, as well as robust error handling and input validation in both Python and C++ layers. This enables users to configure connection behavior (e.g., autocommit, isolation level, timeouts) in a standardized and secure manner.Connection Attribute Support
mssql_python/__init__.py
andmssql_python/constants.py
, making them available for use in Python code.set_attr
method in theConnection
Python class, providing pyodbc-compatible functionality for setting connection attributes with detailed input validation and error handling.C++ Backend Enhancements
setAttribute
as a public method in the C++Connection
class, and added a newsetAttr
method inConnectionHandle
, with improved error reporting and range validation for SQLUINTEGER values.set_attr
method with the Python bindings, making it accessible from Python code.Code Cleanup and Refactoring
ConstantsDDBC
to improve maintainability, and removed legacy/unused constants.These changes provide a robust interface for configuring ODBC connection attributes, improve compatibility with pyodbc, and enhance error handling for attribute operations.