-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm.py
219 lines (174 loc) · 7.1 KB
/
orm.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import inspect
import sqlite3
from typing import Tuple, List, Union
# TODO: Use jinja2 templates for constructing SQL queries
Value = Union[str, int, None, float, bytes, bool]
class Database:
def __init__(self, path: str):
self.connection = sqlite3.Connection(path)
def create(self, table: type["Table"]):
self.connection.execute(table._get_create_sql())
def get(self, table: type["Table"], id: str | int):
sql, fields, params = table._get_select_where_sql(id)
row = self.connection.execute(sql, params).fetchone()
if row is None:
raise Exception(f"{table.__name__} instance with id {id} does not exist")
instance = table()
for field, value in zip(fields, row):
if field.endswith("_id"):
field = field[:-3]
fk = getattr(table, field)
value = self.get(fk.table, id=value)
setattr(instance, field, value)
return instance
def save(self, instance: "Table"):
sql, values = instance._get_insert_sql()
cursor: sqlite3.Cursor = self.connection.execute(sql, values)
instance._data["id"] = cursor.lastrowid
self.connection.commit()
def update(self, instance: "Table"):
sql, values = instance._get_update_sql()
self.connection.execute(sql, values)
self.connection.commit()
def delete(self, table: type["Table"], id: Union[str, int]):
sql, parameters = table._get_delete_sql(id)
self.connection.execute(sql, parameters)
self.connection.commit()
def all(self, table: type["Table"]) -> List["Table"]:
sql, fields = table._get_select_all_sql()
results = []
for row in self.connection.execute(sql).fetchall():
instance = table()
for field, value in zip(fields, row):
if field.endswith("_id"):
field = field[:-3]
fk = getattr(table, field)
value = self.get(fk.table, id=value)
setattr(instance, field, value)
results.append(instance)
return results
@property
def tables(self) -> List[str]:
SELECT_TABLES_SQL = "SELECT name FROM sqlite_master WHERE type = 'table';"
return [x[0] for x in self.connection.execute(SELECT_TABLES_SQL).fetchall()]
class Table:
def __init__(self, **kwargs) -> None:
self._data: dict[str, Value] = {"id": None}
for key, value in kwargs.items():
self._data[key] = value
def __getattribute__(self, key: str):
_data = super().__getattribute__("_data")
if key in _data:
return _data[key]
return super().__getattribute__(key)
def __setattr__(self, key: str, value: Value) -> None:
super().__setattr__(key, value)
if key in self._data:
self._data[key] = value
@classmethod
def _get_create_sql(cls) -> str:
CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS {name} ({fields});"
fields = [
"id INTEGER PRIMARY KEY AUTOINCREMENT",
]
for column_name, field in inspect.getmembers(cls):
if isinstance(field, Column):
fields.append(f"{column_name} {field.sql_type}")
elif isinstance(field, ForeignKey):
fields.append(f"{column_name}_id INTEGER")
fields = ", ".join(fields)
table_name = cls.__name__.lower()
return CREATE_TABLE_SQL.format(name=table_name, fields=fields)
@property
def fields(self) -> List[str]:
fields, cls = [], type(self)
for name, field in inspect.getmembers(cls):
if isinstance(field, Column):
fields.append(name)
return fields
def _get_insert_sql(self) -> Tuple[str, List[Value]]:
INSERT_SQL = "INSERT INTO {name} ({fields}) VALUES ({placeholders});"
cls = self.__class__
fields, placeholders, values = [], [], []
for name, field in inspect.getmembers(cls):
if isinstance(field, Column):
fields.append(name)
values.append(getattr(self, name))
placeholders.append("?")
elif isinstance(field, ForeignKey):
fields.append(name + "_id")
values.append(getattr(self, name).id)
placeholders.append("?")
fields = ", ".join(fields)
placeholders = ", ".join(placeholders)
sql = INSERT_SQL.format(
name=cls.__name__.lower(), fields=fields, placeholders=placeholders
)
return sql, values
@classmethod
def _get_select_all_sql(cls) -> Tuple[str, List[str]]:
SELECT_ALL_SQL = "SELECT {fields} FROM {name};"
fields = ["id"]
for name, field in inspect.getmembers(cls):
if isinstance(field, Column):
fields.append(name)
if isinstance(field, ForeignKey):
fields.append(name + "_id")
sql = SELECT_ALL_SQL.format(name=cls.__name__.lower(), fields=", ".join(fields))
return sql, fields
def asdict(self) -> dict:
serialized = {field: getattr(self, field) for field in self.fields}
serialized["id"] = self.id
return serialized
@classmethod
def _get_select_where_sql(cls, id: str | int) -> Tuple[str, List[str], List[str]]:
SELECT_WHERE_SQL = "SELECT {fields} FROM {name} WHERE id = ?;"
fields = ["id"]
for name, field in inspect.getmembers(cls):
if isinstance(field, Column):
fields.append(name)
if isinstance(field, ForeignKey):
fields.append(name + "_id")
sql = SELECT_WHERE_SQL.format(
name=cls.__name__.lower(), fields=", ".join(fields)
)
params = [id]
return sql, fields, params
def _get_update_sql(self):
UPDATE_SQL = "UPDATE {name} SET {fields} WHERE id = ?"
cls = self.__class__
fields, values = [], []
for name, field in inspect.getmembers(cls):
if isinstance(field, Column):
fields.append(name)
values.append(getattr(self, name))
elif isinstance(field, ForeignKey):
fields.append(name + "_id")
values.append(getattr(self, name).id)
values.append(getattr(self, "id"))
sql = UPDATE_SQL.format(
name=cls.__name__.lower(),
fields=", ".join([f"{field} = ?" for field in fields]),
)
return sql, values
@classmethod
def _get_delete_sql(cls, id):
DELETE_SQL = "DELETE FROM {name} WHERE id = ?"
sql = DELETE_SQL.format(name=cls.__name__.lower())
return sql, [id]
class Column:
def __init__(self, column_type: type) -> None:
self._type = column_type
@property
def sql_type(self) -> str:
TYPE_MAP: dict[type, str] = {
int: "INTEGER",
float: "REAL",
str: "TEXT",
bytes: "BLOB",
bool: "INTEGER", # 0 or 1
}
return TYPE_MAP[self._type]
class ForeignKey:
def __init__(self, table):
self.table = table