From 1459f94be078586af180abc00560c86580ed832a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20Emir=20=C3=96zkan?= Date: Mon, 10 Aug 2020 01:43:14 +0300 Subject: [PATCH] Add entry update --- README.md | 14 ++++++++++---- datalite/__init__.py | 18 ++++++++++++++++++ setup.py | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b094549..e03261a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Datalite +[![Maintainability](https://api.codeclimate.com/v1/badges/9d4ce56bfbd3b63649be/maintainability)](https://codeclimate.com/github/ambertide/datalite/maintainability) + + Datalite is a simple Python package that binds your dataclasses to a table in a sqlite3 database, using it is extremely simple, say that you have a dataclass definition, @@ -28,13 +31,16 @@ integer and text, respectively. The default value for `student_name` is ## Entry manipulation After creating an object traditionally, given that you used the `datalite` decorator, -the object has two new methods: `.create_entry()` and `.remove_entry()`, you -can add the object to its associated table using the former, and remove it -using the latter. +the object has three new methods: `.create_entry()`, `.update_entry()` +and `.remove_entry()`, you can add the object to its associated table +using the former, and remove it using the later. You can also update a record using +the middle. ```python student = Student(10, "Albert Einstein") -student.create_entry() # Adds the entry to the table associated in db.db +student.create_entry() # Adds the entry to the table associated in db.db. +student.student_id = 20 # Update an object on memory. +student.update_entry() # Update the corresponding record in the database. student.remove_entry() # Removes from the table. ``` diff --git a/datalite/__init__.py b/datalite/__init__.py index 2cc9448..4d9ba59 100644 --- a/datalite/__init__.py +++ b/datalite/__init__.py @@ -92,6 +92,23 @@ def _create_entry(self) -> None: con.commit() +def _update_entry(self) -> None: + """ + Given an object, update the objects entry in the bound database. + :param self: The object. + :return: None. + """ + with sql.connect(getattr(self, "db_path")) as con: + cur: sql.Cursor = con.cursor() + table_name: str = self.__clas__.__name__.lower() + kv_pairs = [item for item in asdict(self).items()] + kv_pairs.sort(key=lambda item: item[0]) + cur.execute(f"UPDATE {table_name}" + f"SET {', '.join(item[0] + ' = ' + _convert_sql_format(item[1]) for item in kv_pairs)}" + f"WHERE obj_id = {getattr(self, 'obj_id')}") + con.commit() + + def remove_from(class_: type, obj_id: int): with sql.connect(getattr(class_, "db_path")) as con: cur: sql.Cursor = con.cursor() @@ -121,6 +138,7 @@ def datalite(db_path: str, type_overload: Optional[Dict[Optional[type], str]] = 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 + dataclass_.update_entry = _update_entry return dataclass_ return decorator diff --git a/setup.py b/setup.py index 9f1c457..e7918fe 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.3.0", + version="0.4.0", author="Ege Ozkan", author_email="egeemirozkan24@gmail.com", description="A small package that binds dataclasses to an sqlite database",