PgDB._add_where_clauses: Fix handling fields already in params
This commit is contained in:
parent
afe54819bb
commit
9d611284ef
2 changed files with 22 additions and 5 deletions
|
@ -225,12 +225,20 @@ class PgDB:
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(where_clauses, dict):
|
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 (
|
return (
|
||||||
(" %s " % where_op).join([
|
(" %s " % where_op).join(sql_where_clauses),
|
||||||
'"{0}" = %({0})s'.format(key)
|
|
||||||
for key in where_clauses
|
|
||||||
]),
|
|
||||||
params
|
params
|
||||||
)
|
)
|
||||||
raise PgDBUnsupportedWHEREClauses(where_clauses)
|
raise PgDBUnsupportedWHEREClauses(where_clauses)
|
||||||
|
|
|
@ -212,6 +212,15 @@ def test_add_where_clauses_with_op():
|
||||||
dict()
|
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):
|
def test_insert(mocker, test_pgdb):
|
||||||
values = dict(test1=1, test2=2)
|
values = dict(test1=1, test2=2)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
|
|
Loading…
Reference in a new issue