-
Notifications
You must be signed in to change notification settings - Fork 27
FEAT: uniqueidentifier support in executemany() #245
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
Conversation
7b10bf4 to
32e7d04
Compare
f05ee81 to
763f32f
Compare
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 adds comprehensive support for handling Python uuid.UUID objects with SQL Server's UNIQUEIDENTIFIER type in the mssql_python driver. It enables seamless conversion between Python UUIDs and SQL GUIDs for both single and batch operations.
- Implements UUID parameter binding for
executemany()operations with proper type conversion and validation - Enhances data retrieval to return UUID columns as Python
uuid.UUIDobjects instead of raw data - Adds comprehensive test coverage for UUID operations including mixed UUID/NULL scenarios
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| mssql_python/pybind/ddbc_bindings.cpp | Adds UUID parameter binding logic for batch operations and improves UUID data retrieval with NULL handling |
| mssql_python/cursor.py | Removes unnecessary whitespace in executemany method |
| tests/test_004_cursor.py | Adds comprehensive test cases for UUID operations in both single and batch scenarios |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
merging main branch with this to test out code coverage reports and comments |
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
mssql_python/pybind/ddbc_bindings.cppLines 1977-1997 1977 strLenOrIndArray[i] = SQL_NULL_DATA;
1978 continue;
1979 }
1980 else if (py::isinstance<py::bytes>(element)) {
! 1981 py::bytes b = element.cast<py::bytes>();
! 1982 if (PyBytes_GET_SIZE(b.ptr()) != 16) {
! 1983 ThrowStdException("UUID binary data must be exactly 16 bytes long.");
! 1984 }
! 1985 std::memcpy(uuid_bytes.data(), PyBytes_AS_STRING(b.ptr()), 16);
! 1986 }
1987 else if (py::isinstance(element, uuid_class)) {
1988 py::bytes b = element.attr("bytes_le").cast<py::bytes>();
1989 std::memcpy(uuid_bytes.data(), PyBytes_AS_STRING(b.ptr()), 16);
1990 }
! 1991 else {
! 1992 ThrowStdException(MakeParamMismatchErrorStr(info.paramCType, paramIndex));
! 1993 }
1994 guidArray[i].Data1 = (static_cast<uint32_t>(uuid_bytes[3]) << 24) |
1995 (static_cast<uint32_t>(uuid_bytes[2]) << 16) |
1996 (static_cast<uint32_t>(uuid_bytes[1]) << 8) |
1997 (static_cast<uint32_t>(uuid_bytes[0]));Lines 2004-2012 2004 }
2005 dataPtr = guidArray;
2006 bufferLength = sizeof(SQLGUID);
2007 break;
! 2008 }
2009 default: {
2010 ThrowStdException("BindParameterArray: Unsupported C type: " + std::to_string(info.paramCType));
2011 }
2012 }Lines 3139-3149 3139 }
3140 case SQL_GUID: {
3141 SQLLEN indicator = buffers.indicators[col - 1][i];
3142 if (indicator == SQL_NULL_DATA) {
! 3143 row.append(py::none());
! 3144 break;
! 3145 }
3146 SQLGUID* guidValue = &buffers.guidBuffers[col - 1][i];
3147 uint8_t reordered[16];
3148 reordered[0] = ((char*)&guidValue->Data1)[3];
3149 reordered[1] = ((char*)&guidValue->Data1)[2];📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)mssql_python.pybind.ddbc_bindings.cpp: 67.7%
mssql_python.pybind.connection.connection.cpp: 68.3%
mssql_python.ddbc_bindings.py: 68.5%
mssql_python.cursor.py: 78.8%
mssql_python.pybind.connection.connection_pool.cpp: 78.9%
mssql_python.connection.py: 81.7%
mssql_python.helpers.py: 84.7%
mssql_python.auth.py: 85.3%
mssql_python.type.py: 86.8%
mssql_python.pooling.py: 88.8%🔗 Quick Links
|
sumitmsft
left a comment
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.
Left a comment for test cases
2bcee36 to
14b9b4f
Compare
14b9b4f to
eebd6de
Compare
Work Item / Issue Reference
Summary
This pull request adds full support for handling Python
uuid.UUIDobjects with SQL Server'sUNIQUEIDENTIFIERtype in themssql_pythondriver. It introduces robust conversion between Python UUIDs and SQL GUIDs for both single and batch operations, ensures correct retrieval as Pythonuuid.UUIDobjects, and adds comprehensive tests for these scenarios.UUID/GUID Support Improvements:
uuid.UUIDto SQLGUIDtypes in_map_sql_type, enabling seamless parameter binding for UUIDs.BindParameters) and batch (BindParameterArray) parameter bindings, including validation of binary length and error handling. [1] [2]uuid.UUIDobjects instead of strings or raw bytes, in both single-row (SQLGetData_wrap) and batch (FetchBatchData) fetches. [1] [2]Testing Enhancements:
None(NULL) values and mixed UUID/NULL batches.uuidmodule for use in new test cases.