aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorBonfaceKilz2021-06-07 11:14:30 +0300
committerBonfaceKilz2021-06-07 19:58:44 +0300
commitdf9711a7441f9885335592f349607256ee6e1095 (patch)
tree43d50100ba1927d71803919db543fa4bf06a87fb /gn3
parent983796eaa560a07edbf6d044c3d29c6a737f6ff4 (diff)
downloadgenenetwork3-df9711a7441f9885335592f349607256ee6e1095.tar.gz
gn3: db: Make "WHERE" clause optional
* gn3/db/__init__.py (fetchone): Make "WHERE" an Optional arg.
Diffstat (limited to 'gn3')
-rw-r--r--gn3/db/__init__.py9
1 files changed, 5 insertions, 4 deletions
diff --git a/gn3/db/__init__.py b/gn3/db/__init__.py
index 997a230..d311dea 100644
--- a/gn3/db/__init__.py
+++ b/gn3/db/__init__.py
@@ -61,16 +61,17 @@ def update(conn: Any,
def fetchone(conn: Any,
table: str,
- where: Dataclass) -> Optional[Dataclass]:
+ where: Optional[Dataclass]) -> Optional[Dataclass]:
"""Run a SELECT on a table. Returns only one result!"""
if not any(astuple(where)):
return None
where_ = {k: v for k, v in asdict(where).items()
if v is not None and k in TABLEMAP[table]}
sql = f"SELECT * FROM {table} "
- sql += "WHERE "
- sql += " AND ".join(f"{TABLEMAP[table].get(k)} = "
- "%s" for k in where_.keys())
+ if where:
+ sql += "WHERE "
+ sql += " AND ".join(f"{TABLEMAP[table].get(k)} = "
+ "%s" for k in where_.keys())
with conn.cursor() as cursor:
cursor.execute(sql, tuple(where_.values()))
return DATACLASSMAP[table](*cursor.fetchone())