Reverted fetch_from, added fetch_equals for better documentation and simplicity.
This commit is contained in:
15
README.md
15
README.md
@@ -62,22 +62,23 @@ inserted onto the system.
|
|||||||
> :warning: **Limitation! Fetch can only fetch limited classes correctly**: int, float and str!
|
> :warning: **Limitation! Fetch can only fetch limited classes correctly**: int, float and str!
|
||||||
|
|
||||||
Finally, you may wish to recreate objects from a table that already exist, for
|
Finally, you may wish to recreate objects from a table that already exist, for
|
||||||
this purpose we have the function `fetch_from(class_, value, field)` as well
|
this purpose we have the function `fetch_from(class_, obj_id)` as well
|
||||||
as `is_fetchable(className, object_id)` former fetches a record from the
|
as `is_fetchable(className, object_id)` former fetches a record from the
|
||||||
SQL database given a field and value. If only the value is provided,
|
SQL database given its unique object_id whereas the latter checks if it
|
||||||
field defaults to 'obj_id' which is unique for all objects,
|
is fetchable (most likely to check if it exists.)
|
||||||
whereas the latter checks if it is fetchable (most likely to check if it exists.)
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
>>> fetch_from(Student, 2)
|
>>> fetch_from(Student, 2)
|
||||||
Student(student_id=10, student_name='Albert Einstein')
|
Student(student_id=10, student_name='Albert Einstein')
|
||||||
```
|
```
|
||||||
|
|
||||||
We have three helper methods, `fetch_range(class_, range_)` and
|
We have four helper methods, `fetch_range(class_, range_)` and
|
||||||
`fetch_all(class_)` are very similar: the former fetches the records
|
`fetch_all(class_)` are very similar: the former fetches the records
|
||||||
fetchable from the object id range provided by the user, whereas the
|
fetchable from the object id range provided by the user, whereas the
|
||||||
latter fetches all records. Both return a tuple of `class_` objects.
|
latter fetches all records. Both return a tuple of `class_` objects.
|
||||||
|
|
||||||
The last helper method, `fetch_if(class_, condition)` fetches all
|
The last two helper methods, `fetch_if(class_, condition)` fetches all
|
||||||
the records of type `class_` that fit a certain condition. Here conditions
|
the records of type `class_` that fit a certain condition. Here conditions
|
||||||
must be written is SQL syntax. Be careful about the string espacially.
|
must be written is SQL syntax. For easier, only one conditional checks, there
|
||||||
|
is `fetch_equals(class_, field, value)` that checks the value of only one `field`
|
||||||
|
and returns the object whose `field` equals the provided `value`.
|
||||||
@@ -171,7 +171,7 @@ def _get_table_cols(cur: sql.Cursor, table_name: str) -> List[str]:
|
|||||||
return [row_info[1] for row_info in cur.fetchall()][1:]
|
return [row_info[1] for row_info in cur.fetchall()][1:]
|
||||||
|
|
||||||
|
|
||||||
def fetch_from(class_: type, value: Any, field: str = 'obj_id') -> Any:
|
def fetch_equals(class_: type, field: str, value: Any, ) -> Any:
|
||||||
"""
|
"""
|
||||||
Fetch a class_ type variable from its bound db.
|
Fetch a class_ type variable from its bound db.
|
||||||
:param class_: Class to fetch.
|
:param class_: Class to fetch.
|
||||||
@@ -191,6 +191,19 @@ def fetch_from(class_: type, value: Any, field: str = 'obj_id') -> Any:
|
|||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_from(class_: type, obj_id: int) -> Any:
|
||||||
|
"""
|
||||||
|
Fetch a class_ type variable from its bound dv.
|
||||||
|
:param class_: Class to fetch from.
|
||||||
|
:param obj_id: Unique object id of the object.
|
||||||
|
:return: The fetched object.
|
||||||
|
"""
|
||||||
|
if not is_fetchable(class_, obj_id):
|
||||||
|
raise KeyError(f"An object with {obj_id} of type {class_.__name__} does not exist, or"
|
||||||
|
f"otherwise is unreachable.")
|
||||||
|
return fetch_equals(class_, 'obj_id', obj_id)
|
||||||
|
|
||||||
|
|
||||||
def _convert_record_to_object(class_: type, record: Tuple[Any], field_names: List[str]) -> Any:
|
def _convert_record_to_object(class_: type, record: Tuple[Any], field_names: List[str]) -> Any:
|
||||||
"""
|
"""
|
||||||
Convert a given record fetched from an SQL instance to a Python Object of given class_.
|
Convert a given record fetched from an SQL instance to a Python Object of given class_.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import unittest
|
import unittest
|
||||||
from datalite import datalite, fetch_if, fetch_all, fetch_range, fetch_from
|
from datalite import datalite, fetch_if, fetch_all, fetch_range, fetch_from, fetch_equals
|
||||||
from sqlite3 import connect
|
from sqlite3 import connect
|
||||||
from dataclasses import dataclass, asdict
|
from dataclasses import dataclass, asdict
|
||||||
from os import remove
|
from os import remove
|
||||||
@@ -82,8 +82,8 @@ class DatabaseFetchCalls(unittest.TestCase):
|
|||||||
t_obj = fetch_from(FetchClass, self.objs[0].obj_id)
|
t_obj = fetch_from(FetchClass, self.objs[0].obj_id)
|
||||||
self.assertEqual(self.objs[0], t_obj)
|
self.assertEqual(self.objs[0], t_obj)
|
||||||
|
|
||||||
def testFetchFromDif(self):
|
def testFetchEquals(self):
|
||||||
t_obj = fetch_from(FetchClass, self.objs[0].str_, 'str_')
|
t_obj = fetch_equals(FetchClass, 'str_', self.objs[0].str_)
|
||||||
self.assertEqual(self.objs[0], t_obj)
|
self.assertEqual(self.objs[0], t_obj)
|
||||||
|
|
||||||
def testFetchAll(self):
|
def testFetchAll(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user