Commit 7b91e8f
authored
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
-->1 parent f0b5959 commit 7b91e8f
File tree
5 files changed
+230
-53
lines changed- mssql_python
- pybind
- tests
5 files changed
+230
-53
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| |||
1442 | 1442 | | |
1443 | 1443 | | |
1444 | 1444 | | |
1445 | | - | |
1446 | | - | |
1447 | | - | |
1448 | | - | |
1449 | | - | |
1450 | | - | |
1451 | | - | |
1452 | | - | |
1453 | | - | |
1454 | | - | |
1455 | | - | |
1456 | | - | |
1457 | | - | |
1458 | | - | |
1459 | | - | |
1460 | | - | |
1461 | | - | |
1462 | | - | |
1463 | | - | |
1464 | | - | |
1465 | | - | |
1466 | | - | |
1467 | | - | |
1468 | | - | |
1469 | | - | |
1470 | | - | |
1471 | | - | |
1472 | | - | |
1473 | | - | |
1474 | 1445 | | |
1475 | 1446 | | |
1476 | 1447 | | |
| |||
1641 | 1612 | | |
1642 | 1613 | | |
1643 | 1614 | | |
1644 | | - | |
1645 | | - | |
1646 | | - | |
1647 | | - | |
1648 | | - | |
1649 | | - | |
| 1615 | + | |
1650 | 1616 | | |
1651 | 1617 | | |
1652 | 1618 | | |
| |||
Binary file not shown.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
67 | 81 | | |
68 | 82 | | |
69 | 83 | | |
| |||
78 | 92 | | |
79 | 93 | | |
80 | 94 | | |
| 95 | + | |
81 | 96 | | |
82 | 97 | | |
83 | 98 | | |
| |||
91 | 106 | | |
92 | 107 | | |
93 | 108 | | |
| 109 | + | |
94 | 110 | | |
95 | 111 | | |
96 | 112 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
| |||
496 | 498 | | |
497 | 499 | | |
498 | 500 | | |
| 501 | + | |
499 | 502 | | |
500 | 503 | | |
501 | 504 | | |
| |||
1934 | 1937 | | |
1935 | 1938 | | |
1936 | 1939 | | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
1937 | 1987 | | |
1938 | 1988 | | |
1939 | 1989 | | |
| |||
2658 | 2708 | | |
2659 | 2709 | | |
2660 | 2710 | | |
| 2711 | + | |
2661 | 2712 | | |
2662 | 2713 | | |
2663 | 2714 | | |
| |||
2928 | 2979 | | |
2929 | 2980 | | |
2930 | 2981 | | |
| 2982 | + | |
| 2983 | + | |
| 2984 | + | |
| 2985 | + | |
| 2986 | + | |
| 2987 | + | |
| 2988 | + | |
2931 | 2989 | | |
2932 | 2990 | | |
2933 | 2991 | | |
| |||
3143 | 3201 | | |
3144 | 3202 | | |
3145 | 3203 | | |
| 3204 | + | |
| 3205 | + | |
| 3206 | + | |
| 3207 | + | |
| 3208 | + | |
| 3209 | + | |
| 3210 | + | |
| 3211 | + | |
| 3212 | + | |
| 3213 | + | |
| 3214 | + | |
| 3215 | + | |
| 3216 | + | |
| 3217 | + | |
| 3218 | + | |
| 3219 | + | |
| 3220 | + | |
| 3221 | + | |
| 3222 | + | |
| 3223 | + | |
| 3224 | + | |
| 3225 | + | |
| 3226 | + | |
| 3227 | + | |
| 3228 | + | |
| 3229 | + | |
| 3230 | + | |
3146 | 3231 | | |
3147 | 3232 | | |
3148 | 3233 | | |
| |||
3262 | 3347 | | |
3263 | 3348 | | |
3264 | 3349 | | |
| 3350 | + | |
| 3351 | + | |
| 3352 | + | |
3265 | 3353 | | |
3266 | 3354 | | |
3267 | 3355 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3099 | 3099 | | |
3100 | 3100 | | |
3101 | 3101 | | |
3102 | | - | |
3103 | 3102 | | |
3104 | 3103 | | |
3105 | 3104 | | |
| |||
4114 | 4113 | | |
4115 | 4114 | | |
4116 | 4115 | | |
4117 | | - | |
4118 | | - | |
4119 | 4116 | | |
4120 | 4117 | | |
4121 | 4118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7822 | 7822 | | |
7823 | 7823 | | |
7824 | 7824 | | |
| 7825 | + | |
| 7826 | + | |
| 7827 | + | |
| 7828 | + | |
| 7829 | + | |
| 7830 | + | |
| 7831 | + | |
| 7832 | + | |
| 7833 | + | |
| 7834 | + | |
| 7835 | + | |
| 7836 | + | |
| 7837 | + | |
| 7838 | + | |
| 7839 | + | |
| 7840 | + | |
| 7841 | + | |
| 7842 | + | |
| 7843 | + | |
| 7844 | + | |
| 7845 | + | |
| 7846 | + | |
| 7847 | + | |
| 7848 | + | |
| 7849 | + | |
| 7850 | + | |
| 7851 | + | |
| 7852 | + | |
| 7853 | + | |
| 7854 | + | |
| 7855 | + | |
| 7856 | + | |
| 7857 | + | |
| 7858 | + | |
| 7859 | + | |
| 7860 | + | |
| 7861 | + | |
| 7862 | + | |
| 7863 | + | |
| 7864 | + | |
| 7865 | + | |
| 7866 | + | |
| 7867 | + | |
| 7868 | + | |
| 7869 | + | |
| 7870 | + | |
| 7871 | + | |
| 7872 | + | |
| 7873 | + | |
| 7874 | + | |
| 7875 | + | |
| 7876 | + | |
| 7877 | + | |
| 7878 | + | |
| 7879 | + | |
| 7880 | + | |
| 7881 | + | |
| 7882 | + | |
| 7883 | + | |
| 7884 | + | |
| 7885 | + | |
| 7886 | + | |
| 7887 | + | |
| 7888 | + | |
| 7889 | + | |
| 7890 | + | |
| 7891 | + | |
| 7892 | + | |
| 7893 | + | |
| 7894 | + | |
| 7895 | + | |
| 7896 | + | |
| 7897 | + | |
| 7898 | + | |
| 7899 | + | |
| 7900 | + | |
| 7901 | + | |
| 7902 | + | |
| 7903 | + | |
| 7904 | + | |
| 7905 | + | |
| 7906 | + | |
| 7907 | + | |
| 7908 | + | |
| 7909 | + | |
| 7910 | + | |
| 7911 | + | |
| 7912 | + | |
| 7913 | + | |
| 7914 | + | |
| 7915 | + | |
| 7916 | + | |
| 7917 | + | |
| 7918 | + | |
| 7919 | + | |
| 7920 | + | |
| 7921 | + | |
| 7922 | + | |
| 7923 | + | |
| 7924 | + | |
| 7925 | + | |
| 7926 | + | |
| 7927 | + | |
| 7928 | + | |
| 7929 | + | |
| 7930 | + | |
| 7931 | + | |
| 7932 | + | |
| 7933 | + | |
| 7934 | + | |
| 7935 | + | |
| 7936 | + | |
| 7937 | + | |
| 7938 | + | |
| 7939 | + | |
| 7940 | + | |
| 7941 | + | |
| 7942 | + | |
| 7943 | + | |
| 7944 | + | |
| 7945 | + | |
| 7946 | + | |
| 7947 | + | |
| 7948 | + | |
| 7949 | + | |
| 7950 | + | |
7825 | 7951 | | |
7826 | 7952 | | |
7827 | 7953 | | |
| |||
0 commit comments