Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named filter not working #386

Open
delcroip opened this issue Apr 25, 2024 · 2 comments
Open

Named filter not working #386

delcroip opened this issue Apr 25, 2024 · 2 comments

Comments

@delcroip
Copy link

delcroip commented Apr 25, 2024

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
@mShan0
Copy link
Contributor

mShan0 commented Apr 25, 2024

Hi @delcroip, can you provide an example so I can try to reproduce this locally?

@delcroip
Copy link
Author

delcroip commented Apr 26, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants