diff options
author | BonfaceKilz | 2021-06-07 11:14:30 +0300 |
---|---|---|
committer | zsloan | 2021-06-18 22:08:04 +0000 |
commit | 3b53e42162fbcbdba782016f7b63604081f2b6b1 (patch) | |
tree | 049884d7297ea3c41809953251fc7c061c0201f0 /gn3 | |
parent | 3abef0f7a14860c0babbf59bd99b4c5a88387693 (diff) | |
download | genenetwork3-3b53e42162fbcbdba782016f7b63604081f2b6b1.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__.py | 9 |
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()) |