From 3d1a662b209d6fb2f11df5b21cdc0149746a2fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20Emir=20=C3=96zkan?= Date: Sun, 9 Aug 2020 07:30:28 +0300 Subject: [PATCH] Add remove_from and .remove_entry --- datalite/__init__.py | 14 +++++++++----- setup.py | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/datalite/__init__.py b/datalite/__init__.py index a4182be..2cc9448 100644 --- a/datalite/__init__.py +++ b/datalite/__init__.py @@ -92,16 +92,20 @@ def _create_entry(self) -> None: con.commit() +def remove_from(class_: type, obj_id: int): + with sql.connect(getattr(class_, "db_path")) as con: + cur: sql.Cursor = con.cursor() + cur.execute(f"DELETE FROM {class_.__name__.lower()} WHERE obj_id = {obj_id}") + con.commit() + + def _remove_entry(self) -> None: """ Remove the object's record in the underlying database. :param self: self instance. :return: None. """ - with sql.connect(getattr(self, "db_path")) as con: - cur: sql.Cursor = con.cursor() - cur.execute(f"DELETE FROM {self.__class__.__name__.lower()} WHERE obj_id = {self.obj_id}") - con.commit() + remove_from(self.__class__, getattr(self, 'obj_id')) def datalite(db_path: str, type_overload: Optional[Dict[Optional[type], str]] = None, @@ -116,11 +120,11 @@ def datalite(db_path: str, type_overload: Optional[Dict[Optional[type], str]] = _create_table(dataclass_, cur, type_table) setattr(dataclass_, 'db_path', db_path) # We add the path of the database to class itself. dataclass_.create_entry = _create_entry + dataclass_.remove_entry = _remove_entry return dataclass_ return decorator - def is_fetchable(class_: type, obj_id: int) -> bool: """ Check if a record is fetchable given its obj_id and diff --git a/setup.py b/setup.py index 0598017..9f1c457 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="datalite", # Replace with your own username - version="0.2.2", + version="0.3.0", author="Ege Ozkan", author_email="egeemirozkan24@gmail.com", description="A small package that binds dataclasses to an sqlite database",