aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2021-05-20 21:25:48 +0300
committerzsloan2021-06-18 22:08:04 +0000
commit2face963fcba5b3231e9cc26a38f1370b00eb7b0 (patch)
treea50dce938575169d1310af62bb4468eeff874fd9
parent1b6ed578ac251daf19ca2299870a0e7e9a3eb6cc (diff)
downloadgenenetwork3-2face963fcba5b3231e9cc26a38f1370b00eb7b0.tar.gz
db: phenotypes: Add function for fetching a single result
* gn3/db/phenotypes.py (fetchone): New function.
-rw-r--r--gn3/db/phenotypes.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/gn3/db/phenotypes.py b/gn3/db/phenotypes.py
index c3ad683..96cb275 100644
--- a/gn3/db/phenotypes.py
+++ b/gn3/db/phenotypes.py
@@ -142,3 +142,20 @@ def update(conn: Any,
with conn.cursor() as cursor:
cursor.execute(sql)
return cursor.rowcount
+
+
+def fetchone(conn: Any,
+ table: str,
+ where: Dataclass) -> Optional[Dataclass]:
+ """Run a SELECT on a table. Returns only one result!"""
+ if not any(astuple(where)):
+ return None
+ sql = f"SELECT * FROM {table} "
+ sql += "WHERE "
+ 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 TABLEMAP[table])
+ with conn.cursor() as cursor:
+ cursor.execute(sql)
+ return DATACLASSMAP[table](*cursor.fetchone())