Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
c403899
Fix for FetchMany(number of rows) ignores batch size when table conta…
dlevy-msft-sql Nov 25, 2025
f96d14c
First round of code review fixes
dlevy-msft-sql Nov 25, 2025
e135e40
Fix test_fetchone
dlevy-msft-sql Nov 25, 2025
38966d0
Fix for issue 352
dlevy-msft-sql Dec 1, 2025
3f52dd7
Apply fixes from copilot review
dlevy-msft-sql Dec 1, 2025
38b041b
Fix for FetchMany(number of rows) ignores batch size when table conta…
dlevy-msft-sql Nov 25, 2025
139994a
First round of code review fixes
dlevy-msft-sql Nov 25, 2025
6fd823e
Fix test_fetchone
dlevy-msft-sql Nov 25, 2025
a7d9b8e
Fix for issue 352
dlevy-msft-sql Dec 1, 2025
646ef04
Apply fixes from copilot review
dlevy-msft-sql Dec 1, 2025
8018872
Merge branch 'Issue-352' of https://github.com/dlevy-msft-sql/mssql-p…
dlevy-msft-sql Jan 5, 2026
67268e5
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 5, 2026
d335ed0
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 8, 2026
2dc93f3
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 9, 2026
eee5256
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 16, 2026
d289a9b
Merge branch 'microsoft:main' into Issue-352
dlevy-msft-sql Jan 16, 2026
ced5fc2
feat: Add SQLTypeCode for dual-compatible cursor.description type codes
dlevy-msft-sql Jan 19, 2026
0568451
fix: Remove stray comment from constants.py
dlevy-msft-sql Jan 19, 2026
257f0f6
chore: Update black target-version to include py312, py313
dlevy-msft-sql Jan 19, 2026
033f7cd
style: Apply black 26.1.0 formatting and pin version in requirements.txt
dlevy-msft-sql Jan 19, 2026
63c08f5
test: Improve SQLTypeCode equality test for 100% coverage
dlevy-msft-sql Jan 19, 2026
6adeead
chore: Remove unnecessary .gitattributes
dlevy-msft-sql Jan 19, 2026
7b6da4e
chore: Keep black unpinned to always use latest
dlevy-msft-sql Jan 19, 2026
42d3e09
fix: Address Copilot review comments
dlevy-msft-sql Jan 19, 2026
31a6d13
refactor: Use DROP TABLE IF EXISTS for cleaner test cleanup
dlevy-msft-sql Jan 19, 2026
6eb43ea
refactor: Remove dead _map_data_type method
dlevy-msft-sql Jan 20, 2026
327ee42
Sync SQL Server ODBC constants between Python and C++
dlevy-msft-sql Jan 20, 2026
7a487b8
test: Add spatial type error handling and thread safety tests
dlevy-msft-sql Jan 20, 2026
839019c
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 20, 2026
466a4d5
test: Add spatial type error handling and thread safety tests
dlevy-msft-sql Jan 20, 2026
5e67a1b
test: Add spatial type error handling and thread safety tests
dlevy-msft-sql Jan 20, 2026
f484e8d
Merge remote branch, keep table name fix
dlevy-msft-sql Jan 20, 2026
c0c2117
Ran black --line-length=100 mssql_python/ tests/ to fix issue
dlevy-msft-sql Jan 20, 2026
b5460dd
docs: Add sync comment for SQL Server constants
dlevy-msft-sql Jan 20, 2026
bced470
fix: Use per-thread connections in thread safety test (MARS not enabled)
dlevy-msft-sql Jan 20, 2026
3d09f1a
style: Fix black formatting
dlevy-msft-sql Jan 20, 2026
fe2a042
Merge branch 'microsoft:main' into Issue-352
dlevy-msft-sql Jan 22, 2026
9913467
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 22, 2026
c0b619e
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 30, 2026
96b3240
Merge branch 'main' into Issue-352
dlevy-msft-sql Jan 30, 2026
aa6b071
Merge branch 'main' into Issue-352
dlevy-msft-sql Feb 1, 2026
f88b75e
fix: Address Copilot review comments - ODBC 3.x types, hash contract,…
dlevy-msft-sql Feb 1, 2026
0011cf2
fix: Address latest Copilot review comments
dlevy-msft-sql Feb 1, 2026
f41a0cb
fix: Use named constants for ODBC 3.x types and narrow test exception
dlevy-msft-sql Feb 1, 2026
fb30fc6
style: Fix black formatting
dlevy-msft-sql Feb 1, 2026
4761dda
fix: Handle SQLTypeCode in output converter methods
dlevy-msft-sql Feb 1, 2026
c5a4ae3
docs: Fix SQL_DATETIMEOFFSET comment to reference SQL_SS_TIMESTAMPOFFSET
dlevy-msft-sql Feb 1, 2026
0b3b36c
fix: Strengthen geography test assertion and update UDT log message
dlevy-msft-sql Feb 1, 2026
117b3b4
style: Fix black formatting
dlevy-msft-sql Feb 1, 2026
fb9c3e6
style: Fix black formatting
dlevy-msft-sql Feb 1, 2026
6a77ed4
fix: Address Copilot review comments
dlevy-msft-sql Feb 1, 2026
151893f
style: Apply black formatting to test_004_cursor.py
dlevy-msft-sql Feb 1, 2026
f28f94d
fix: Address Copilot review comments in ddbc_bindings.cpp
dlevy-msft-sql Feb 1, 2026
bbd4e50
style: Fix black formatting in test_004_cursor.py
dlevy-msft-sql Feb 1, 2026
85dc297
fix: Widen type stubs to Union[SQLTypeCode, type] for description typ…
dlevy-msft-sql Feb 1, 2026
98503ad
fix: Address Copilot review comments
dlevy-msft-sql Feb 1, 2026
6fc30c1
style: Fix black formatting
dlevy-msft-sql Feb 1, 2026
8e282db
fix: Update add_output_converter type hint to Union[int, SQLTypeCode]
dlevy-msft-sql Feb 2, 2026
d57ec4e
fix: Address Copilot review - clear _column_metadata in _reset_cursor…
dlevy-msft-sql Feb 2, 2026
080138a
fix: Remove line number reference from test comment
dlevy-msft-sql Feb 2, 2026
d17d9b1
fix: Remove duplicate DDBCSQLDescribeCol call in execute()
dlevy-msft-sql Feb 2, 2026
d54aea5
fix: Clear cached maps in _reset_cursor to prevent stale converters
dlevy-msft-sql Feb 2, 2026
965b1e1
fix: Add Python type fallback for output converter lookup
dlevy-msft-sql Feb 2, 2026
b2f1d5c
fix: Address Copilot review - add type to Union, use constants, add d…
dlevy-msft-sql Feb 2, 2026
dfd68fa
style: Fix black formatting in test_004_cursor.py
dlevy-msft-sql Feb 2, 2026
d656121
fix: Add SQL_WVARCHAR fallback for str/bytes and remove duplicate dat…
dlevy-msft-sql Feb 2, 2026
deb4242
style: Fix black formatting in test_004_cursor.py
dlevy-msft-sql Feb 2, 2026
2b6af43
fix: Only forward int sqltypes to native conn, use SQL_WVARCHAR constant
dlevy-msft-sql Feb 2, 2026
5ce031a
fix: Widen add/get/remove_output_converter annotations to include type
dlevy-msft-sql Feb 2, 2026
73f47ae
fix: Only forward int sqltypes to native remove_output_converter
dlevy-msft-sql Feb 2, 2026
496c3d1
docs: Fix add_output_converter docstring to reflect actual value types
dlevy-msft-sql Feb 2, 2026
0dfa606
fix: Add python_type fallback for get_output_converter, guard SQL_SS_…
dlevy-msft-sql Feb 2, 2026
dee3852
Merge branch 'main' into Issue-352
dlevy-msft-sql Feb 2, 2026
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
33 changes: 32 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,53 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]

