-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_set.py
32 lines (27 loc) · 984 Bytes
/
query_set.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
class QuerySet:
def __init__(self, model_cls, **conditions):
self.model = model_cls
self.conditions = conditions
def __iter__(self):
pass
def get(self):
conn = self.model._conn
cursor = conn.cursor()
command = f'select * from {self.model._table_name} where '
for k, v in self.conditions.items():
command += k + ' = \'' + str(v) + '\' and '
command = command[:-5]
res = cursor.execute(command).fetchall()
return [self.model(**item) for item in res]
def filter(self, **kwargs):
self.conditions.update(kwargs)
return self
def delete(self):
conn = self.model._conn
cursor = conn.cursor()
command = f'delete from {self.model._table_name} where '
for k, v in self.conditions.items():
command += k + ' = \'' + str(v) + '\' and '
command = command[:-5]
cursor.execute(command)
conn.commit()