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()