Convert Database::CreateWorldRegistration

This commit is contained in:
Akkadius 2019-07-03 23:13:13 -05:00
parent d17cfff8fe
commit 126d8edc57

View File

@ -510,57 +510,41 @@ void Database::UpdateWorldRegistration(unsigned int id, std::string long_name, s
*/ */
bool Database::CreateWorldRegistration(std::string long_name, std::string short_name, unsigned int &id) bool Database::CreateWorldRegistration(std::string long_name, std::string short_name, unsigned int &id)
{ {
if (!database) { auto query = fmt::format(
"SELECT ifnull(max(ServerID),0) + 1 FROM {0}",
server.options.GetWorldRegistrationTable()
);
auto results = QueryDatabase(query);
if (!results.Success() || results.RowCount() != 1) {
return false; return false;
} }
MYSQL_RES *res; auto row = results.begin();
MYSQL_ROW row;
char escaped_long_name[201]; id = atoi(row[0]);
char escaped_short_name[101];
unsigned long length; auto insert_query = fmt::format(
length = mysql_real_escape_string( "INSERT INTO {0} SET ServerID = {1}, ServerLongName = '{2}', ServerShortName = '{3}', \n"
database, "ServerListTypeID = 3, ServerAdminID = 0, ServerTrusted = 0, ServerTagDescription = ''",
escaped_long_name, server.options.GetWorldRegistrationTable(),
long_name.substr(0, 100).c_str(), id,
long_name.substr(0, 100).length()); long_name,
escaped_long_name[length + 1] = 0; short_name
length = mysql_real_escape_string( );
database,
escaped_short_name, auto insert_results = QueryDatabase(insert_query);
short_name.substr(0, 100).c_str(), if (!insert_results.Success()) {
short_name.substr(0, 100).length()); LogF(
escaped_short_name[length + 1] = 0; Logs::General,
std::stringstream query(std::stringstream::in | std::stringstream::out); Logs::Error,
query << "SELECT ifnull(max(ServerID),0) FROM " << server.options.GetWorldRegistrationTable(); "World registration did not exist in the database for {0} - {1}",
long_name,
short_name
);
if (mysql_query(database, query.str().c_str()) != 0) {
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.str().c_str());
return false; return false;
} }
res = mysql_use_result(database); return true;
if (res) {
if ((row = mysql_fetch_row(res)) != nullptr) {
id = atoi(row[0]) + 1;
mysql_free_result(res);
std::stringstream query(std::stringstream::in | std::stringstream::out);
query << "INSERT INTO " << server.options.GetWorldRegistrationTable() << " SET ServerID = " << id;
query << ", ServerLongName = '" << escaped_long_name << "', ServerShortName = '" << escaped_short_name;
query << "', ServerListTypeID = 3, ServerAdminID = 0, ServerTrusted = 0, ServerTagDescription = ''";
if (mysql_query(database, query.str().c_str()) != 0) {
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.str().c_str());
return false;
}
return true;
}
}
Log(Logs::General,
Logs::Error,
"World registration did not exist in the database for %s %s",
long_name.c_str(),
short_name.c_str());
return false;
} }