-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBase.py
293 lines (263 loc) · 9.23 KB
/
DataBase.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from flask import url_for
import psycopg2
import traceback
from psycopg2 import errors
InFailedSqlTransaction = errors.lookup('25P02')
class DataBase:
def __init__(self, db):
self.__db = db
self.__cur = db.cursor()
def getBooks(self):
sql = '''SELECT * FROM dmel_books ORDER BY title'''
try:
self.__cur.execute(sql)
res = self.__cur.fetchall()
if res: return res
except:
print("Ошибка чтения из БД")
return []
def getBook(self, book_id):
try:
self.__cur.execute(f"SELECT * FROM dmel_books WHERE id = '{book_id}' LIMIT 1")
res = self.__cur.fetchone()
if res: return res
except:
print("Ошибка чтения из БД")
return []
def getBookImage(self, book_id):
img = None
try:
self.__cur.execute(f"SELECT * FROM dmel_books WHERE id = '{book_id}' LIMIT 1")
res = self.__cur.fetchone()
print (res)
img = res[6]
if img: return img
except :
print("Ошибка чтения из БД")
def addBook(self, author, title, num_pg, year, discription, image, file, is_open):
try:
dat = file.read()
binary_file = psycopg2.Binary(dat)
dat = image.read()
binary_img = psycopg2.Binary(dat)
self.__cur.execute('INSERT INTO dmel_books ( title, year, num_pg, discription, file, image, is_open)'
'VALUES ( %s, %s, %s, %s, %s, %s, %s)',
( title, year, num_pg, discription, binary_file, binary_img, is_open)
)
self.__db.commit()
self.__cur.execute(f"SELECT id FROM dmel_books WHERE year = '{year}' AND num_pg = '{num_pg}' AND title LIKE '{title}'")
id = self.__cur.fetchone()
self.__cur.execute('INSERT INTO dmel_book_authors (book_id, author_id)'
'VALUES (%s, %s)',
(id, author[0])
)
self.__db.commit()
return True
except :
print("Ошибка добавления в БД")
def deleteBook(self, book_id):
try:
self.__cur.execute("DELETE FROM dmel_books WHERE id = %s", [book_id])
self.__db.commit()
return True
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def updateBook(self, book_id, title, num_pg, year, discription, is_open):
try:
self.__cur.execute('UPDATE dmel_books SET title = %s WHERE id = %s ', (title, book_id))
self.__cur.execute('UPDATE dmel_books SET year = %s WHERE id = %s ', (year, book_id))
self.__cur.execute('UPDATE dmel_books SET num_pg = %s WHERE id = %s ', (num_pg, book_id))
self.__cur.execute('UPDATE dmel_books SET discription = %s WHERE id = %s ', (discription, book_id))
self.__cur.execute('UPDATE dmel_books SET is_open = %s WHERE id = %s ', (is_open, book_id))
self.__db.commit()
return True
except :
print("Ошибка изменения в БД")
def updateFileImg(self, book_id, image):
try:
dat = image.read()
binary_img = psycopg2.Binary(dat)
self.__cur.execute('UPDATE dmel_books SET image = %s WHERE id = %s ', (binary_img, book_id))
self.__db.commit()
return True
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def updateFilePdf(self, book_id, file):
try:
dat = file.read()
binary_file = psycopg2.Binary(dat)
self.__cur.execute('UPDATE dmel_books SET file = %s WHERE id = %s ', (binary_file, book_id))
self.__db.commit()
return True
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def getTagsOfBook(self, book_id):
try:
self.__cur.execute(f"SELECT * FROM dmel_book_tags WHERE book_id = '{book_id}'")
books = self.__cur.fetchall()
self.__db.commit()
return books
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def getAllTags(self):
try:
self.__cur.execute(f"SELECT * FROM dmel_tags ORDER BY id")
tags = self.__cur.fetchall()
self.__db.commit()
return tags
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def getTag(self, tag_id):
try:
self.__cur.execute("SELECT * FROM dmel_tags WHERE id = %s", [tag_id])
tag = self.__cur.fetchone()
self.__db.commit()
return tag
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def getBooksByTag(self, tag_id):
try:
self.__cur.execute("SELECT * FROM dmel_book_tags WHERE tag_id = %s", [tag_id])
tags = self.__cur.fetchall()
self.__db.commit()
tag_ids = []
for tag in tags:
if (int(tag[1]) == int(tag_id)):
tag_ids.append(tag[2])
self.__cur.execute("SELECT * FROM dmel_books")
books = self.__cur.fetchall()
self.__db.commit()
used_books = []
for book in books:
if (int(book[0]) in tag_ids):
used_books.append(book)
return used_books
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def addTag(self, tag):
try:
self.__cur.execute('INSERT INTO dmel_tags (tag)'
'VALUES (%s)',
(tag,)
)
self.__db.commit()
return True
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def deleteTag(self, tag_id):
try:
self.__cur.execute("DELETE FROM dmel_tags WHERE id = %s", [tag_id])
self.__db.commit()
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def deleteTagBook(self, book_id, tag_id):
try:
self.__cur.execute("DELETE FROM dmel_book_tags WHERE book_id = %s AND tag_id = %s", [book_id, tag_id])
self.__db.commit()
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def addTagBook(self, book_id, tag_id):
try:
self.__cur.execute('INSERT INTO dmel_book_tags (book_id, tag_id)'
'VALUES (%s, %s)',
(book_id, tag_id)
)
self.__db.commit()
return True
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def getAllAuthors(self):
try:
self.__cur.execute(f"SELECT id, name, sec_name, sourname FROM dmel_authors ORDER BY sourname")
authors = self.__cur.fetchall()
return authors
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def addAuthor(self, name, sourname, sec_name):
try:
self.__cur.execute('INSERT INTO dmel_authors (name, sourname, sec_name)'
'VALUES (%s, %s, %s)',
(name, sourname, sec_name)
)
self.__db.commit()
return True
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def deleteAuthor(self, author_id):
try:
self.__cur.execute("DELETE FROM dmel_authors WHERE id = %s", [author_id])
self.__db.commit()
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def addBookAuthor(self, book_id, author_id):
try:
self.__cur.execute('INSERT INTO dmel_book_authors (book_id, author_id)'
'VALUES (%s, %s)',
(book_id, author_id)
)
self.__db.commit()
return True
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def getAuthorsOfBook(self, book_id):
try:
self.__cur.execute(f"SELECT * FROM dmel_book_authors WHERE book_id = '{book_id}'")
authors = self.__cur.fetchall()
self.__db.commit()
all_authors = self.getAllAuthors()
used_authors = []
for a in all_authors:
for b in authors:
if (int(a[0]) == int(b[1])):
used_authors.append(a)
return used_authors
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def deleteAuthorBook(self, book_id, author_id):
try:
self.__cur.execute("DELETE FROM dmel_book_authors WHERE book_id = %s AND author_id = %s", [book_id, author_id])
self.__db.commit()
except InFailedSqlTransaction:
traceback.print_exc()
self._cr.rollback()
pass
def getAuthor(self, author_id):
try:
self.__cur.execute(f"SELECT * FROM dmel_authors WHERE id = '{author_id}' LIMIT 1")
res = self.__cur.fetchone()
if res: return res
except:
print("Ошибка чтения из БД")
return []