Skip to content

Commit 36a0f9b

Browse files
update
1 parent 9d68bcc commit 36a0f9b

File tree

2 files changed

+38
-45
lines changed

2 files changed

+38
-45
lines changed

mysql.cc

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -230,64 +230,58 @@ void sql_add(std::string username, std::string passwd, int avatar) {
230230

231231
// return result;
232232
// }
233-
Json::Value get_chat_info(std::string me){
233+
Json::Value get_chat_info(std::string me, std::string who_send_me="") {
234234
Json::Value json;
235+
235236
try {
236237
sql::mysql::MySQL_Driver *driver;
237238
driver = sql::mysql::get_mysql_driver_instance();
238239
sql::Connection *con;
239240
con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
240241
con->setSchema("flypen");
241242

242-
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
243-
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
244-
prepStmt->setString(1, me);
245-
246-
sql::ResultSet *res = prepStmt->executeQuery();
247-
if (res->next()) {
248-
Json::Value user;
249-
int avatar=res->getInt("avatar");
250-
std::string friends=res->getString("friends");
251-
std::string req=res->getString("req");
252-
user["avatar"] = avatar;
253-
user["friends"] = friends;
254-
user["req"] = req;
255-
256-
json[me] = user;
243+
if (!who_send_me.empty()) {
244+
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
245+
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
246+
prepStmt->setString(1, who_send_me);
247+
248+
sql::ResultSet *res = prepStmt->executeQuery();
249+
if (res->next()) {
250+
Json::Value user;
251+
int avatar=res->getInt("avatar");
252+
std::string friends=res->getString("friends");
253+
std::string req=res->getString("req");
254+
user["avatar"] = avatar;
255+
user["friends"] = friends;
256+
user["req"] = req;
257+
258+
json[who_send_me] = user;
259+
}
257260
}
258-
} catch (sql::SQLException &e) {
259-
std::cerr << "SQL Exception: " << e.what() << std::endl;
260-
}
261-
return json;
262-
}
263-
Json::Value get_chat_info(std::string me,std::string who_send_me){
264-
Json::Value json;
265-
try {
266-
sql::mysql::MySQL_Driver *driver;
267-
driver = sql::mysql::get_mysql_driver_instance();
268-
sql::Connection *con;
269-
con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
270-
con->setSchema("flypen");
271261

272-
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
273-
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
274-
prepStmt->setString(1, who_send_me);
275-
276-
sql::ResultSet *res = prepStmt->executeQuery();
277-
if (res->next()) {
278-
Json::Value user;
279-
int avatar=res->getInt("avatar");
280-
std::string friends=res->getString("friends");
281-
std::string req=res->getString("req");
282-
user["avatar"] = avatar;
283-
user["friends"] = friends;
284-
user["req"] = req;
285-
286-
json[who_send_me] = user;
262+
if (!me.empty()) {
263+
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
264+
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
265+
prepStmt->setString(1, me);
266+
267+
sql::ResultSet *res = prepStmt->executeQuery();
268+
if (res->next()) {
269+
Json::Value user;
270+
int avatar=res->getInt("avatar");
271+
std::string friends=res->getString("friends");
272+
std::string req=res->getString("req");
273+
user["avatar"] = avatar;
274+
user["friends"] = friends;
275+
user["req"] = req;
276+
277+
json[me] = user;
278+
}
287279
}
280+
288281
} catch (sql::SQLException &e) {
289282
std::cerr << "SQL Exception: " << e.what() << std::endl;
290283
}
284+
291285
return json;
292286
}
293287
bool sql_check(std::string user, std::string passwd) {

mysql.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ void sql_addrequest(std::string send,std::string receiver);
1212
void sql_process_request(std::string ,std::string,std::string);
1313
Json::Value sql_find_my_msg(std::string,std::string);
1414
Json::Value get_chat_info(std::string,std::string);
15-
Json::Value get_chat_info(std::string);
1615
//bool sql_check(std::string );
1716
#endif

0 commit comments

Comments
 (0)