Improve wheres for update_filler.

This commit is contained in:
voussoir 2025-03-24 22:18:57 -07:00
parent 8179022785
commit 306060031c

View file

@ -111,11 +111,16 @@ def update_filler(pairs, where_key):
cur.execute(query, bindings) cur.execute(query, bindings)
''' '''
pairs = pairs.copy() pairs = pairs.copy()
where_value = pairs.pop(where_key) if isinstance(where_key, tuple):
wheres = {k: pairs.pop(k) for k in where_key}
else:
wheres = {where_key: pairs.pop(where_key)}
for (where_key, where_value) in wheres.items():
if isinstance(where_value, tuple): if isinstance(where_value, tuple):
(where_value, pairs[where_key]) = where_value (wheres[where_key], pairs[where_key]) = where_value
if isinstance(where_value, dict): if isinstance(where_value, dict):
where_value = where_value['old'] wheres[where_key] = where_value['old']
pairs[where_key] = where_value['new'] pairs[where_key] = where_value['new']
if len(pairs) == 0: if len(pairs) == 0:
@ -129,10 +134,12 @@ def update_filler(pairs, where_key):
else: else:
qmarks.append(f'{key} = ?') qmarks.append(f'{key} = ?')
bindings.append(value) bindings.append(value)
bindings.append(where_value)
wheres = list(wheres.items())
bindings.extend(value for (key, value) in wheres)
wheres = ' AND '.join(f'{key} = ?' for (key, value) in wheres)
setters = ', '.join(qmarks) setters = ', '.join(qmarks)
qmarks = 'SET {setters} WHERE {where_key} == ?' qmarks = f'SET {setters} WHERE {wheres}'
qmarks = qmarks.format(setters=setters, where_key=where_key)
return (qmarks, bindings) return (qmarks, bindings)
def executescript(conn, script): def executescript(conn, script):