aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorBonfaceKilz2021-05-19 22:09:46 +0300
committerBonfaceKilz2021-05-20 23:25:59 +0300
commitc9f6c7b3c3541c1b60781c78aa3e21a02604bfa0 (patch)
treef6b896decbc7790e4a3584bd4efdcac62a2d92c9 /gn3
parent24fa8700fdcb1338a8e05283b63bb282a8c96a8b (diff)
downloadgenenetwork3-c9f6c7b3c3541c1b60781c78aa3e21a02604bfa0.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