Migrate quite a few database calls to dbcore and fmt

This commit is contained in:
Akkadius 2019-07-03 04:03:54 -05:00
parent b04d71ff45
commit daec42c4d9
5 changed files with 85 additions and 81 deletions

View File

@ -71,6 +71,26 @@ DatabaseMySQL::DatabaseMySQL(
Log(Logs::General, Logs::Error, "Failed to connect to MySQL database. Error: %s", mysql_error(database));
exit(1);
}
uint32 errnum = 0;
char errbuf[MYSQL_ERRMSG_SIZE];
if (!Open(
host.c_str(),
user.c_str(),
pass.c_str(),
name.c_str(),
atoi(port.c_str()),
&errnum,
errbuf
)
) {
Log(Logs::General, Logs::Error, "Failed to connect to database: Error: %s", errbuf);
exit(1);
}
else {
Log(Logs::General, Logs::Status, "Using database '%s' at %s:%d", database, host, port);
}
}
else {
Log(Logs::General, Logs::Error, "Failed to create db object in MySQL database.");
@ -273,25 +293,22 @@ bool DatabaseMySQL::CreateLoginDataWithID(
unsigned int id
)
{
if (!database) {
return false;
}
if (id == 0) {
return false;
}
MYSQL_RES *result;
MYSQL_ROW row;
std::stringstream query(std::stringstream::in | std::stringstream::out);
auto query = fmt::format(
"INSERT INTO {0} (LoginServerID, AccountLoginserver, AccountName, AccountPassword, AccountEmail, LastLoginDate, LastIPAddress) "
"VALUES ({1}, '{2}', '{3}', '{4}', 'local_creation', NOW(), '127.0.0.1')",
server.options.GetAccountTable(),
id,
EscapeString(loginserver),
EscapeString(name),
EscapeString(password)
);
query << "INSERT INTO " << server.options.GetAccountTable()
<< " (LoginServerID, AccountLoginserver, AccountName, AccountPassword, AccountEmail, LastLoginDate, LastIPAddress) ";
query << " VALUES(" << id << ", '" << EscapeString(loginserver) << "', '" << EscapeString(name) << "', '"
<< EscapeString(password) << "', 'local_creation', NOW(), '127.0.0.1'); ";
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()) {
return false;
}
@ -305,20 +322,15 @@ bool DatabaseMySQL::CreateLoginDataWithID(
*/
void DatabaseMySQL::UpdateLoginHash(const std::string &name, const std::string &loginserver, const std::string &hash)
{
if (!database) {
return;
}
auto query = fmt::format(
"UPDATE {0} SET AccountPassword='{1}' WHERE AccountName='{2}' AND AccountLoginserver='{3}'",
server.options.GetAccountTable(),
hash,
EscapeString(name),
EscapeString(loginserver));
EscapeString(loginserver)
);
if (mysql_query(database, query.c_str()) != 0) {
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.c_str());
}
QueryDatabase(query);
}
/**
@ -423,19 +435,14 @@ bool DatabaseMySQL::GetWorldRegistration(
*/
void DatabaseMySQL::UpdateLSAccountData(unsigned int id, std::string ip_address)
{
if (!database) {
return;
}
auto query = fmt::format(
"UPDATE {0} SET LastIPAddress = '{2}', LastLoginDate = now() where LoginServerId = {3}",
server.options.GetAccountTable(),
ip_address,
id
);
std::stringstream query(std::stringstream::in | std::stringstream::out);
query << "UPDATE " << server.options.GetAccountTable() << " SET LastIPAddress = '";
query << ip_address;
query << "', LastLoginDate = now() where LoginServerID = ";
query << id;
if (mysql_query(database, query.str().c_str()) != 0) {
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.str().c_str());
}
QueryDatabase(query);
}
/**
@ -444,21 +451,24 @@ void DatabaseMySQL::UpdateLSAccountData(unsigned int id, std::string ip_address)
* @param password
* @param email
*/
void DatabaseMySQL::UpdateLSAccountInfo(unsigned int id, std::string name, std::string password, std::string email)
void DatabaseMySQL::UpdateLSAccountInfo(
unsigned int id,
std::string name,
std::string password,
std::string email
)
{
if (!database) {
return;
}
auto query = fmt::format(
"REPLACE {0} SET LoginServerID = {1}, AccountName = '{2}', AccountPassword = sha('{3}'), AccountCreateDate = now(), "
"AccountEmail = '{4}', LastIPAddress = '0.0.0.0', LastLoginDate = now()",
server.options.GetAccountTable(),
id,
EscapeString(name),
EscapeString(password),
EscapeString(email)
);
std::stringstream query(std::stringstream::in | std::stringstream::out);
query << "REPLACE " << server.options.GetAccountTable() << " SET LoginServerID = ";
query << id << ", AccountName = '" << name << "', AccountPassword = sha('";
query << password << "'), AccountCreateDate = now(), AccountEmail = '" << email;
query << "', LastIPAddress = '0.0.0.0', LastLoginDate = now()";
if (mysql_query(database, query.str().c_str()) != 0) {
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.str().c_str());
}
QueryDatabase(query);
}
/**
@ -468,30 +478,15 @@ void DatabaseMySQL::UpdateLSAccountInfo(unsigned int id, std::string name, std::
*/
void DatabaseMySQL::UpdateWorldRegistration(unsigned int id, std::string long_name, std::string ip_address)
{
if (!database) {
return;
}
auto query = fmt::format(
"UPDATE {0} SET ServerLastLoginDate = NOW(), ServerLastIPAddr = '{1}', ServerLongName = '{2}' WHERE ServerID = {3}",
server.options.GetWorldRegistrationTable(),
ip_address,
EscapeString(long_name),
id
);
char escaped_long_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;
std::stringstream query(std::stringstream::in | std::stringstream::out);
query << "UPDATE " << server.options.GetWorldRegistrationTable()
<< " SET ServerLastLoginDate = now(), ServerLastIPAddr = '";
query << ip_address;
query << "', ServerLongName = '";
query << escaped_long_name;
query << "' WHERE ServerID = ";
query << id;
if (mysql_query(database, query.str().c_str()) != 0) {
Log(Logs::General, Logs::Error, "Mysql query failed: %s", query.str().c_str());
}
QueryDatabase(query);
}
/**

View File

@ -22,6 +22,7 @@
#define EQEMU_DATABASEMYSQL_H
#include "database.h"
#include "../common/dbcore.h"
#ifdef EQEMU_MYSQL_ENABLED
@ -30,7 +31,7 @@
#include <stdlib.h>
#include <mysql.h>
class DatabaseMySQL : public Database {
class DatabaseMySQL : public DBcore {
public:
DatabaseMySQL() { database = nullptr; }

View File

@ -18,6 +18,7 @@
*
*/
#ifndef EQEMU_LOGINSERVER_H
#define EQEMU_LOGINSERVER_H

