FEAT: Param as Dict by jahnvi480 · Pull Request #385 · microsoft/mssql-python
Work Item / Issue Reference
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
paramstyleinmssql_python/__init__.pyfrom"qmark"to"pyformat", makingpyformatthe default parameter style. - Added a new module
mssql_python/parameter_helper.pycontaining 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
executemethod inmssql_python/cursor.pyto 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
executeto rely on the new auto-detection and conversion, removing the old manual flattening. - Enhanced the
executemanymethod inmssql_python/cursor.pyto 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_paramstyletest intests/test_001_globals.pyto expect the new defaultparamstylevalue of"pyformat".