Add entry update
This commit is contained in:
14
README.md
14
README.md
@@ -1,5 +1,8 @@
|
|||||||
# Datalite
|
# Datalite
|
||||||
|
|
||||||
|
[](https://codeclimate.com/github/ambertide/datalite/maintainability)
|
||||||
|
|
||||||
|
|
||||||
Datalite is a simple Python
|
Datalite is a simple Python
|
||||||
package that binds your dataclasses to a table in a sqlite3 database,
|
package that binds your dataclasses to a table in a sqlite3 database,
|
||||||
using it is extremely simple, say that you have a dataclass definition,
|
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
|
## Entry manipulation
|
||||||
|
|
||||||
After creating an object traditionally, given that you used the `datalite` decorator,
|
After creating an object traditionally, given that you used the `datalite` decorator,
|
||||||
the object has two new methods: `.create_entry()` and `.remove_entry()`, you
|
the object has three new methods: `.create_entry()`, `.update_entry()`
|
||||||
can add the object to its associated table using the former, and remove it
|
and `.remove_entry()`, you can add the object to its associated table
|
||||||
using the latter.
|
using the former, and remove it using the later. You can also update a record using
|
||||||
|
the middle.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
student = Student(10, "Albert Einstein")
|
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.
|
student.remove_entry() # Removes from the table.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -92,6 +92,23 @@ def _create_entry(self) -> None:
|
|||||||
con.commit()
|
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):
|
def remove_from(class_: type, obj_id: int):
|
||||||
with sql.connect(getattr(class_, "db_path")) as con:
|
with sql.connect(getattr(class_, "db_path")) as con:
|
||||||
cur: sql.Cursor = con.cursor()
|
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.
|
setattr(dataclass_, 'db_path', db_path) # We add the path of the database to class itself.
|
||||||
dataclass_.create_entry = _create_entry
|
dataclass_.create_entry = _create_entry
|
||||||
dataclass_.remove_entry = _remove_entry
|
dataclass_.remove_entry = _remove_entry
|
||||||
|
dataclass_.update_entry = _update_entry
|
||||||
return dataclass_
|
return dataclass_
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
|
|||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="datalite", # Replace with your own username
|
name="datalite", # Replace with your own username
|
||||||
version="0.3.0",
|
version="0.4.0",
|
||||||
author="Ege Ozkan",
|
author="Ege Ozkan",
|
||||||
author_email="egeemirozkan24@gmail.com",
|
author_email="egeemirozkan24@gmail.com",
|
||||||
description="A small package that binds dataclasses to an sqlite database",
|
description="A small package that binds dataclasses to an sqlite database",
|
||||||
|
|||||||
Reference in New Issue
Block a user