-
Notifications
You must be signed in to change notification settings - Fork 0
/
mappers.py
104 lines (81 loc) · 2.97 KB
/
mappers.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
import sqlite3
from models import Student, Category
connection = sqlite3.connect('site_db.sqlite')
class RecordNotFoundException(Exception):
def __init__(self, text):
super().__init__(f'Record not found: {text}')
class DBCommitException(Exception):
def __init__(self, text):
super().__init__(f'DB commit error: {text}')
class DBUpdateException(Exception):
def __init__(self, text):
super().__init__(f'DB update error: {text}')
class DBDeleteException(Exception):
def __init__(self, text):
super().__init__(f'DB delete error: {text}')
class StudentMapper:
def __init__(self, connection):
self.connection = connection
self.cursor = connection.cursor()
self.table_name = 'student'
def find_all(self):
command = f'SELECT * FROM {self.table_name}'
self.cursor.execute(command)
result = []
for item in self.cursor.fetchall():
id, name = item
student = Student(name)
student.id = id
result.append(student)
return result
def find_by_id(self, id):
command = f'SELECT id, name FROM {self.table_name} WHERE id=?'
self.cursor.execute(command, (id,))
result = self.cursor.fetchone()
if result:
return Student(*result)
else:
raise RecordNotFoundException(f'запись с id={id} не найдена')
def find_by_name(self, name):
command = f'SELECT id, name FROM {self.table_name} WHERE id=?'
self.cursor.execute(command, (id,))
result = self.cursor.fetchone()
if result:
return Student(*result)
else:
raise RecordNotFoundException(f'запись с id={id} не найдена')
def insert(self, obj):
command = f'INSERT INTO {self.table_name} (name) VALUES (?)'
self.cursor.execute(command, (obj.name,))
try:
self.connection.commit()
except Exception as e:
raise DBCommitException(e.args)
def update(self, obj):
command = f'UPDATE {self.table_name} SET name=? WHERE id=?'
self.cursor.execute(command, (obj.name, obj.id))
try:
self.connection.commit()
except Exception as e:
raise DBUpdateException(e.args)
def delete(self, obj):
command = f"DELETE FROM {self.table_name} WHERE id=?"
self.cursor.execute(command, (obj.id,))
try:
self.connection.commit()
except Exception as e:
raise DBDeleteException(e.args)
class MapperRegistry:
mappers = {
'student': StudentMapper,
# 'category': CategoryMapper
}
@staticmethod
def get_mapper(obj):
if isinstance(obj, Student):
return StudentMapper(connection)
# if isinstance(obj, Category):
# return CategoryMapper(connection)
@staticmethod
def get_current_mapper(name):
return MapperRegistry.mappers[name](connection)