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)
{
if (!database) {
return false;
}
auto query = fmt::format(
"SELECT MAX(LoginServerID) + 1 FROM {0} WHERE AccountLoginServer='{1}'",
server.options.GetAccountTable(),
EscapeString(loginserver)
);
MYSQL_RES *res;
MYSQL_ROW row;
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());
auto results = QueryDatabase(query);
if (!results.Success() || results.RowCount() != 1) {
return 0;
}
res = mysql_use_result(database);
auto row = results.begin();
if (res) {
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;
return atol(row[0]);
}
/**

View File

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