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];
char escaped_short_name[101];
unsigned long length;
length = mysql_real_escape_string(
database,
escaped_long_name,
long_name.substr(0, 100).c_str(),
long_name.substr(0, 100).length());
escaped_long_name[length + 1] = 0;
length = mysql_real_escape_string(
database,
escaped_short_name,
short_name.substr(0, 100).c_str(),
short_name.substr(0, 100).length());
escaped_short_name[length + 1] = 0;
std::stringstream query(std::stringstream::in | std::stringstream::out);
query << "SELECT ifnull(max(ServerID),0) FROM " << server.options.GetWorldRegistrationTable();
if (mysql_query(database, query.str().c_str()) != 0) { id = atoi(row[0]);
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.str().c_str());
return false;
}
res = mysql_use_result(database); auto insert_query = fmt::format(
if (res) { "INSERT INTO {0} SET ServerID = {1}, ServerLongName = '{2}', ServerShortName = '{3}', \n"
if ((row = mysql_fetch_row(res)) != nullptr) { "ServerListTypeID = 3, ServerAdminID = 0, ServerTrusted = 0, ServerTagDescription = ''",
id = atoi(row[0]) + 1; server.options.GetWorldRegistrationTable(),
mysql_free_result(res); id,
long_name,
short_name
);
std::stringstream query(std::stringstream::in | std::stringstream::out); auto insert_results = QueryDatabase(insert_query);
query << "INSERT INTO " << server.options.GetWorldRegistrationTable() << " SET ServerID = " << id; if (!insert_results.Success()) {
query << ", ServerLongName = '" << escaped_long_name << "', ServerShortName = '" << escaped_short_name; LogF(
query << "', ServerListTypeID = 3, ServerAdminID = 0, ServerTrusted = 0, ServerTagDescription = ''"; Logs::General,
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, Logs::Error,
"World registration did not exist in the database for %s %s", "World registration did not exist in the database for {0} - {1}",
long_name.c_str(), long_name,
short_name.c_str()); short_name
);
return false; return false;
}
return true;
} }