Open
Conversation
… jahnvi/setinputsize_with_decimal
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR fixes a crash when binding Python Decimal values to SQL DECIMAL/NUMERIC using setinputsizes() (notably with executemany) by switching to string-based binding and adding regression tests.
Changes:
- Map SQL
DECIMAL/NUMERICtoSQL_C_CHARfor C-type binding and convert PythonDecimalvalues to strings during parameter processing. - Update
executemanypreprocessing to convertDecimalvalues when SQL type isDECIMAL/NUMERIC. - Add new cursor tests covering
setinputsizes()+SQL_DECIMAL/SQL_NUMERICwithexecutemany,execute, andNULL.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| mssql_python/cursor.py | Switches DECIMAL/NUMERIC binding to SQL_C_CHAR and adds Decimal-to-string conversions in parameter handling paths. |
| tests/test_004_cursor.py | Adds regression tests validating Decimal binding via setinputsizes() for execute/executemany and NULL handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
mssql_python/cursor.pyLines 2323-2332 2323 processed_row[i] = format(val, "f")
2324 else:
2325 try:
2326 processed_row[i] = format(decimal.Decimal(str(val)), "f")
! 2327 except Exception as e: # pylint: disable=broad-exception-caught
! 2328 raise ValueError(
2329 f"Failed to convert parameter at row {row}, column {i} to Decimal: {e}"
2330 ) from e
2331 processed_parameters.append(processed_row)📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 67.8%
mssql_python.row.py: 70.5%
mssql_python.pybind.logger_bridge.hpp: 70.8%
mssql_python.pybind.ddbc_bindings.cpp: 74.5%
mssql_python.pybind.connection.connection.cpp: 75.3%
mssql_python.__init__.py: 77.3%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.pybind.connection.connection_pool.cpp: 79.6%
mssql_python.connection.py: 85.2%🔗 Quick Links
|
…/microsoft/mssql-python into jahnvi/setinputsize_with_decimal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
Summary
This pull request improves the handling of Python
Decimalvalues when binding to SQLDECIMALandNUMERICtypes, especially when usingsetinputsizesandexecutemany. It fixes a runtime error by ensuringDecimalobjects are converted to strings for proper binding, and adds comprehensive tests to verify this behavior.Decimal binding and conversion improvements:
DECIMALandNUMERICtypes to useSQL_C_CHARinstead ofSQL_C_NUMERICin_get_c_type_for_sql_type, enabling string-based binding for decimals._create_parameter_types_listandexecutemanyto convert PythonDecimalobjects to strings when binding to SQLDECIMALorNUMERICcolumns.Testing enhancements:
test_004_cursor.pyto verify thatsetinputsizeswithSQL_DECIMALandSQL_NUMERICaccepts PythonDecimalvalues, works with bothexecutemanyandexecute, and correctly handlesNULLvalues.