forked from CMS/memberPlumbing
sqlExport: Simplify insert functions with f strings
This commit is contained in:
parent
d5ecf50943
commit
bdf3b84c74
26
sqlExport.py
26
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()
|
||||
|
Loading…
Reference in New Issue
Block a user