diff options
author | BonfaceKilz | 2021-06-07 19:43:17 +0300 |
---|---|---|
committer | BonfaceKilz | 2021-06-07 19:58:44 +0300 |
commit | 284e7aba7860783cec271471fd2c6f0078805965 (patch) | |
tree | c19d3a6267985f271a23b1549af3d70c37ea9e77 | |
parent | 61cf4be04f1382ead04e9c1f7d5fffdfc395d6a4 (diff) | |
download | genenetwork3-284e7aba7860783cec271471fd2c6f0078805965.tar.gz |
gn3: db: Fix how columns from tables is resolved
-rw-r--r-- | gn3/db/__init__.py | 10 |
1 files 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()) |