From bdf3b84c74cda2b4dc6278776d509e0f7e8bdf24 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 1 Apr 2020 17:46:41 -0400 Subject: [PATCH] sqlExport: Simplify insert functions with f strings --- sqlExport.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/sqlExport.py b/sqlExport.py index 2e37f51..abb64ee 100755 --- a/sqlExport.py +++ b/sqlExport.py @@ -22,22 +22,21 @@ def formatRows(tableMap, data): def insertFromTableMap(table, data, tableMap): # TODO: this could probably be done better as a single statement? c.executemany( - 'INSERT INTO ' + table + ' (' + - ','.join(f'`{k}`' for k in tableMap.keys()) + - ') VALUES (' + ','.join(len(tableMap) * ['%s']) + ') ' + - 'ON DUPLICATE KEY UPDATE ' + - ', '.join(f'`{k}`=VALUES(`{k}`)' for k in tableMap.keys()) + ';', + f"""INSERT INTO {table} ({','.join(f'`{k}`' for k in tableMap.keys())}) + VALUES ({','.join(len(tableMap) * ['%s'])}) + ON DUPLICATE KEY UPDATE + {', '.join(f'`{k}`=VALUES(`{k}`)' for k in tableMap.keys())};""", 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? c.executemany( - 'INSERT INTO ' + table + ' (' + - ','.join(f'`{k}`' for k in tableMap.keys()) + - ') SELECT ' + ','.join(len(tableMap) * ['%s']) + - ' WHERE NOT EXISTS (SELECT 1 from ' + table + - ' WHERE ' + ' AND '.join(f'`{k}`<=>%s' for k in tableMap.keys()) + - ');' , + f"""INSERT INTO {table} ({','.join(f'`{k}`' for k in tableMap.keys())}) + SELECT {','.join(len(tableMap) * ['%s'])} + WHERE NOT EXISTS ( + SELECT 1 from {table} + WHERE {' AND '.join(f'`{k}`<=>%s' for k in tableMap.keys())} + );""" , list([r * 2 for r in formatRows(tableMap, data)])) @@ -130,7 +129,8 @@ try: assert all([t['Account ID'] == t.get('uid', '') and t['Payment ID'] == t.get('sid', '') for t in transactions]) - insertFromTableMapWithoutDups('transactions', transactions, tableMapping['transactions']) + insertDistinctFromTableMap( + 'transactions', transactions, tableMapping['transactions']) print("Committing changes...") conn.commit()