View File

@ -47,8 +47,12 @@ int main()
LogSys.LoadLogSettingsDefaults();
LogSys.log_settings[Logs::Error].log_to_console = Logs::General;
LogSys.log_settings[Logs::Error].is_category_enabled = 1;
LogSys.log_settings[Logs::Error].log_to_console = Logs::General;
LogSys.log_settings[Logs::Error].is_category_enabled = Logs::General;
LogSys.log_settings[Logs::MySQLError].is_category_enabled = Logs::General;
LogSys.log_settings[Logs::MySQLError].log_to_console = Logs::General;
LogSys.log_settings[Logs::Netcode].is_category_enabled = Logs::General;
LogSys.log_settings[Logs::Netcode].log_to_console = Logs::General;
Log(Logs::General, Logs::Login_Server, "Logging System Init.");
@ -133,7 +137,6 @@ int main()
server.config.GetVariableString("database", "db", "peq")
);
/**
* make sure our database got created okay, otherwise cleanup and exit
*/

View File

@ -379,12 +379,16 @@ void WorldServer::ProcessLSAccountUpdate(uint16_t opcode, const EQ::Net::Packet
ServerLSAccountUpdate_Struct *lsau = (ServerLSAccountUpdate_Struct *) p.Data();
if (is_server_trusted) {
Log(Logs::General, Logs::Netcode, "ServerOP_LSAccountUpdate update processed for: %s", lsau->useraccount);
std::string name;
std::string password;
std::string email;
std::string name = "";
std::string password = "";
std::string email = "";
name.assign(lsau->useraccount);
password.assign(lsau->userpassword);
email.assign(lsau->useremail);
if (lsau->useremail) {
email.assign(lsau->useremail);
}
server.db->UpdateLSAccountInfo(lsau->useraccountid, name, password, email);
}
}
@ -614,9 +618,9 @@ void WorldServer::Handle_NewLSInfo(ServerNewLSInfo_Struct *i)
}
}
/**
* this is the first of two cases where we should deny access even if unregistered is allowed
*/
/**
* this is the first of two cases where we should deny access even if unregistered is allowed
*/
else {
Log(Logs::General,
Logs::World_Server,