Commit 38e2382
authored
FEAT: Param as Dict (#385)
### 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#40995](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/40995)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #20
-------------------------------------------------------------------
### Summary
This pull request introduces support for both `qmark` (`?`) and
`pyformat` (`%(name)s`) parameter styles in SQL queries, improving
compatibility and usability for users of the `mssql_python` package. The
default `paramstyle` is now set to `pyformat`, and both `execute` and
`executemany` methods have been updated to automatically detect and
convert parameter styles as needed. A new utility module,
`parameter_helper.py`, has been added to handle parameter style parsing
and conversion.
**Parameter style support and conversion:**
* Changed the global `paramstyle` in `mssql_python/__init__.py` from
`"qmark"` to `"pyformat"`, making `pyformat` the default parameter
style.
* Added a new module `mssql_python/parameter_helper.py` containing
helper functions to parse pyformat parameters, convert pyformat SQL to
qmark style, and auto-detect/convert parameter styles in queries.
**Enhancements to parameter handling in cursor methods:**
* Updated the `execute` method in `mssql_python/cursor.py` to
auto-detect and convert parameter styles, supporting both single values
and various parameter formats, and to use the new helper functions for
conversion.
* Refactored parameter flattening logic in `execute` to rely on the new
auto-detection and conversion, removing the old manual flattening.
* Enhanced the `executemany` method in `mssql_python/cursor.py` to
auto-detect parameter style, convert pyformat to qmark for all rows if
needed, and wrap single parameters for backward compatibility.
**Tests and validation:**
* Updated the `test_paramstyle` test in `tests/test_001_globals.py` to
expect the new default `paramstyle` value of `"pyformat"`.1 parent c666f6c commit 38e2382
5 files changed
Lines changed: 2279 additions & 8 deletions
File tree
- mssql_python
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
32 | 37 | | |
33 | 38 | | |
34 | 39 | | |
| |||
1233 | 1238 | | |
1234 | 1239 | | |
1235 | 1240 | | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
1236 | 1288 | | |
1237 | 1289 | | |
1238 | 1290 | | |
| |||
1241 | 1293 | | |
1242 | 1294 | | |
1243 | 1295 | | |
1244 | | - | |
1245 | | - | |
1246 | | - | |
1247 | | - | |
1248 | | - | |
1249 | | - | |
1250 | 1296 | | |
1251 | 1297 | | |
1252 | 1298 | | |
| |||
1933 | 1979 | | |
1934 | 1980 | | |
1935 | 1981 | | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
1936 | 2016 | | |
1937 | 2017 | | |
1938 | 2018 | | |
| |||
0 commit comments