aboutsummaryrefslogtreecommitdiff
path: root/gn3/db
diff options
context:
space:
mode:
authorBonfaceKilz2021-07-26 22:00:25 +0300
committerBonfaceKilz2021-07-26 22:00:25 +0300
commitab108e2988ed15b3de4db506a36444d9c736348d (patch)
treea2b12f51437d591f55c83048290b6223fca1fbb2 /gn3/db
parent9fe0f711a6b9ba8149ad26ffe2ff8cc1f686d90b (diff)
downloadgenenetwork3-ab108e2988ed15b3de4db506a36444d9c736348d.tar.gz
gn3: db: Create a raw update query
* gn3/db/__init__.py (update_raw): New function.
Diffstat (limited to 'gn3/db')
-rw-r--r--gn3/db/__init__.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/gn3/db/__init__.py b/gn3/db/__init__.py
index 5ab9f3c..24ae8f1 100644
--- a/gn3/db/__init__.py
+++ b/gn3/db/__init__.py
@@ -1,7 +1,7 @@
# pylint: disable=[R0902, R0903]
"""Module that exposes common db operations"""
from dataclasses import asdict, astuple
-from typing import Any, Dict, List, Optional, Generator, Union
+from typing import Any, Dict, List, Optional, Generator, Tuple, Union
from typing_extensions import Protocol
from gn3.db.metadata_audit import MetadataAudit
@@ -64,6 +64,20 @@ def update(conn: Any,
return cursor.rowcount
+def update_raw(conn: Any, table: str,
+ set_: List[Tuple[str, Any]],
+ where: Tuple[str, Tuple]):
+ """Run a generic raw statement"""
+ sql = f"UPDATE {table} SET "
+ sql += ", ".join([f"{k} = '%s'" for k, v in set_])
+ sql += f" WHERE {where[0]}"
+ with conn.cursor() as cursor:
+ cursor.execute(sql,
+ tuple(v for _, v in set_) + where[1])
+ conn.commit()
+ return cursor.rowcount
+
+
def fetchone(conn: Any,
table: str,
where: Optional[Dataclass],