aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorBonfaceKilz2021-05-19 22:09:46 +0300
committerzsloan2021-06-18 22:08:04 +0000
commitb30a029cc3c0771a5f5ecaeed663cca24c88f534 (patch)
tree50906759ffd417e2eda78b4e4a806bcdf981d20b /gn3
parentd7571e3c30aa7b1f312fcf975e7336b9f2912709 (diff)
downloadgenenetwork3-b30a029cc3c0771a5f5ecaeed663cca24c88f534.tar.gz
db: phenotypes: Generalise the update function
* gn3/db/phenotypes.py (update_phenotype): Delete it. (update): New, more general function.
Diffstat (limited to 'gn3')
-rw-r--r--gn3/db/phenotypes.py21
1 files changed, 11 insertions, 10 deletions
diff --git a/gn3/db/phenotypes.py b/gn3/db/phenotypes.py
index f6ca944..fdb148b 100644
--- a/gn3/db/phenotypes.py
+++ b/gn3/db/phenotypes.py
@@ -1,4 +1,4 @@
-# pylint: disable=[R0902]
+# pylint: disable=[R0902, R0903]
"""This contains all the necessary functions that access the phenotypes from
the db"""
from dataclasses import dataclass, asdict, astuple
@@ -85,22 +85,23 @@ TABLEMAP = {
}
-def update_phenotype(conn: Any,
- data: Phenotype,
- where: Phenotype) -> Optional[int]:
- """Update phenotype metadata with DATA that depends on the WHERE clause"""
+def update(conn: Any,
+ table: str,
+ data: Dataclass,
+ where: Dataclass) -> Optional[int]:
+ """Run an UPDATE on a table"""
if not any(astuple(data) + astuple(where)):
return None
- sql = "UPDATE Phenotype SET "
- sql += ", ".join(f"{phenotype_column_mapping.get(k)} "
+ sql = f"UPDATE {table} SET "
+ sql += ", ".join(f"{TABLEMAP[table].get(k)} "
f"= '{escape_string(str(v)).decode('utf-8')}'" for
k, v in asdict(data).items()
- if v is not None and k in phenotype_column_mapping)
+ if v is not None and k in TABLEMAP[table])
sql += " WHERE "
- sql += "AND ".join(f"{phenotype_column_mapping.get(k)} = "
+ sql += "AND ".join(f"{TABLEMAP[table].get(k)} = "
f"'{escape_string(str(v)).decode('utf-8')}'" for
k, v in asdict(where).items()
- if v is not None and k in phenotype_column_mapping)
+ if v is not None and k in TABLEMAP[table])
with conn.cursor() as cursor:
cursor.execute(sql)
return cursor.rowcount