Skip to content

Commit

Permalink
Fix param format bug (#378)
Browse files Browse the repository at this point in the history
* Fix param format bug

* fix null case

---------

Co-authored-by: mShan0 <96149598+mShan0@users.noreply.github.com>
  • Loading branch information
dauinsight and mShan0 authored Apr 26, 2024
1 parent c1f1dc7 commit f684659
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions mssql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,14 @@ def format_sql(self, sql, params):
return sql

def format_group_by_params(self, query, params):
# Prepare query for string formatting
query = re.sub(r'%\w+', '{}', query)

if params:
# Insert None params directly into the query
if None in params:
null_params = ['NULL' if param is None else '%s' for param in params]
query = query % tuple(null_params)
null_params = ['NULL' if param is None else '{}' for param in params]
query = query.format(*null_params)
params = tuple(p for p in params if p is not None)
params = [(param, type(param)) for param in params]
params_dict = {param: '@var%d' % i for i, param in enumerate(set(params))}
Expand All @@ -634,8 +637,7 @@ def format_group_by_params(self, query, params):
datatype = self._as_sql_type(key[1], key[0])
variables.append("%s %s = %%s " % (value, datatype))
params.append(key[0])
query = ('DECLARE %s \n' % ','.join(variables)) + (query % tuple(args))

query = ('DECLARE %s \n' % ','.join(variables)) + (query.format(*args))
return query, params

def format_params(self, params):
Expand Down

0 comments on commit f684659

Please sign in to comment.