Named filter not working
delcroip opened this issue · 2 comments
delcroip commented
There are some features which are not supported yet. Please check the Limitations first to see if your bug is listed.
Software versions
- Django: 4.2
- mssql-django: 1.4.2
- python: 3.10
- SQL Server: 2017
- OS: Linux
if I have a querry with named filter it does not work
.venv/lib/python3.10/site-packages/mssql/base.py", line 637, in format_group_by_params
query = ('DECLARE %s \n' % ','.join(variables)) + (query % tuple(args))
TypeError: format requires a mapping
proposed solution in case params is a dict, it generate an list of param taking the order from the sql
def manage_named_parameters(sql, params):
if isinstance(params, dict):
import re
params = [
params[p]
for p in re.findall(r"%\(([\w]+)\)", sql)
]
sql = re.sub(r"%\([\w]+\)", "%", sql)
return sql, params
else:
return sql, params
delcroip commented
with this data
sql = '''SELECT prod."ProdID" as "ProdID"
FROM "tblProduct" prod
WHERE prod."LocationId" = %(LocationId)s OR %(LocationId)s = 0 ;'''
params = {"LocationId": locationId}
This fails
cur.execute(sql, params)
this doesn't
cur.execute(*manage_named_parameters(sql,params))
hope it helps (the table can be change to anything the key to reproduce is to have a dict as params )
Note: regarding the error management it param[p] should throw a KeyError as it should that why no error management was added