about summary refs log tree commit diff
path: root/uploader
diff options
context:
space:
mode:
Diffstat (limited to 'uploader')
-rw-r--r--uploader/phenotypes/models.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/uploader/phenotypes/models.py b/uploader/phenotypes/models.py
index fe4ccac..48e64da 100644
--- a/uploader/phenotypes/models.py
+++ b/uploader/phenotypes/models.py
@@ -310,3 +310,29 @@ def create_new_phenotypes(conn: mdb.Connection,
             } for row in cursor.fetchall())
 
     return _phenos
+
+
+def save_phenotypes_data(
+        conn: mdb.Connection,
+        table: str,
+        data: Iterable[dict]
+) -> tuple[dict, ...]:
+    """Save new phenotypes data into the database."""
+    _table_details = {
+        "PublishData": {"table": "PublishData", "valueCol": "value"},
+        "PublishSE": {"table": "PublishSE", "valueCol": "error"},
+        "NStrain": {"table": "PublishData", "valueCol": "count"}
+    }[table]
+    saved_data = tuple()
+    with conn.cursor(cursorclass=DictCursor) as cursor:
+        for batch in take(data, 5000):
+            cursor.executemany(
+            (f"INSERT INTO {_table_details['table']}"
+             f"(Id, StrainId, {_table_details['valueCol']}) "
+             "VALUES "
+             f"(%(data_id)s, %(sample_id)s, %({_table_details['valueCol']})s) "
+             "RETURNING *"),
+                tuple(batch))
+            _data = data + tuple(cursor.fetchall())
+
+    return saved_data