aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2021-05-20 21:25:48 +0300
committerBonfaceKilz2021-05-20 23:25:59 +0300
commit9de02a89cf4d134a4b289e5a799e0880c0e5840f (patch)
treee49f04f6564f4a887426861b15d5b339fc90b971
parentda3ae0871271e9b88766ed94cb9b3f0e00d19088 (diff)
downloadgenenetwork3-9de02a89cf4d134a4b289e5a799e0880c0e5840f.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())