You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FEAT: datetimeoffset support in executemany() (#260)
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below (e.g.
AB#37452)
For external contributors: Insert Github Issue number below (e.g. #149)
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
>
[AB#34944](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34944)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #<ISSUE_NUMBER>
-------------------------------------------------------------------
### Summary
<!-- Insert your summary of changes below. Minimum 10 characters
required. -->
This pull request adds comprehensive support for the SQL Server
`DATETIMEOFFSET` type in both the Python and C++ layers of the codebase.
It introduces new constants, enhances the datetime parsing logic to
handle timezone-aware datetimes, and updates the parameter binding and
data fetching logic to correctly handle `DATETIMEOFFSET` values. The
changes ensure accurate round-trip of timezone-aware datetime values
between Python and SQL Server.
**Support for DATETIMEOFFSET type:**
* Added `SQL_DATETIMEOFFSET` and `SQL_C_SS_TIMESTAMPOFFSET` constants to
both `mssql_python/constants.py` and the C++ bindings
(`ddbc_bindings.cpp`).
[[1]](diffhunk://#diff-e6d80f1000af6fd5afca05f435b11fd82df7f5c3e75ecf5763f85d3aacdbe758R127-R128)
[[2]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1L15-R22)
* Introduced a new `DateTimeOffset` C++ struct and integrated it into
buffer management for column data.
[[1]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R67-R80)
[[2]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R95)
[[3]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R109)
* Updated the C++ parameter binding logic to correctly marshal Python
timezone-aware `datetime` objects as `DATETIMEOFFSET`, including array
binding for `executemany`.
[[1]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R482-R514)
[[2]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R1881)
[[3]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R1904-R1964)
* Implemented fetching and conversion logic for `DATETIMEOFFSET`
columns, ensuring Python receives timezone-aware `datetime` objects in
UTC.
[[1]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R2598-R2647)
[[2]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R2904-R2910)
[[3]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R3126-R3162)
**Datetime parsing and mapping improvements:**
* Enhanced `_parse_datetime` in `mssql_python/cursor.py` to support
parsing timezone-aware datetime strings, improving compatibility with
`DATETIMEOFFSET`.
* Updated SQL type mapping logic to distinguish between naive and
timezone-aware `datetime` objects, mapping the latter to
`DATETIMEOFFSET`.
**Refactoring and cleanup:**
* Removed the now-unused `_select_best_sample_value` static method from
`mssql_python/cursor.py` as type inference is now handled differently.
* Adjusted type inference during `executemany` to use the new
`_compute_column_type` method, aligning with the improved datetime
handling.
<!--
### PR Title Guide
> For feature requests
FEAT: (short-description)
> For non-feature requests like test case updates, config updates ,
dependency updates etc
CHORE: (short-description)
> For Fix requests
FIX: (short-description)
> For doc update requests
DOC: (short-description)
> For Formatting, indentation, or styling update
STYLE: (short-description)
> For Refactor, without any feature changes
REFACTOR: (short-description)
> For release related changes, without any feature changes
RELEASE: #<RELEASE_VERSION> (short-description)
### Contribution Guidelines
External contributors:
- Create a GitHub issue first:
https://github.com/microsoft/mssql-python/issues/new
- Link the GitHub issue in the "GitHub Issue" section above
- Follow the PR title format and provide a meaningful summary
mssql-python maintainers:
- Create an ADO Work Item following internal processes
- Link the ADO Work Item in the "ADO Work Item" section above
- Follow the PR title format and provide a meaningful summary
-->
0 commit comments