7
7
#include " jdbc/mysql_driver.h"
8
8
#include " json/json.h"
9
9
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) {
12
11
std::vector<std::string> usernamelist;
13
12
sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance ();
14
13
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
28
27
29
28
size_t pos = req.find (sender);
30
29
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 );
35
34
36
35
std::string updateQuery = " UPDATE users SET req = ? WHERE username = ?" ;
37
36
sql::PreparedStatement *updateStatement = con->prepareStatement (updateQuery);
@@ -40,8 +39,7 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
40
39
updateStatement->setString (2 , receiver);
41
40
updateStatement->execute ();
42
41
43
- if (attitude == " allow" )
44
- {
42
+ if (attitude == " allow" ) {
45
43
std::string readdata = " SELECT friends FROM users WHERE username = ?" ;
46
44
sql::PreparedStatement *Rreaddatament = con->prepareStatement (readdata);
47
45
sql::PreparedStatement *Sreaddatament = con->prepareStatement (readdata);
@@ -57,35 +55,29 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
57
55
if (SresultSet->next ())
58
56
Sfriendslist = SresultSet->getString (" friends" );
59
57
size_t pos = Rfriendslist.find (sender);
60
- if (pos != std::string::npos)
58
+ if (pos != std::string::npos)
61
59
return ;
62
60
std::string updateQuery = " UPDATE users SET friends = ? WHERE username = ?" ;
63
61
sql::PreparedStatement *updateR = con->prepareStatement (updateQuery);
64
62
sql::PreparedStatement *updateS = con->prepareStatement (updateQuery);
65
63
66
- if (Rfriendslist != " " )
67
- {
64
+ if (Rfriendslist != " " ) {
68
65
Rfriendslist += " ," + sender;
69
66
updateR->setString (1 , Rfriendslist);
70
67
updateR->setString (2 , receiver);
71
68
updateR->execute ();
72
- }
73
- else
74
- {
69
+ } else {
75
70
updateR->setString (1 , sender);
76
71
updateR->setString (2 , receiver);
77
72
updateR->execute ();
78
73
}
79
74
80
- if (Sfriendslist != " " )
81
- {
75
+ if (Sfriendslist != " " ) {
82
76
Sfriendslist += " ," + receiver;
83
77
updateS->setString (1 , Sfriendslist);
84
78
updateS->setString (2 , sender);
85
79
updateS->execute ();
86
- }
87
- else
88
- {
80
+ } else {
89
81
updateS->setString (1 , receiver);
90
82
updateS->setString (2 , sender);
91
83
updateS->execute ();
@@ -95,8 +87,7 @@ void sql_process_request(std::string sender, std::string receiver, std::string a
95
87
return ;
96
88
}
97
89
98
- void sql_addrequest (std::string sender, std::string receiver)
99
- {
90
+ void sql_addrequest (std::string sender, std::string receiver) {
100
91
std::vector<std::string> usernamelist;
101
92
sql::mysql::MySQL_Driver *driver;
102
93
driver = sql::mysql::get_mysql_driver_instance ();
@@ -109,15 +100,13 @@ void sql_addrequest(std::string sender, std::string receiver)
109
100
sql::PreparedStatement *readdatament = con->prepareStatement (readdata);
110
101
readdatament->setString (1 , receiver);
111
102
sql::ResultSet *resultSet = readdatament->executeQuery ();
112
- if (resultSet->next ())
113
- {
103
+ if (resultSet->next ()) {
114
104
std::string req = resultSet->getString (" req" );
115
105
size_t pos = req.find (sender);
116
- if (pos!= std::string::npos)return ;
106
+ if (pos != std::string::npos) return ;
117
107
std::string updateQuery = " UPDATE users SET req = ? WHERE username = ?" ;
118
108
sql::PreparedStatement *updateStatement = con->prepareStatement (updateQuery);
119
- if (req != " " )
120
- {
109
+ if (req != " " ) {
121
110
req += " ," + sender;
122
111
updateStatement->setString (1 , req);
123
112
updateStatement->setString (2 , receiver);
@@ -130,10 +119,8 @@ void sql_addrequest(std::string sender, std::string receiver)
130
119
}
131
120
}
132
121
133
- void sql_addconnect (std::string connectptr)
134
- {
135
- try
136
- {
122
+ void sql_addconnect (std::string connectptr) {
123
+ try {
137
124
sql::mysql::MySQL_Driver *driver;
138
125
driver = sql::mysql::get_mysql_driver_instance ();
139
126
sql::Connection *con;
@@ -147,17 +134,13 @@ void sql_addconnect(std::string connectptr)
147
134
delete insertconnect;
148
135
// delete tool;
149
136
delete con;
150
- }
151
- catch (sql::SQLException &e)
152
- {
137
+ } catch (sql::SQLException &e) {
153
138
std::cerr << " SQL Exception: " << e.what () << std::endl;
154
139
}
155
140
}
156
141
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 {
161
144
sql::mysql::MySQL_Driver *driver;
162
145
driver = sql::mysql::get_mysql_driver_instance ();
163
146
sql::Connection *con;
@@ -175,16 +158,13 @@ void sql_addhistory(std::string sender, std::string receiver, std::string messag
175
158
delete insertdata;
176
159
// delete tool;
177
160
delete con;
178
- }
179
- catch (sql::SQLException &e)
180
- {
161
+ } catch (sql::SQLException &e) {
181
162
std::cerr << " SQL Exception: " << e.what () << std::endl;
182
163
}
183
164
}
184
165
185
166
void sql_add (std::string username, std::string passwd, int avatar) {
186
167
try {
187
-
188
168
sql::mysql::MySQL_Driver *driver;
189
169
driver = sql::mysql::get_mysql_driver_instance ();
190
170
sql::Connection *con;
@@ -203,9 +183,7 @@ void sql_add(std::string username, std::string passwd, int avatar) {
203
183
delete ptool;
204
184
delete tool;
205
185
delete con;
206
- }
207
- catch (sql::SQLException &e)
208
- {
186
+ } catch (sql::SQLException &e) {
209
187
std::cerr << " SQL Exception: " << e.what () << std::endl;
210
188
}
211
189
}
@@ -247,23 +225,19 @@ void sql_add(std::string username, std::string passwd, int avatar) {
247
225
// return result;
248
226
// }
249
227
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) {
252
229
Json::Value json;
253
230
std::string send[2 ] = {me, who_send_me};
254
231
255
- try
256
- {
232
+ try {
257
233
sql::mysql::MySQL_Driver *driver;
258
234
driver = sql::mysql::get_mysql_driver_instance ();
259
235
sql::Connection *con;
260
236
con = driver->connect (" tcp://8.130.48.157:3306" , " root" , " abc.123" );
261
237
con->setSchema (" flypen" );
262
238
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 ()) {
267
241
std::string sql = " SELECT * FROM users WHERE username = ? LIMIT 1" ;
268
242
sql::PreparedStatement *prepStmt = con->prepareStatement (sql);
269
243
prepStmt->setString (1 , send[i]);
@@ -272,34 +246,57 @@ Json::Value get_chat_info(std::string me, std::string who_send_me)
272
246
273
247
if (res->next ()) {
274
248
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
+ }
279
279
user[" avatar" ] = avatar;
280
- user[" friends" ] = friends ;
281
- user[" req" ] = req ;
280
+ user[" friends" ] = friends_array ;
281
+ user[" req" ] = req_array ;
282
282
283
283
Json::StreamWriterBuilder builder;
284
284
std::string userJson = Json::writeString (builder, user);
285
285
286
- json[send[i]] = user;
286
+ json[send[i]] = user;
287
287
}
288
288
}
289
289
}
290
- }
291
- catch (sql::SQLException &e)
292
- {
290
+ } catch (sql::SQLException &e) {
293
291
std::cerr << " SQL Exception: " << e.what () << std::endl;
294
292
}
295
-
293
+
296
294
return json;
297
295
}
298
296
299
297
bool sql_check (std::string user, std::string passwd) {
300
298
bool result = false ;
301
- try
302
- {
299
+ try {
303
300
sql::mysql::MySQL_Driver *driver;
304
301
driver = sql::mysql::get_mysql_driver_instance ();
305
302
sql::Connection *con;
@@ -315,8 +312,7 @@ bool sql_check(std::string user, std::string passwd) {
315
312
316
313
// 获取查询结果
317
314
// (¬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 ())))) {
320
316
result = true ;
321
317
// std::cout << "in" << std::endl;
322
318
// 提取所有列的值
@@ -334,21 +330,17 @@ bool sql_check(std::string user, std::string passwd) {
334
330
delete res;
335
331
delete prepStmt;
336
332
delete con;
337
- }
338
- catch (sql::SQLException &e)
339
- {
333
+ } catch (sql::SQLException &e) {
340
334
std::cerr << " SQL Exception: " << e.what () << std::endl;
341
335
}
342
336
std::cout << result << std::endl;
343
337
344
338
return result;
345
339
}
346
340
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) {
349
342
// std::cout << "login user: " << user << std::endl;
350
- try
351
- {
343
+ try {
352
344
sql::mysql::MySQL_Driver *driver;
353
345
driver = sql::mysql::get_mysql_driver_instance ();
354
346
sql::Connection *con;
@@ -398,9 +390,7 @@ Json::Value sql_find_my_msg(std::string me, std::string who_send_me)
398
390
399
391
Json::Value messages;
400
392
401
- while (res->next ())
402
- {
403
-
393
+ while (res->next ()) {
404
394
std::string content = res->getString (" content" );
405
395
std::string time = res->getString (" time" );
406
396
std::string sender = res->getString (" sender" );
@@ -419,9 +409,7 @@ Json::Value sql_find_my_msg(std::string me, std::string who_send_me)
419
409
delete prepStmt;
420
410
delete con;
421
411
return result;
422
- }
423
- catch (sql::SQLException &e)
424
- {
412
+ } catch (sql::SQLException &e) {
425
413
std::cerr << " SQL Exception: " << e.what () << std::endl;
426
414
return " error" ;
427
415
}
0 commit comments