Merge pull request #1015 from JackDandy/feature/AddMassUpsert

Add db.mass_upsert_sql
This commit is contained in:
JackDandy 2017-11-15 17:28:39 +00:00 committed by GitHub
commit 33d349d765
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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):