aboutsummaryrefslogtreecommitdiff
path: root/gn3/db/phenotypes.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/db/phenotypes.py')
-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())