Migrate query Database::GetFreeID

This commit is contained in:
Akkadius 2019-07-03 22:54:22 -05:00
parent 1a5ce7a9de
commit d17cfff8fe
2 changed files with 26 additions and 38 deletions

View File

@ -231,39 +231,20 @@ bool Database::GetLoginTokenDataFromToken(
*/ */
unsigned int Database::GetFreeID(const std::string &loginserver) unsigned int Database::GetFreeID(const std::string &loginserver)
{ {
if (!database) { auto query = fmt::format(
return false; "SELECT MAX(LoginServerID) + 1 FROM {0} WHERE AccountLoginServer='{1}'",
} server.options.GetAccountTable(),
EscapeString(loginserver)
);
MYSQL_RES *res; auto results = QueryDatabase(query);
MYSQL_ROW row; if (!results.Success() || results.RowCount() != 1) {
std::stringstream query(std::stringstream::in | std::stringstream::out);
query << "SELECT MAX(LoginServerID) + 1 FROM " << server.options.GetAccountTable() << " WHERE AccountLoginServer='";
query << EscapeString(loginserver) << "'";
if (mysql_query(database, query.str().c_str()) != 0) {
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.str().c_str());
return 0; return 0;
} }
res = mysql_use_result(database); auto row = results.begin();
if (res) { return atol(row[0]);
while ((row = mysql_fetch_row(res)) != nullptr) {
if (row[0] == nullptr) {
mysql_free_result(res);
return 1;
}
auto ret = atol(row[0]);
mysql_free_result(res);
return ret;
}
mysql_free_result(res);
}
return 1;
} }
/** /**

View File

@ -478,16 +478,19 @@ void WorldServer::Handle_NewLSInfo(ServerNewLSInfo_Struct *i)
} }
else { else {
Log(Logs::General, Logs::Error, "Handle_NewLSInfo error, local address was too long."); Log(Logs::General, Logs::Error, "Handle_NewLSInfo error, local address was too long.");
return; return;
} }
if (strlen(i->remote_address) <= 125) { if (strlen(i->remote_address) <= 125) {
if (strlen(i->remote_address) == 0) { if (strlen(i->remote_address) == 0) {
remote_ip = GetConnection()->Handle()->RemoteIP(); remote_ip = GetConnection()->Handle()->RemoteIP();
Log(Logs::General, Log(
Logs::General,
Logs::Error, Logs::Error,
"Handle_NewLSInfo error, remote address was null, defaulting to stream address %s.", "Remote address was null, defaulting to stream address %s.",
remote_ip.c_str()); remote_ip.c_str()
);
} }
else { else {
remote_ip = i->remote_address; remote_ip = i->remote_address;
@ -495,10 +498,13 @@ void WorldServer::Handle_NewLSInfo(ServerNewLSInfo_Struct *i)
} }
else { else {
remote_ip = GetConnection()->Handle()->RemoteIP(); remote_ip = GetConnection()->Handle()->RemoteIP();
Log(Logs::General,
Log(
Logs::General,
Logs::Error, Logs::Error,
"Handle_NewLSInfo error, remote address was too long, defaulting to stream address %s.", "Handle_NewLSInfo error, remote address was too long, defaulting to stream address %s.",
remote_ip.c_str()); remote_ip.c_str()
);
} }
if (strlen(i->serverversion) <= 64) { if (strlen(i->serverversion) <= 64) {
@ -687,11 +693,12 @@ void WorldServer::Handle_NewLSInfo(ServerNewLSInfo_Struct *i)
} }
} }
else { else {
Log(Logs::General, LogF(Logs::General,
Logs::World_Server, Logs::World_Server,
"Server %s(%s) attempted to log in but database couldn't find an entry but unregistered servers are allowed.", "Server [{0}] ({1}) is not registered but unregistered servers are allowed",
long_name.c_str(), long_name,
short_name.c_str()); short_name
);
if (server.db->CreateWorldRegistration(long_name, short_name, server_id)) { if (server.db->CreateWorldRegistration(long_name, short_name, server_id)) {
is_server_authorized = true; is_server_authorized = true;