diff options
-rw-r--r-- | gn3/db/__init__.py | 29 | ||||
-rw-r--r-- | gn3/db/phenotypes.py | 35 | ||||
-rw-r--r-- | gn3/db/probesets.py | 14 |
3 files changed, 49 insertions, 29 deletions
diff --git a/gn3/db/__init__.py b/gn3/db/__init__.py index 9a86ad8..b8c6e20 100644 --- a/gn3/db/__init__.py +++ b/gn3/db/__init__.py @@ -43,35 +43,6 @@ class Dataclass(Protocol): __dataclass_fields__: Dict -def update(conn: Any, - table: str, - data: Dataclass, - where: Dataclass) -> Optional[int]: - """Run an UPDATE on a table""" - logger.warning( - "DEPRECATION WARNING: The function `%s.update` is deprecated and will " - "be removed soon. **DO NOT** use it, and remove all references to it " - "from your code.", - __name__) - if not (any(astuple(data)) and any(astuple(where))): - return None - data_ = {k: v for k, v in asdict(data).items() - if v is not None and k in TABLEMAP[table]} - where_ = {k: v for k, v in asdict(where).items() - if v is not None and k in TABLEMAP[table]} - sql = f"UPDATE {table} SET " - sql += ", ".join(f"{TABLEMAP[table].get(k)} " - "= %s" for k in data_.keys()) - sql += " WHERE " - sql += " AND ".join(f"{TABLEMAP[table].get(k)} = " - "%s" for k in where_.keys()) - with conn.cursor() as cursor: - cursor.execute(sql, - tuple(data_.values()) + tuple(where_.values())) - conn.commit() - return cursor.rowcount - - def diff_from_dict(old: Dict, new: Dict) -> Dict: """Construct a new dict with a specific structure that contains the difference between the 2 dicts in the structure: diff --git a/gn3/db/phenotypes.py b/gn3/db/phenotypes.py index b42118a..2b16b17 100644 --- a/gn3/db/phenotypes.py +++ b/gn3/db/phenotypes.py @@ -157,7 +157,42 @@ def update_publication(conn, data=dict) -> int: updatable_cols = ", ".join(f"{publication_mapping[col]}=%({col})s" for col in data if col not in ("id_", "id")) + if not bool(updatable_cols): + return 0 with conn.cursor(cursorclass=DictCursor) as cursor: cursor.execute( f"UPDATE Publication SET {updatable_cols} WHERE Id=%(id_)s", data) return cursor.rowcount + +def update_phenotype(conn, data:dict) -> int: + """Update the `Phenotype` table with the given data.""" + cols = ", ".join(f"{phenotype_mapping[col]}=%({col})s" + for col in data + if col not in ("id_", "id")) + if not bool(cols): + return 0 + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute( + f"UPDATE Phenotype SET {cols} WHERE Id=%(id_)s", data) + return cursor.rowcount + +def update_cross_reference(conn, dataset_id, trait_name, data:dict) -> int: + """Update the `PublishXRef` table with data.""" + cols = ", ".join(f"{publish_x_ref_mapping[col]}=%({col})s" + for col in data + if (col not in ("id_", "id") and + col in publish_x_ref_mapping)) + if not bool(cols): + return 0 + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute( + f"UPDATE PublishXRef SET {cols} WHERE " + "Id=%(trait_name)s AND " + "InbredSetId=" + "(SELECT InbredSetId FROM PublishFreeze WHERE Id=%(dataset_id)s)", + { + "dataset_id": dataset_id, + "trait_name": trait_name, + **data + }) + return cursor.rowcount diff --git a/gn3/db/probesets.py b/gn3/db/probesets.py index 19c6896..57ae452 100644 --- a/gn3/db/probesets.py +++ b/gn3/db/probesets.py @@ -75,3 +75,17 @@ def fetch_probeset_metadata_by_name(conn: DBConnection, name: str) -> dict: "WHERE Name = %(name)s"), {"name": name}) return cursor.fetchone() + +def update_probeset(conn, probeset_id, data:dict) -> int: + """Update the ProbeSet table with given `data`.""" + cols = ", ".join(f"{probeset_mapping[col]}=%({col})s" + for col in data + if (col not in ("id_", "id") and + col in probeset_mapping)) + if not bool(cols): + return 0 + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute( + f"UPDATE ProbeSet SET {cols} WHERE Id=%(probeset_id)s", + {"probeset_id": probeset_id, **data}) + return cursor.rowcount |