Add delete_filler.
This commit is contained in:
parent
53ab58d693
commit
d34aeaaf41
1 changed files with 32 additions and 6 deletions
|
@ -1,3 +1,28 @@
|
||||||
|
def delete_filler(pairs):
|
||||||
|
'''
|
||||||
|
Manually aligning the bindings for DELETE statements is annoying.
|
||||||
|
Given a dictionary of {column: value}, return the "WHERE ..." portion of
|
||||||
|
the query and the bindings in the correct order.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
pairs={'test': 'toast', 'ping': 'pong'}
|
||||||
|
->
|
||||||
|
returns ('WHERE test = ? AND ping = ?', ['toast', 'pong'])
|
||||||
|
|
||||||
|
In context:
|
||||||
|
(qmarks, bindings) = delete_filler(pairs)
|
||||||
|
query = 'DELETE FROM table %s' % qmarks
|
||||||
|
cur.execute(query, bindings)
|
||||||
|
'''
|
||||||
|
qmarks = []
|
||||||
|
bindings = []
|
||||||
|
for (key, value) in pairs.items():
|
||||||
|
qmarks.append('%s = ?' % key)
|
||||||
|
bindings.append(value)
|
||||||
|
qmarks = ' AND '.join(qmarks)
|
||||||
|
qmarks = 'WHERE %s' % qmarks
|
||||||
|
return (qmarks, bindings)
|
||||||
|
|
||||||
def insert_filler(column_names, values, require_all=True):
|
def insert_filler(column_names, values, require_all=True):
|
||||||
'''
|
'''
|
||||||
Manually aligning the bindings for INSERT statements is annoying.
|
Manually aligning the bindings for INSERT statements is annoying.
|
||||||
|
@ -57,8 +82,8 @@ def update_filler(pairs, where_key):
|
||||||
returns ('SET filepath = ? WHERE filepath == ?', ['/newplace', '/oldplace'])
|
returns ('SET filepath = ? WHERE filepath == ?', ['/newplace', '/oldplace'])
|
||||||
|
|
||||||
In context:
|
In context:
|
||||||
(query, bindings) = update_filler(data, where_key)
|
(qmarks, bindings) = update_filler(data, where_key)
|
||||||
query = 'UPDATE table %s' % query
|
query = 'UPDATE table %s' % qmarks
|
||||||
cur.execute(query, bindings)
|
cur.execute(query, bindings)
|
||||||
'''
|
'''
|
||||||
pairs = pairs.copy()
|
pairs = pairs.copy()
|
||||||
|
@ -68,13 +93,14 @@ def update_filler(pairs, where_key):
|
||||||
|
|
||||||
if len(pairs) == 0:
|
if len(pairs) == 0:
|
||||||
raise ValueError('No pairs left after where_key.')
|
raise ValueError('No pairs left after where_key.')
|
||||||
|
|
||||||
qmarks = []
|
qmarks = []
|
||||||
bindings = []
|
bindings = []
|
||||||
for (key, value) in pairs.items():
|
for (key, value) in pairs.items():
|
||||||
qmarks.append('%s = ?' % key)
|
qmarks.append('%s = ?' % key)
|
||||||
bindings.append(value)
|
bindings.append(value)
|
||||||
bindings.append(where_value)
|
bindings.append(where_value)
|
||||||
qmarks = ', '.join(qmarks)
|
setters = ', '.join(qmarks)
|
||||||
query = 'SET {setters} WHERE {where_key} == ?'
|
qmarks = 'SET {setters} WHERE {where_key} == ?'
|
||||||
query = query.format(setters=qmarks, where_key=where_key)
|
qmarks = qmarks.format(setters=setters, where_key=where_key)
|
||||||
return (query, bindings)
|
return (qmarks, bindings)
|
||||||
|
|
Loading…
Reference in a new issue