Changed name sqlify to datalite

This commit is contained in:
Ege Emir Özkan
2020-08-03 03:11:22 +03:00
parent 61a94944d1
commit bb02b110c2
2 changed files with 21 additions and 6 deletions

0
setup.py Normal file
View File

View File

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