mirror of
https://github.com/SickGear/SickGear.git
synced 2025-01-05 17:43:37 +00:00
Add db.mass_upsert_sql
Example usage: my_db = db.DBConnection() cl = [] for i in list: cl.extend(db.mass_upsert_sql( 'tablename', {'fieldname': 'some value'}, {'where clause field(s)': 'value'})) if cl: my_db.mass_action(cl)
This commit is contained in:
parent
b4ca844f60
commit
0216658231
1 changed files with 23 additions and 0 deletions
|
@ -47,6 +47,29 @@ def dbFilename(filename='sickbeard.db', suffix=None):
|
|||
return ek.ek(os.path.join, sickbeard.DATA_DIR, filename)
|
||||
|
||||
|
||||
def mass_upsert_sql(tableName, valueDict, keyDict):
|
||||
|
||||
"""
|
||||
use with cl.extend(mass_upsert_sql(tableName, valueDict, keyDict))
|
||||
|
||||
:param tableName: table name
|
||||
:param valueDict: dict of values to be set {'table_fieldname': value}
|
||||
:param keyDict: dict of restrains for update {'table_fieldname': value}
|
||||
:return: list of 2 sql command
|
||||
"""
|
||||
cl = []
|
||||
|
||||
genParams = lambda myDict: [x + ' = ?' for x in myDict.keys()]
|
||||
|
||||
cl.append(['UPDATE [%s] SET %s WHERE %s' % (
|
||||
tableName, ', '.join(genParams(valueDict)), ' AND '.join(genParams(keyDict))), valueDict.values() + keyDict.values()])
|
||||
|
||||
|
||||
cl.append(['INSERT INTO [' + tableName + '] (' + ', '.join(["'%s'" % ('%s' % v).replace("'", "''") for v in valueDict.keys() + keyDict.keys()]) + ')' +
|
||||
' SELECT ' + ', '.join(["'%s'" % ('%s' % v).replace("'", "''") for v in valueDict.values() + keyDict.values()]) + ' WHERE changes() = 0'])
|
||||
return cl
|
||||
|
||||
|
||||
class DBConnection(object):
|
||||
def __init__(self, filename='sickbeard.db', suffix=None, row_type=None):
|
||||
|
||||
|
|
Loading…
Reference in a new issue