Changed name sqlify to datalite
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
from os.path import exists
|
|
||||||
from pathlib import Path
|
|
||||||
import sqlite3 as sql
|
import sqlite3 as sql
|
||||||
from dataclasses import Field, asdict, dataclass
|
from dataclasses import Field, asdict, dataclass
|
||||||
from typing import List, Dict, Optional, Callable, Any
|
from typing import List, Dict, Optional, Callable, Any
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _convert_type(type_: Optional[type], type_overload: Dict[Optional[type], str]) -> str:
|
def _convert_type(type_: Optional[type], type_overload: Dict[Optional[type], str]) -> str:
|
||||||
"""
|
"""
|
||||||
Given a Python type, return the str name of its
|
Given a Python type, return the str name of its
|
||||||
@@ -12,6 +13,8 @@ def _convert_type(type_: Optional[type], type_overload: Dict[Optional[type], str
|
|||||||
:param type_: A Python type, or None.
|
:param type_: A Python type, or None.
|
||||||
:param type_overload: A type table to overload the custom type table.
|
:param type_overload: A type table to overload the custom type table.
|
||||||
:return: The str name of the sql type.
|
:return: The str name of the sql type.
|
||||||
|
>>> _convert_type(int)
|
||||||
|
"INTEGER"
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return type_overload[type_]
|
return type_overload[type_]
|
||||||
@@ -77,7 +80,6 @@ def _create_entry(self) -> None:
|
|||||||
Given an object, create the entry for the object. As a side-effect,
|
Given an object, create the entry for the object. As a side-effect,
|
||||||
this will set the object_id attribute of the object to the unique
|
this will set the object_id attribute of the object to the unique
|
||||||
id of the entry.
|
id of the entry.
|
||||||
:param cur: Cursor of the database.
|
|
||||||
:param self: Instance of the object.
|
:param self: Instance of the object.
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
@@ -93,7 +95,19 @@ def _create_entry(self) -> None:
|
|||||||
con.commit()
|
con.commit()
|
||||||
|
|
||||||
|
|
||||||
def sqlify(db_path: str, type_overload: Optional[Dict[Optional[type], str]] = None,
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
def datalite(db_path: str, type_overload: Optional[Dict[Optional[type], str]] = None,
|
||||||
*args, **kwargs) -> Callable:
|
*args, **kwargs) -> Callable:
|
||||||
def decorator(dataclass_: type, *args_i, **kwargs_i):
|
def decorator(dataclass_: type, *args_i, **kwargs_i):
|
||||||
type_table: Dict[Optional[type], str] = {None: "NULL", int: "INTEGER", float: "REAL",
|
type_table: Dict[Optional[type], str] = {None: "NULL", int: "INTEGER", float: "REAL",
|
||||||
@@ -109,6 +123,7 @@ def sqlify(db_path: str, type_overload: Optional[Dict[Optional[type], str]] = No
|
|||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def is_fetchable(class_: type, obj_id: int) -> bool:
|
def is_fetchable(class_: type, obj_id: int) -> bool:
|
||||||
"""
|
"""
|
||||||
Check if a record is fetchable given its obj_id and
|
Check if a record is fetchable given its obj_id and
|
||||||
|
|||||||
Reference in New Issue
Block a user