From 3b53e42162fbcbdba782016f7b63604081f2b6b1 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Mon, 7 Jun 2021 11:14:30 +0300 Subject: gn3: db: Make "WHERE" clause optional * gn3/db/__init__.py (fetchone): Make "WHERE" an Optional arg. --- gn3/db/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'gn3') 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()) -- cgit v1.2.3