-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
306 lines (245 loc) · 9.24 KB
/
db.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
294
295
296
297
298
299
300
301
302
303
304
305
306
import mysql.connector
class BookDatabase:
def __init__(self):
self.config = {
'host': 'aliyun.duerx.host',
'user': 'remote',
'password': '123456',
'db': 'book'
}
self.con = mysql.connector.connect(**self.config)
self.cur = self.con.cursor()
def __del__(self):
self.con.close()
def search_with_book_name(self, book_name):
query = '''
SELECT books.book_id, title, author, author_name, book_likes_count.likes_num
FROM books, author, book_likes_count
WHERE books.author = author_id AND title like '%{}%' AND books.book_id = book_likes_count.book_id
'''.format(book_name)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_book_list(self,book_range):
query = '''
SELECT books.book_id, title, author, author_name, book_likes_count.likes_num
FROM books, author, book_likes_count
WHERE books.author = author_id and books.book_id = book_likes_count.book_id
ORDER BY book_likes_count.likes_num desc
LIMIT {},{}
'''.format(book_range,book_range+20)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_book_list_by_author_id(self, author_id):
query = '''
SELECT books.book_id, title, author, summary, book_likes_count.likes_num
FROM books, book_likes_count
WHERE author = {} and books.book_id = book_likes_count.book_id;
'''.format(author_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_book_list_by_publisher_id(self, publisher_id):
query = '''
SELECT book_id, title, publisher, summary
FROM books
WHERE publisher = {};
'''.format(publisher_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_book_list_by_like_user_id(self, user_id):
query = '''
SELECT books.book_id, books.title, author, author_name, book_likes_count.likes_num
FROM book_likes, books, author, book_likes_count
WHERE book_likes.liker_id = {} and book_likes.book_id = books.book_id and books.author = author.author_id and books.book_id = book_likes_count.book_id
'''.format(user_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_book_detail(self, book_id):
query = '''
SELECT title,subtitle,author,image,summary,publisher, pages, binding, rating, isbn10, isbn13, series, serie_name, price, pubdate, alt, author.author_name, publisher_name, rating.average
FROM books, serie, author, publisher, rating
WHERE books.book_id = '{}' and books.series = serie.serie_id and books.author = author.author_id and books.publisher = publisher.publisher_id and rating = rating.rating_id;
'''.format(book_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_serie_list(self,serie_range):
query = '''
SELECT serie_id, serie_name, description, volumes
FROM serie
ORDER BY serie.serie_id desc
LIMIT {},{}
'''.format(serie_range,serie_range+20)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_serie_detail(self, serie_id):
query = '''
SELECT books.title, books.book_id, serie.serie_name, author.author_id, author.author_name, serie.volumes
FROM serie, books, author
where serie.serie_id = '{}' and serie.serie_id=books.series and books.author=author.author_id;
'''.format(serie_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_author_list(self,author_range):
query = '''
SELECT author_id, author_name, author_introduction
FROM author
ORDER BY author_id desc
LIMIT {},{}
'''.format(author_range,author_range+10)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_author_detail(self, author_id):
query = '''
SELECT *
FROM author
where author_id = '{}';
'''.format(author_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_publisher_list(self,publisher_range):
query = '''
SELECT publisher_id, publisher_name, publisher_introduction
FROM publisher
ORDER BY publisher_id desc
LIMIT {},{}
'''.format(publisher_range,publisher_range+20)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_publisher_detail(self, publisher_id):
query = '''
SELECT *
FROM publisher
where publisher_id = '{}';
'''.format(publisher_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_reviews_by_book_id(self,book_id):
query = '''
SELECT user.user_name, reviews.time, content, review_likes_count.likes_num, user.user_id, reviews.review_id
FROM user, reviews, review_likes_count
WHERE reviews.book_id = '{}' and reviews.review_id = review_likes_count.reviews and reviews.reviewer_id = user.user_id
ORDER BY reviews.time desc
'''.format(book_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_reviews_by_user_id(self,user_id):
query = '''
SELECT books.title, reviews.time, content, review_likes_count.likes_num, books.book_id
FROM books, reviews, review_likes_count
WHERE reviews.reviewer_id = '{}' and books.book_id = reviews.book_id and reviews.review_id = review_likes_count.reviews
ORDER BY reviews.time desc
'''.format(user_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def insert_reivews(self, user_id, book_id, content):
query = '''
INSERT INTO reviews (reviewer_id, book_id, content)
VALUES ({}, '{}', '{}');
'''.format(user_id, book_id, content)
self.cur.execute(query)
self.con.commit()
# result = self.cur.fetchall()
# return result
def get_reviews(self):
query = '''
SELECT title, content, review_likes_count.likes_num, books.book_id, reviews.review_id
FROM books, reviews, review_likes_count
WHERE reviews.book_id = books.book_id and reviews.review_id = review_likes_count.reviews
ORDER BY reviews.time desc
LIMIT 0,20;
'''
self.cur.execute(query)
result = self.cur.fetchall()
return result
def get_username_by_id(self, id):
query = '''
SELECT user_name
FROM user
WHERE user_id = \'{}\'
'''.format(id)
self.cur.execute(query)
username, = self.cur.fetchone()
return username
def get_password_by_id(self, id):
query = '''
SELECT password
FROM user
WHERE user_id = \'{}\'
'''.format(id)
self.cur.execute(query)
password, = self.cur.fetchone()
return password
def validate_login(self, username, password):
query = '''
SELECT password
FROM user
WHERE user_name = \'{}\'
'''.format(username)
self.cur.execute(query)
result = self.cur.fetchone()
if result == None:
return "No Such Username"
elif result[0] != password:
return "Wrong Password"
return result[0] == password
def get_user_info_by_username(self, username):
query = '''
SELECT user_id, user_name, password
FROM user
WHERE user_name = \'{}\'
'''.format(username)
self.cur.execute(query)
result = self.cur.fetchone()
return result
def get_user_info_by_id(self, id):
query = '''
SELECT user_id, user_name, password
FROM user
WHERE user_id = \'{}\'
'''.format(id)
self.cur.execute(query)
result = self.cur.fetchone()
return result
def delete_review(self, review_id,reviewer_id):
query = '''
DELETE FROM reviews
WHERE review_id = {} and reviewer_id = {};
'''.format(review_id,reviewer_id)
self.cur.execute(query)
self.con.commit()
def get_like_list_by_user_id(self, user_id):
query = '''
SELECT reviews
FROM review_likes
WHERE liker_id = {}
'''.format(user_id)
self.cur.execute(query)
result = self.cur.fetchall()
return result
def add_like_review(self, review_id, user_id):
query = '''
INSERT INTO `review_likes` (`likes_id`, `liker_id`, `reviews`, `time`)
VALUES (NULL, '{}', '{}', CURRENT_TIMESTAMP)
'''.format(user_id, review_id)
self.cur.execute(query)
self.con.commit()
def del_like_review(self, review_id, user_id):
query = '''
DELETE FROM `review_likes`
WHERE liker_id = '{}' AND reviews = '{}'
'''.format(user_id, review_id)
self.cur.execute(query)
self.con.commit()