-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbapi.py
84 lines (71 loc) · 2.63 KB
/
dbapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Abstraction module to interact with the database."""
from pathlib import Path
from sqlite3 import connect
from threading import Lock
DB_PATH = Path("data/i.db")
if not DB_PATH.exists():
with open(DB_PATH, "w", encoding=None):
pass
class Database:
database: str
lock: Lock
def __init__(self) -> None:
Database.database = str(DB_PATH)
Database.lock = Lock()
class Table:
name: str
def __init__(self, name: str) -> None:
self.name = name
def query(
self, columns, where_column: str | None = None, where_data: list | None = None
) -> list:
"""Queries the table's current data."""
Database.lock.acquire()
with connect(Database.database) as connection:
cursor = connection.cursor()
if where_column and where_data:
result = cursor.execute(
f"SELECT {columns} FROM {self.name} WHERE {where_column} = ?",
where_data,
)
else:
result = cursor.execute(f"SELECT {columns} FROM {self.name}")
Database.lock.release()
return [*result]
def insert(self, values, *, lock_needed: bool = True) -> None:
"""Inserts <values> into the current table."""
if lock_needed:
Database.lock.acquire()
with connect(Database.database) as connection:
cursor = connection.cursor()
command = f"INSERT INTO {self.name} VALUES({('?, ' * len(values))[:-2]})"
cursor.execute(command, values)
connection.commit()
if lock_needed:
Database.lock.release()
def update_property(
self,
property_column: str,
property_data: str,
where_column: str,
where_data: str,
) -> None:
"""Updates a SINGLE property."""
Database.lock.acquire()
with connect(Database.database) as connection:
cursor = connection.cursor()
command = f"UPDATE {self.name} SET {property_column} = ? WHERE {where_column} = ?"
cursor.execute(command, [str(property_data), str(where_data)])
connection.commit()
Database.lock.release()
def insert_object(self, object_) -> None:
"""Inserts an object or its properties into the current table."""
Database.lock.acquire()
try:
data = object_.__dict__
if not data:
data = object_.__slots__
except AttributeError:
data = object_.__slots__
self.insert([str(getattr(object_, attr)) for attr in data], lock_needed=False)
Database.lock.release()