From e67316601be4b43da6f60322fef1f4ce15ed5905 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Mon, 7 Jun 2021 19:43:17 +0300 Subject: gn3: db: Fix how columns from tables is resolved --- gn3/db/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gn3/db/__init__.py b/gn3/db/__init__.py index 25ecfd6..34a0236 100644 --- a/gn3/db/__init__.py +++ b/gn3/db/__init__.py @@ -65,12 +65,12 @@ def fetchone(conn: Any, """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() + where_ = {TABLEMAP[table].get(k): v for k, v in asdict(where).items() if v is not None and k in TABLEMAP[table]} sql = f"SELECT * FROM {table} " if where: sql += "WHERE " - sql += " AND ".join(f"{TABLEMAP[table].get(k)} = " + sql += " AND ".join(f"{k} = " "%s" for k in where_.keys()) with conn.cursor() as cursor: cursor.execute(sql, tuple(where_.values())) @@ -83,12 +83,12 @@ def fetchall(conn: Any, """Run a SELECT on a table. Returns all the results as a tuple!""" if not any(astuple(where)): return None - where_ = {k: v for k, v in asdict(where).items() + where_ = {TABLEMAP[table].get(k): v for k, v in asdict(where).items() if v is not None and k in TABLEMAP[table]} sql = f"SELECT * FROM {table} " if where: sql += "WHERE " - sql += " AND ".join(f"{TABLEMAP[table].get(k)} = " + sql += " AND ".join(f"{k} = " "%s" for k in where_.keys()) with conn.cursor() as cursor: cursor.execute(sql, tuple(where_.values())) @@ -99,7 +99,7 @@ def insert(conn: Any, table: str, data: Dataclass) -> Optional[int]: """Run an INSERT into a table""" - dict_ = {k: v for k, v in asdict(data).items() + dict_ = {TABLEMAP[table].get(k): v for k, v in asdict(data).items() if v is not None and k in TABLEMAP[table]} sql = f"INSERT INTO {table} (" sql += ", ".join(f"{k}" for k in dict_.keys()) -- cgit v1.2.3