Add remove_from and .remove_entry

This commit is contained in:
Ege Emir Özkan
2020-08-09 07:30:28 +03:00
parent 42d233bdd6
commit 3d1a662b20
2 changed files with 10 additions and 6 deletions

View File

@@ -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

View File

@@ -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",