[python] yalla

Viewer

  1. from sqlalchemy import create_engine
  2.  
  3. engine = create_engine('your_database_uri')
  4.  
  5. def bulk_upsert(rows):
  6.     conn = engine.connect()
  7.     values = []
  8.     for row in rows:
  9.         values.append({
  10.             'config_id': row['config_id'],
  11.             'port_id': row['port_id'],
  12.             'as_of_date': row['as_of_date'],
  13.             'shell_id': row['shell_id'],
  14.             'start_of_day': row['start_of_day'],
  15.             'swap_index': row['swap_index'],
  16.             'fund_id': row['fund_id'],
  17.             'row_data': row['row_data'],
  18.             'summary_data': row['summary_data'],
  19.             'exceptions': row['exceptions']
  20.         })
  21.     insert_statement = """
  22.         INSERT INTO tstructured_summary (
  23.             config_id, port_id, as_of_date, shell_id, start_of_day,
  24.             swap_index, fund_id, row_data, summary_data, exceptions
  25.         )
  26.         VALUES (
  27.             :config_id, :port_id, :as_of_date, :shell_id, :start_of_day,
  28.             :swap_index, :fund_id, :row_data, :summary_data, :exceptions
  29.         )
  30.         ON CONFLICT (config_id, port_id, as_of_date, shell_id, start_of_day, swap_index, fund_id)
  31.         DO UPDATE SET
  32.             row_data = excluded.row_data,
  33.             summary_data = excluded.summary_data,
  34.             exceptions = excluded.exceptions;
  35.     """
  36.     conn.execute(insert_statement, values)
  37.  

Editor

You can edit this paste and save as new: