Skip to content

Commit 273c535

Browse files
update
1 parent b672b49 commit 273c535

File tree

1 file changed

+68
-80
lines changed

1 file changed

+68
-80
lines changed

mysql.cc

Lines changed: 68 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
#include "jdbc/mysql_driver.h"
88
#include "json/json.h"
99

10-
void sql_process_request(std::string sender, std::string receiver, std::string attitude)
11-
{
10+
void sql_process_request(std::string sender, std::string receiver, std::string attitude) {
1211
std::vector<std::string> usernamelist;
1312
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
1413
sql::Connection *con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
@@ -28,10 +27,10 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
2827

2928
size_t pos = req.find(sender);
3029

31-
if (pos != std::string::npos && pos!=0)
32-
req.erase(pos-1, sender.length()+1);
33-
else if(pos == 0)
34-
req.erase(pos, sender.length()+1);
30+
if (pos != std::string::npos && pos != 0)
31+
req.erase(pos - 1, sender.length() + 1);
32+
else if (pos == 0)
33+
req.erase(pos, sender.length() + 1);
3534

3635
std::string updateQuery = "UPDATE users SET req = ? WHERE username = ?";
3736
sql::PreparedStatement *updateStatement = con->prepareStatement(updateQuery);
@@ -40,8 +39,7 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
4039
updateStatement->setString(2, receiver);
4140
updateStatement->execute();
4241

43-
if (attitude == "allow")
44-
{
42+
if (attitude == "allow") {
4543
std::string readdata = "SELECT friends FROM users WHERE username = ?";
4644
sql::PreparedStatement *Rreaddatament = con->prepareStatement(readdata);
4745
sql::PreparedStatement *Sreaddatament = con->prepareStatement(readdata);
@@ -57,35 +55,29 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
5755
if (SresultSet->next())
5856
Sfriendslist = SresultSet->getString("friends");
5957
size_t pos = Rfriendslist.find(sender);
60-
if(pos != std::string::npos)
58+
if (pos != std::string::npos)
6159
return;
6260
std::string updateQuery = "UPDATE users SET friends = ? WHERE username = ?";
6361
sql::PreparedStatement *updateR = con->prepareStatement(updateQuery);
6462
sql::PreparedStatement *updateS = con->prepareStatement(updateQuery);
6563

66-
if (Rfriendslist != "")
67-
{
64+
if (Rfriendslist != "") {
6865
Rfriendslist += "," + sender;
6966
updateR->setString(1, Rfriendslist);
7067
updateR->setString(2, receiver);
7168
updateR->execute();
72-
}
73-
else
74-
{
69+
} else {
7570
updateR->setString(1, sender);
7671
updateR->setString(2, receiver);
7772
updateR->execute();
7873
}
7974

80-
if (Sfriendslist != "")
81-
{
75+
if (Sfriendslist != "") {
8276
Sfriendslist += "," + receiver;
8377
updateS->setString(1, Sfriendslist);
8478
updateS->setString(2, sender);
8579
updateS->execute();
86-
}
87-
else
88-
{
80+
} else {
8981
updateS->setString(1, receiver);
9082
updateS->setString(2, sender);
9183
updateS->execute();
@@ -95,8 +87,7 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
9587
return;
9688
}
9789

98-
void sql_addrequest(std::string sender, std::string receiver)
99-
{
90+
void sql_addrequest(std::string sender, std::string receiver) {
10091
std::vector<std::string> usernamelist;
10192
sql::mysql::MySQL_Driver *driver;
10293
driver = sql::mysql::get_mysql_driver_instance();
@@ -109,15 +100,13 @@ void sql_addrequest(std::string sender, std::string receiver)
109100
sql::PreparedStatement *readdatament = con->prepareStatement(readdata);
110101
readdatament->setString(1, receiver);
111102
sql::ResultSet *resultSet = readdatament->executeQuery();
112-
if (resultSet->next())
113-
{
103+
if (resultSet->next()) {
114104
std::string req = resultSet->getString("req");
115105
size_t pos = req.find(sender);
116-
if(pos!=std::string::npos)return;
106+
if (pos != std::string::npos) return;
117107
std::string updateQuery = "UPDATE users SET req = ? WHERE username = ?";
118108
sql::PreparedStatement *updateStatement = con->prepareStatement(updateQuery);
119-
if (req != "")
120-
{
109+
if (req != "") {
121110
req += "," + sender;
122111
updateStatement->setString(1, req);
123112
updateStatement->setString(2, receiver);
@@ -130,10 +119,8 @@ void sql_addrequest(std::string sender, std::string receiver)
130119
}
131120
}
132121

133-
void sql_addconnect(std::string connectptr)
134-
{
135-
try
136-
{
122+
void sql_addconnect(std::string connectptr) {
123+
try {
137124
sql::mysql::MySQL_Driver *driver;
138125
driver = sql::mysql::get_mysql_driver_instance();
139126
sql::Connection *con;
@@ -147,17 +134,13 @@ void sql_addconnect(std::string connectptr)
147134
delete insertconnect;
148135
// delete tool;
149136
delete con;
150-
}
151-
catch (sql::SQLException &e)
152-
{
137+
} catch (sql::SQLException &e) {
153138
std::cerr << "SQL Exception: " << e.what() << std::endl;
154139
}
155140
}
156141

157-
void sql_addhistory(std::string sender, std::string receiver, std::string message, std::string isread)
158-
{
159-
try
160-
{
142+
void sql_addhistory(std::string sender, std::string receiver, std::string message, std::string isread) {
143+
try {
161144
sql::mysql::MySQL_Driver *driver;
162145
driver = sql::mysql::get_mysql_driver_instance();
163146
sql::Connection *con;
@@ -175,16 +158,13 @@ void sql_addhistory(std::string sender, std::string receiver, std::string messag
175158
delete insertdata;
176159
// delete tool;
177160
delete con;
178-
}
179-
catch (sql::SQLException &e)
180-
{
161+
} catch (sql::SQLException &e) {
181162
std::cerr << "SQL Exception: " << e.what() << std::endl;
182163
}
183164
}
184165

185166
void sql_add(std::string username, std::string passwd, int avatar) {
186167
try {
187-
188168
sql::mysql::MySQL_Driver *driver;
189169
driver = sql::mysql::get_mysql_driver_instance();
190170
sql::Connection *con;
@@ -203,9 +183,7 @@ void sql_add(std::string username, std::string passwd, int avatar) {
203183
delete ptool;
204184
delete tool;
205185
delete con;
206-
}
207-
catch (sql::SQLException &e)
208-
{
186+
} catch (sql::SQLException &e) {
209187
std::cerr << "SQL Exception: " << e.what() << std::endl;
210188
}
211189
}
@@ -247,23 +225,19 @@ void sql_add(std::string username, std::string passwd, int avatar) {
247225
// return result;
248226
// }
249227

250-
Json::Value get_chat_info(std::string me, std::string who_send_me)
251-
{
228+
Json::Value get_chat_info(std::string me, std::string who_send_me) {
252229
Json::Value json;
253230
std::string send[2] = {me, who_send_me};
254231

255-
try
256-
{
232+
try {
257233
sql::mysql::MySQL_Driver *driver;
258234
driver = sql::mysql::get_mysql_driver_instance();
259235
sql::Connection *con;
260236
con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
261237
con->setSchema("flypen");
262238

263-
for(int i = 0; i < 2; i++)
264-
{
265-
if(!send[i].empty())
266-
{
239+
for (int i = 0; i < 2; i++) {
240+
if (!send[i].empty()) {
267241
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
268242
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
269243
prepStmt->setString(1, send[i]);
@@ -272,34 +246,57 @@ Json::Value get_chat_info(std::string me, std::string who_send_me)
272246

273247
if (res->next()) {
274248
Json::Value user;
275-
int avatar=res->getInt("avatar");
276-
std::string friends=res->getString("friends");
277-
std::string req=res->getString("req");
278-
249+
int avatar = res->getInt("avatar");
250+
std::string friends = res->getString("friends");
251+
std::string req = res->getString("req");
252+
Json::Value friends_array(Json::arrayValue);
253+
Json::Value req_array(Json::arrayValue);
254+
std::stringstream sf(friends);
255+
std::stringstream sr(req);
256+
std::string token;
257+
while (std::getline(sf, token, ',')) {
258+
Json::Value afriend;
259+
afriend["username"] = token;
260+
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
261+
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
262+
prepStmt->setString(1, token);
263+
sql::ResultSet *res = prepStmt->executeQuery();
264+
if (res->next())
265+
afriend["avatar"] = res->getInt("avatar"); // 假设avatar统一为4
266+
friends_array.append(afriend);
267+
}
268+
while (std::getline(sr, token, ',')) {
269+
Json::Value areq;
270+
areq["username"] = token;
271+
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
272+
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
273+
prepStmt->setString(1, token);
274+
sql::ResultSet *res = prepStmt->executeQuery();
275+
if (res->next())
276+
areq["avatar"] = res->getInt("avatar"); // 假设avatar统一为4
277+
req_array.append(areq);
278+
}
279279
user["avatar"] = avatar;
280-
user["friends"] = friends;
281-
user["req"] = req;
280+
user["friends"] = friends_array;
281+
user["req"] = req_array;
282282

283283
Json::StreamWriterBuilder builder;
284284
std::string userJson = Json::writeString(builder, user);
285285

286-
json[send[i]] = user;
286+
json[send[i]] = user;
287287
}
288288
}
289289
}
290-
}
291-
catch (sql::SQLException &e)
292-
{
290+
} catch (sql::SQLException &e) {
293291
std::cerr << "SQL Exception: " << e.what() << std::endl;
294292
}
295-
293+
296294
return json;
297295
}
298296

299297
bool sql_check(std::string user, std::string passwd) {
300298
bool result = false;
301-
try
302-
{
299+
try {
303300
sql::mysql::MySQL_Driver *driver;
304301
driver = sql::mysql::get_mysql_driver_instance();
305302
sql::Connection *con;
@@ -315,8 +312,7 @@ bool sql_check(std::string user, std::string passwd) {
315312

316313
// 获取查询结果
317314
// (¬A ∧B) ∨ (A ∧¬B)
318-
if (((passwd != "@DEFAULT@") && (res->next())) || ((passwd == "@DEFAULT@") && (!(res->next()))))
319-
{
315+
if (((passwd != "@DEFAULT@") && (res->next())) || ((passwd == "@DEFAULT@") && (!(res->next())))) {
320316
result = true;
321317
// std::cout << "in" << std::endl;
322318
// 提取所有列的值
@@ -334,21 +330,17 @@ bool sql_check(std::string user, std::string passwd) {
334330
delete res;
335331
delete prepStmt;
336332
delete con;
337-
}
338-
catch (sql::SQLException &e)
339-
{
333+
} catch (sql::SQLException &e) {
340334
std::cerr << "SQL Exception: " << e.what() << std::endl;
341335
}
342336
std::cout << result << std::endl;
343337

344338
return result;
345339
}
346340

347-
Json::Value sql_find_my_msg(std::string me, std::string who_send_me)
348-
{
341+
Json::Value sql_find_my_msg(std::string me, std::string who_send_me) {
349342
// std::cout << "login user: " << user << std::endl;
350-
try
351-
{
343+
try {
352344
sql::mysql::MySQL_Driver *driver;
353345
driver = sql::mysql::get_mysql_driver_instance();
354346
sql::Connection *con;
@@ -398,9 +390,7 @@ Json::Value sql_find_my_msg(std::string me, std::string who_send_me)
398390

399391
Json::Value messages;
400392

401-
while (res->next())
402-
{
403-
393+
while (res->next()) {
404394
std::string content = res->getString("content");
405395
std::string time = res->getString("time");
406396
std::string sender = res->getString("sender");
@@ -419,9 +409,7 @@ Json::Value sql_find_my_msg(std::string me, std::string who_send_me)
419409
delete prepStmt;
420410
delete con;
421411
return result;
422-
}
423-
catch (sql::SQLException &e)
424-
{
412+
} catch (sql::SQLException &e) {
425413
std::cerr << "SQL Exception: " << e.what() << std::endl;
426414
return "error";
427415
}

0 commit comments

Comments
 (0)