sqlExport: Simplify insert functions with f strings

This commit is contained in:
Adam Goldsmith 2020-04-01 17:46:41 -04:00
parent d5ecf50943
commit bdf3b84c74

View File

@ -22,22 +22,21 @@ def formatRows(tableMap, data):
def insertFromTableMap(table, data, tableMap): def insertFromTableMap(table, data, tableMap):
# TODO: this could probably be done better as a single statement? # TODO: this could probably be done better as a single statement?
c.executemany( c.executemany(
'INSERT INTO ' + table + ' (' + f"""INSERT INTO {table} ({','.join(f'`{k}`' for k in tableMap.keys())})
','.join(f'`{k}`' for k in tableMap.keys()) + VALUES ({','.join(len(tableMap) * ['%s'])})
') VALUES (' + ','.join(len(tableMap) * ['%s']) + ') ' + ON DUPLICATE KEY UPDATE
'ON DUPLICATE KEY UPDATE ' + {', '.join(f'`{k}`=VALUES(`{k}`)' for k in tableMap.keys())};""",
', '.join(f'`{k}`=VALUES(`{k}`)' for k in tableMap.keys()) + ';',
list(formatRows(tableMap, data))) list(formatRows(tableMap, data)))
def insertFromTableMapWithoutDups(table, data, tableMap): def insertDistinctFromTableMap(table, data, tableMap):
# TODO: this could probably be done better as a single statement? # TODO: this could probably be done better as a single statement?
c.executemany( c.executemany(
'INSERT INTO ' + table + ' (' + f"""INSERT INTO {table} ({','.join(f'`{k}`' for k in tableMap.keys())})
','.join(f'`{k}`' for k in tableMap.keys()) + SELECT {','.join(len(tableMap) * ['%s'])}
') SELECT ' + ','.join(len(tableMap) * ['%s']) + WHERE NOT EXISTS (
' WHERE NOT EXISTS (SELECT 1 from ' + table + SELECT 1 from {table}
' WHERE ' + ' AND '.join(f'`{k}`<=>%s' for k in tableMap.keys()) + WHERE {' AND '.join(f'`{k}`<=>%s' for k in tableMap.keys())}
');' , );""" ,
list([r * 2 for r in formatRows(tableMap, data)])) list([r * 2 for r in formatRows(tableMap, data)]))
@ -130,7 +129,8 @@ try:
assert all([t['Account ID'] == t.get('uid', '') assert all([t['Account ID'] == t.get('uid', '')
and t['Payment ID'] == t.get('sid', '') and t['Payment ID'] == t.get('sid', '')
for t in transactions]) for t in transactions])
insertFromTableMapWithoutDups('transactions', transactions, tableMapping['transactions']) insertDistinctFromTableMap(
'transactions', transactions, tableMapping['transactions'])
print("Committing changes...") print("Committing changes...")
conn.commit() conn.commit()