-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.cpp
executable file
·468 lines (431 loc) · 15.8 KB
/
database.cpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "database.h"
bool database::initDatabase() {
stringstream sql;
stringstream msg;
string pathPic;
cout << "#Starting ... " << endl;
//sql::Driver * driver = sql::mysql::get_driver_instance();
std::auto_ptr< sql::Connection > con(driver->connect(url, user, password));
std::auto_ptr< sql::Statement > stmt(con->createStatement());
/* Create a test table demonstrating the use of sql::Statement.execute() */
stmt->execute("USE " + db);
stmt->execute("DROP TABLE IF EXISTS media_info");
stmt->execute("CREATE TABLE media_info (title VARCHAR(100), category VARCHAR(20), year CHAR(4), length SMALLINT UNSIGNED, director VARCHAR(50), cast VARCHAR(500), content VARCHAR(2000), wiki VARCHAR(10000), pic VARCHAR(200), type VARCHAR(10))");
cout << "#\t media_info table created" << endl;
cout << "#Starting add files ... " << endl;
vector<string> pathset;
strtokenizer strtok;
vector<string>::size_type size = 0;
utils::addfile(path, pathset, size);
/* Populate the test table with data */
try {
for (vector<string>::size_type i = 0; i < size; i++) {
utils::readfile(pathset[i], strtok);
pathPic = pathset[i].substr(0, pathset[i].find_last_of("/"));
sql.str("");
sql << "INSERT INTO media_info (title, category, year, length, director, cast, content, wiki, pic, type) VALUES ('";
sql << strtok.token(2) << "', '"; // title
sql << strtok.token(1) << "', '"; // category
sql << strtok.token(3) << "', "; // year
sql << strtok.token(4) << ", '"; // length
sql << strtok.token(5) << "', '"; // director
sql << strtok.token(6) << "', '"; // cast
sql << strtok.token(7) << "', '"; // content
for (vector<string>::size_type i = 8; i < strtok.count_tokens()-1; i++) {
sql << strtok.token(i);
}
sql << "', '" << pathPic << "/[Image]" << strtok.token(strtok.count_tokens()-1) << ".jpg', '";
sql << strtok.token(0) << "')"; // M or TV
stmt->execute(sql.str());
strtok.clear();
}
stmt-> execute("ALTER TABLE media_info ADD number INT UNSIGNED NOT NULL AUTO_INCREMENT KEY");
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return false;
}
cout << "#\t Test table populated" << endl;
return true;
}
bool database::addUser(const string& name, const string& pwd) const {
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stringstream sql;
stmt->execute("USE " + db);
sql << "SELECT username, password FROM users WHERE username = '" << name << "'";
auto_ptr<ResultSet> res(stmt->executeQuery(sql.str()));
if (res->next()) {
cout << "Username has existed, please choose another one!" << endl;
return false;
}
sql.str("");
sql << "INSERT INTO users (username, password) VALUES (";
sql << "'" << name << "', '" << pwd << "')";
stmt->execute(sql.str());
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return false;
}
return true;
}
bool database::login(const string& name, const string& pwd) const {
stringstream msg;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
msg << "SELECT username, password FROM users WHERE username = '" << name << "'";
auto_ptr<ResultSet> res(stmt->executeQuery(msg.str()));
if (!res->next()) {
cout << "No such username!" << endl;
return false;
}
if ((res->getString("username") != name) || (res->getString("password") != pwd)) {
cout << "Invalid username or password!" << endl;
return false;
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return false;
}
return true;
}
bool database::changePassword(const string& name, const string& origin, const string& pwd) const {
bool loginSuccess = login(name, origin);
if (!loginSuccess) {
cout << "Original password incorrect!" << endl;
return false;
}
stringstream msg;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
msg << "UPDATE users SET password='" << pwd << "' WHERE username='" << name << "'";
stmt->execute(msg.str());
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return false;
}
return true;
}
vector<int> database::search(const string& query, int num, int pos) const {
int row = 0;
stringstream sql;
vector<int> p;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
sql << "SELECT title, director, number FROM media_info WHERE title LIKE \"";
sql << query << "%\" OR title LIKE \"%" << query << "\" OR director LIKE \"";
sql << query << "%\" OR director LIKE \"%" << query << "\"";
cout << sql.str() << endl;
std::auto_ptr<ResultSet> res(stmt->executeQuery(sql.str()));
cout << "#\t Fetching search for " << query << endl;
row = pos;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) {break;}
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return p;
}
void database::preciseFetchDisp(int index) const {
stringstream out;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
out << "SELECT title, length, director, cast, year, content, number, wiki, pic FROM media_info WHERE number=" << index;
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
cout << "# Fetching search for " << index << endl;
if (res->next()) {
cout << "title: " << res->getString("title") << endl;
cout << "content: " << res->getString("content") << endl;
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return;
}
string database::preciseFetch(int index) const {
stringstream out;
string arg, tmp;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
out << "SELECT title, length, director, cast, year, content, number, wiki, pic FROM media_info WHERE number=" << index;
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
cout << "# Fetching search for " << index << endl;
if (res->next()) {
arg = res->getString("title");
arg.append("^^^^^");
arg.append(res->getString("year"));
arg.append("^^^^^");
arg.append(res->getString("length"));
arg.append("^^^^^");
if ((tmp=res->getString("director")) == "") {
arg.append("TBD");
} else {
arg.append(tmp);
}
arg.append("^^^^^");
if ((tmp=res->getString("cast")) == "") {
arg.append("TBD");
} else {
arg.append(tmp);
}
arg.append("^^^^^");
if ((tmp=res->getString("content")) == "") {
arg.append("TBD");
} else {
arg.append(tmp);
}
arg.append("^^^^^");
//arg.push_back(res->getString("wiki"));
string pic = res->getString("pic");
pic = "temp/" + pic;
arg.append(pic.substr(pic.find_last_of("/")+1));
arg.append("^^^^^");
arg.append(res->getString("number"));
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return arg;
}
vector<string> database::fetchPic(int index) {
stringstream out;
vector<string> info;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
out << "SELECT pic, number, title FROM media_info WHERE number=" << index;
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
cout << "# Fetching search for " << index << endl;
if (res->next()) {
info.push_back(res->getString("number"));
info.push_back(res->getString("title"));
info.push_back(res->getString("pic"));
} else {
throw sql::SQLException("fetch error");
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return info;
}
vector<int> database::fetchLatest(int num) {
int row = 0;
stringstream out;
vector<int> p;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
out << "SELECT year, number FROM media_info ORDER BY year DESC";
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return p;
}
vector<int> database::hotCollect(string username, int num) {
int row = 0;
vector<int> p;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
std::auto_ptr<ResultSet> res(stmt->executeQuery("SELECT username, favorite, score, number FROM comments WHERE favorite=1 ORDER BY score DESC"));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
if (p.empty()) {
std::auto_ptr<ResultSet> res(stmt->executeQuery("SELECT year, number FROM media_info ORDER BY year DESC"));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return p;
}
vector<int> database::hotTypeCollect(string username, int num, string type) {
int row = 0;
stringstream out;
vector<int> p;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
out << "SELECT username, favorite, score, number, type FROM comments WHERE favorite=1 AND type='" << type << "' ORDER BY score DESC";
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
if (p.empty()) {
out.str("");
out << "SELECT year, number, type FROM media_info WHERE type='" << type << "' ORDER BY year DESC";
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return p;
}
vector<int> database::hotTypeCollectWithType(string username, int num, string type, string category) {
int row = 0;
stringstream out;
vector<int> p;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
out << "SELECT username, favorite, score, number, type, category FROM comments WHERE favorite=1 AND type='" << type << "' AND category='" << category << "' ORDER BY score DESC";
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
if (p.empty()) {
out.str("");
out << "SELECT year, number, type, category FROM media_info WHERE type='" << type << "' ORDER BY year DESC";
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return p;
}
vector<int> database::FavoriteCollect(string username, int num) {
int row = 0;
vector<int> p;
stringstream msg;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
msg << "SELECT username, favorite, score, number, type FROM comments WHERE favorite=1 AND username='" << username << "' ORDER BY score DESC";
std::auto_ptr<ResultSet> res(stmt->executeQuery(msg.str()));
row = 0;
while (res->next()) {
p.push_back(res->getInt("number"));
row++;
if (row == num) { break; }
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return p;
}
bool database::addComment(string username, int number, string comment, bool favor, int score) {
stringstream msg;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
msg << "SELECT username, number FROM comments WHERE number='" << number << "' AND username='" << username << "'";
std::auto_ptr<ResultSet> res(stmt->executeQuery(msg.str()));
msg.clear();
if (res->next()) {
msg.str("");
msg << "UPDATE comments SET comments='" << comment << "' WHERE username='" << username << "' AND number='" << number << "'";
stmt->execute(msg.str());
} else {
msg.str("");
pair<string,string> cat = getInfo("media_info", number);
msg << "INSERT INTO comments (username, number, comments, favorite, score, type, category) VALUES (";
msg << "'" << username << "', '" << number << "', '" << comment << "', " << favor << ", '" << score << "', '" << cat.first << "', '" << cat.second << "')";
stmt->execute(msg.str());
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return false;
}
return true;
}
pair<string,string> database::getInfo(string table, int number) {
stringstream out;
string str;
try {
auto_ptr<Connection> con(driver->connect(url, user, password));
auto_ptr<Statement> stmt(con->createStatement());
stmt->execute("USE " + db);
out << "SELECT number,type,category FROM " << table << " WHERE number=" << number;
std::auto_ptr<ResultSet> res(stmt->executeQuery(out.str()));
if (res->next()) {
return pair<string,string>(res->getString("type"),res->getString("category"));
}
stmt.reset(NULL);
} catch (sql::SQLException &e) {
cout << "#Error: " << e.what() << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return pair<string,string>("","");
}