Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions mbid_mapping/mapping/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from time import asctime

import psycopg2
from psycopg2 import sql
from psycopg2.extras import execute_values
from psycopg2.errors import OperationalError

Expand All @@ -27,11 +28,17 @@ def insert_rows(curs, table, values, cols=None):
Helper function to insert a large number of rows into postgres in one go.
'''

if not isinstance(table, str) or not table:
raise ValueError("table must be a non-empty string")

table_sql = sql.SQL('.').join(sql.Identifier(part) for part in table.split('.'))

if cols is not None and len(cols) > 0:
query = "INSERT INTO " + table + " (" + ",".join(cols) + ") VALUES %s"
cols_sql = sql.SQL(',').join(sql.Identifier(col) for col in cols)
query = sql.SQL("INSERT INTO {} ({}) VALUES %s").format(table_sql, cols_sql)
execute_values(curs, query, values, template=None)
else:
query = "INSERT INTO " + table + " VALUES %s"
query = sql.SQL("INSERT INTO {} VALUES %s").format(table_sql)
execute_values(curs, query, values, template=None)


Expand Down