FEAT: Param as Dict by jahnvi480 · Pull Request #385 · microsoft/mssql-python

@jahnvi480 jahnvi480 commented

Dec 22, 2025

edited by azure-boards bot

Loading

Work Item / Issue Reference

AB#40995

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".