### Added

- New feature: Support for macOS and Linux.
- Documentation: Added API documentation in the Wiki.
- Added support for SQL Server spatial data types (geography, geometry, hierarchyid) via SQL_SS_UDT type handling.
- Added `SQLTypeCode` class for dual-compatible type codes in `cursor.description`.

### Changed

- Improved error handling in the connection module.
- Enhanced `cursor.description[i][1]` to return `SQLTypeCode` objects that compare equal to both SQL type integers and Python types, maintaining full backwards compatibility while aligning with DB-API 2.0.

### Fixed

- Bug fix: Resolved issue with connection timeout.
- Fixed `cursor.description` type handling for better DB-API 2.0 compliance (Issue #352).

### SQLTypeCode Usage

The `type_code` field in `cursor.description` now returns `SQLTypeCode` objects that support both comparison styles:

```python
cursor.execute("SELECT id, name FROM users")
desc = cursor.description

# Style 1: Compare with Python types (backwards compatible with pandas, etc.)
if desc[0][1] == int:
print("Integer column")

# Style 2: Compare with SQL type codes (DB-API 2.0 compliant)
from mssql_python.constants import ConstantsDDBC as sql_types
if desc[0][1] == sql_types.SQL_INTEGER.value: # or just == 4
print("Integer column")

# Get the raw SQL type code
type_code = int(desc[0][1]) # Returns 4 for SQL_INTEGER
```

## [1.0.0-alpha] - 2025-02-24

### Added

- Initial release of the mssql-python driver for SQL Server.

### Changed

- N/A

### Fixed
- N/A

- N/A
2 changes: 1 addition & 1 deletion mssql_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
from .connection_string_builder import _ConnectionStringBuilder

# Cursor Objects
from .cursor import Cursor
from .cursor import Cursor, SQLTypeCode

# Logging Configuration (Simplified single-level DEBUG system)
from .logging import logger, setup_logging, driver_logger
Expand Down
63 changes: 49 additions & 14 deletions mssql_python/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

if TYPE_CHECKING:
from mssql_python.row import Row
from mssql_python.cursor import SQLTypeCode

# Add SQL_WMETADATA constant for metadata decoding configuration
SQL_WMETADATA: int = -99 # Special flag for column name decoding
Expand Down Expand Up @@ -923,7 +924,9 @@ def cursor(self) -> Cursor:
logger.debug("cursor: Cursor created successfully - total_cursors=%d", len(self._cursors))
return cursor

def add_output_converter(self, sqltype: int, func: Callable[[Any], Any]) -> None:
def add_output_converter(
self, sqltype: "Union[int, SQLTypeCode, type]", func: Callable[[Any], Any]
) -> None:
"""
Register an output converter function that will be called whenever a value
with the given SQL type is read from the database.
Expand All @@ -936,32 +939,47 @@ def add_output_converter(self, sqltype: int, func: Callable[[Any], Any]) -> None
vulnerabilities. This API should never be exposed to untrusted or external input.

Args:
sqltype (int): The integer SQL type value to convert, which can be one of the
defined standard constants (e.g. SQL_VARCHAR) or a database-specific
value (e.g. -151 for the SQL Server 2008 geometry data type).
sqltype (int, SQLTypeCode, or type): The integer SQL type value to convert, which can be
one of the defined standard constants (e.g. SQL_VARCHAR) or a
database-specific value (e.g. -151 for the SQL Server 2008
geometry data type). Also accepts SQLTypeCode objects (from
cursor.description) or Python types (e.g., str, int) for
backward compatibility.
func (callable): The converter function which will be called with a single parameter,
the value, and should return the converted value. If the value is NULL
then the parameter passed to the function will be None, otherwise it
will be a bytes object.
then the parameter passed to the function will be None. For string/binary
columns, the value will be bytes (UTF-16LE encoded for strings). For other
types (int, decimal.Decimal, datetime, etc.), the value will be the native
Python object.

Returns:
None
"""
# Handle SQLTypeCode objects (from cursor.description) by converting to int
if hasattr(sqltype, "type_code"):
sqltype = sqltype.type_code
with self._converters_lock:
self._output_converters[sqltype] = func
# Pass to the underlying connection if native implementation supports it
if hasattr(self._conn, "add_output_converter"):
# Only forward int type codes to native layer; Python type keys are handled
# only in our Python-side dictionary
if isinstance(sqltype, int) and hasattr(self._conn, "add_output_converter"):
self._conn.add_output_converter(sqltype, func)
logger.info(f"Added output converter for SQL type {sqltype}")

def get_output_converter(self, sqltype: Union[int, type]) -> Optional[Callable[[Any], Any]]:
def get_output_converter(
self, sqltype: "Union[int, SQLTypeCode, type]"
) -> Optional[Callable[[Any], Any]]:
"""
Get the output converter function for the specified SQL type.

Thread-safe implementation that protects the converters dictionary with a lock.

Args:
sqltype (int or type): The SQL type value or Python type to get the converter for
sqltype (int, SQLTypeCode, or type): The SQL type value to get the converter for.
Also accepts SQLTypeCode objects (from cursor.description), which are
automatically converted to their integer type code, or Python types
(e.g., str, int) for backward compatibility.

Returns:
callable or None: The converter function or None if no converter is registered
Expand All @@ -970,26 +988,43 @@ def get_output_converter(self, sqltype: Union[int, type]) -> Optional[Callable[[
⚠️ The returned converter function will be executed on database values. Only use
converters from trusted sources.
"""
# Handle SQLTypeCode objects (from cursor.description) by converting to int
# SQLTypeCode has a type_code attribute and supports int() conversion
original_sqltype = sqltype
if hasattr(sqltype, "type_code"):
sqltype = sqltype.type_code
with self._converters_lock:
return self._output_converters.get(sqltype)

def remove_output_converter(self, sqltype: Union[int, type]) -> None:
result = self._output_converters.get(sqltype)
# If int lookup misses for an SQLTypeCode, also try its python_type
# to preserve backward compatibility with converters registered by Python type
if result is None and hasattr(original_sqltype, "python_type"):
result = self._output_converters.get(original_sqltype.python_type)
return result

def remove_output_converter(self, sqltype: "Union[int, SQLTypeCode, type]") -> None:
"""
Remove the output converter function for the specified SQL type.

Thread-safe implementation that protects the converters dictionary with a lock.

Args:
sqltype (int or type): The SQL type value to remove the converter for
sqltype (int, SQLTypeCode, or type): The SQL type value to remove the converter for.
Also accepts SQLTypeCode objects (from cursor.description) or Python types
(e.g., str, int) for backward compatibility.

Returns:
None
"""
# Handle SQLTypeCode objects (from cursor.description) by converting to int
if hasattr(sqltype, "type_code"):
sqltype = sqltype.type_code
with self._converters_lock:
if sqltype in self._output_converters:
del self._output_converters[sqltype]
# Pass to the underlying connection if native implementation supports it
if hasattr(self._conn, "remove_output_converter"):
# Only forward int type codes to native layer; Python type keys are handled
# only in our Python-side dictionary
if isinstance(sqltype, int) and hasattr(self._conn, "remove_output_converter"):
self._conn.remove_output_converter(sqltype)
logger.info(f"Removed output converter for SQL type {sqltype}")

Expand Down
5 changes: 5 additions & 0 deletions mssql_python/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ class ConstantsDDBC(Enum):
SQL_FETCH_ABSOLUTE = 5
SQL_FETCH_RELATIVE = 6
SQL_FETCH_BOOKMARK = 8
# NOTE: The following SQL Server-specific type constants MUST stay in sync with
# the corresponding values in mssql_python/pybind/ddbc_bindings.cpp
SQL_DATETIMEOFFSET = -155
SQL_SS_TIME2 = -154 # SQL Server TIME(n) type
SQL_SS_UDT = -151 # SQL Server User-Defined Types (geometry, geography, hierarchyid)
SQL_SS_XML = -152 # SQL Server XML type
SQL_C_SS_TIMESTAMPOFFSET = 0x4001
SQL_SCOPE_CURROW = 0
SQL_BEST_ROWID = 1
Expand Down
Loading
Loading