diff options
Diffstat (limited to 'uploader/publications/models.py')
-rw-r--r-- | uploader/publications/models.py | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/uploader/publications/models.py b/uploader/publications/models.py index 89da06c..3fc9542 100644 --- a/uploader/publications/models.py +++ b/uploader/publications/models.py @@ -1,7 +1,17 @@ """Module to handle persistence and retrieval of publication to/from MariaDB""" +import logging + +from MySQLdb.cursors import DictCursor + +from gn_libs.mysqldb import Connection, debug_query + +logger = logging.getLogger(__name__) + def fetch_phenotype_publications( - conn, ids: tuple[tuple[int, int], ...]) -> tuple[dict, ...]: + conn: Connection, + ids: tuple[tuple[int, int], ...] +) -> tuple[dict, ...]: """Fetch publication from database by ID.""" paramstr = ",".join(["(%s, %s)"] * len(ids)) query = ( @@ -13,3 +23,51 @@ def fetch_phenotype_publications( with conn.cursor(cursorclass=DictCursor) as cursor: cursor.execute(query, tuple(item for row in ids for item in row)) return tuple(dict(row) for row in cursor.fetchall()) + + +def create_new_publications( + conn: Connection, + publications: tuple[dict, ...] +) -> tuple[dict, ...]: + if len(publications) > 0: + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.executemany( + ("INSERT INTO " + "Publication( " + "PubMed_ID, Abstract, Authors, Title, Journal, Volume, Pages, " + "Month, Year" + ") " + "VALUES(" + "%(pubmed_id)s, %(abstract)s, %(authors)s, %(title)s, " + "%(journal)s, %(volume)s, %(pages)s, %(month)s, %(year)s" + ") " + "ON DUPLICATE KEY UPDATE " + "Abstract=VALUES(Abstract), Authors=VALUES(Authors), " + "Title=VALUES(Title), Journal=VALUES(Journal), " + "Volume=VALUES(Volume), Pages=VALUES(pages), " + "Month=VALUES(Month), Year=VALUES(Year) " + "RETURNING *"), + publications) + return tuple({ + **row, "PublicationId": row["Id"] + } for row in cursor.fetchall()) + return tuple() + + +def update_publications(conn: Connection , publications: tuple[dict, ...]) -> tuple[dict, ...]: + """Update details for multiple publications""" + if len(publications) > 0: + with conn.cursor(cursorclass=DictCursor) as cursor: + logger.debug("UPDATING PUBLICATIONS: %s", publications) + cursor.executemany( + ("UPDATE Publication SET " + "PubMed_ID=%(pubmed_id)s, Abstract=%(abstract)s, " + "Authors=%(authors)s, Title=%(title)s, Journal=%(journal)s, " + "Volume=%(volume)s, Pages=%(pages)s, Month=%(month)s, " + "Year=%(year)s " + "WHERE Id=%(publication_id)s"), + publications) + debug_query(cursor, logger) + return publications + return tuple() + return tuple() |