PgDB._add_where_clauses: Fix handling fields already in params

This commit is contained in:
Benjamin Renard 2021-11-03 17:04:58 +01:00
parent afe54819bb
commit 9d611284ef
2 changed files with 22 additions and 5 deletions

View file

@ -225,12 +225,20 @@ class PgDB:
)
if isinstance(where_clauses, dict):
cls._combine_params(params, where_clauses)
sql_where_clauses = []
for field, value in where_clauses.items():
param = field
if field in params:
idx = 1
while param in params:
param = '%s_%d' % (field, idx)
idx += 1
cls._combine_params(params, {param: value})
sql_where_clauses.append(
'"{field}" = %({param})s'.format(field=field, param=param)
)
return (
(" %s " % where_op).join([
'"{0}" = %({0})s'.format(key)
for key in where_clauses
]),
(" %s " % where_op).join(sql_where_clauses),
params
)
raise PgDBUnsupportedWHEREClauses(where_clauses)

View file

@ -212,6 +212,15 @@ def test_add_where_clauses_with_op():
dict()
)
def test_add_where_clauses_with_duplicated_field():
sql="UPDATE table SET test1=%(test1)s"
params = dict(test1='new_value')
where_clauses = dict(test1='where_value')
assert PgDB._add_where_clauses(sql, params, where_clauses) == (
sql + ' WHERE "test1" = %(test1_1)s',
dict(test1='new_value', test1_1='where_value')
)
def test_insert(mocker, test_pgdb):
values = dict(test1=1, test2=2)
mocker.patch(