mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-22 13:37:22 +00:00
15dde782cf
test Revert "test" This reverts commit fb831f268b7a2f0ccd20595aff64902ab4f4b4ee.
98 lines
2.5 KiB
C++
98 lines
2.5 KiB
C++
#include "Database.h"
|
|
#include "Game.h"
|
|
#include "dConfig.h"
|
|
#include "dLogger.h"
|
|
using namespace std;
|
|
|
|
#pragma warning (disable:4251) //Disables SQL warnings
|
|
|
|
sql::Driver* Database::driver;
|
|
sql::Connection* Database::con;
|
|
sql::Properties Database::props;
|
|
std::string Database::database;
|
|
|
|
void Database::Connect(const string& host, const string& database, const string& username, const string& password) {
|
|
|
|
//To bypass debug issues:
|
|
std::string newHost = "tcp://" + host;
|
|
const char* szHost = newHost.c_str();
|
|
const char* szDatabase = database.c_str();
|
|
const char* szUsername = username.c_str();
|
|
const char* szPassword = password.c_str();
|
|
|
|
driver = sql::mariadb::get_driver_instance();
|
|
|
|
sql::Properties properties;
|
|
properties["hostName"] = szHost;
|
|
properties["user"] = szUsername;
|
|
properties["password"] = szPassword;
|
|
properties["autoReconnect"] = "true";
|
|
|
|
Database::props = properties;
|
|
Database::database = database;
|
|
|
|
Database::Connect();
|
|
}
|
|
|
|
void Database::Connect() {
|
|
con = driver->connect(Database::props);
|
|
con->setSchema(Database::database);
|
|
}
|
|
|
|
void Database::Destroy(std::string source, bool log) {
|
|
if (!con) return;
|
|
|
|
if (log) {
|
|
if (source != "") Game::logger->Log("Database", "Destroying MySQL connection from %s!", source.c_str());
|
|
else Game::logger->Log("Database", "Destroying MySQL connection!");
|
|
}
|
|
|
|
con->close();
|
|
delete con;
|
|
} //Destroy
|
|
|
|
sql::Statement* Database::CreateStmt() {
|
|
sql::Statement* toReturn = con->createStatement();
|
|
return toReturn;
|
|
} //CreateStmt
|
|
|
|
sql::PreparedStatement* Database::CreatePreppedStmt(const std::string& query) {
|
|
const char* test = query.c_str();
|
|
size_t size = query.length();
|
|
sql::SQLString str(test, size);
|
|
|
|
if (!con) {
|
|
Connect();
|
|
Game::logger->Log("Database", "Trying to reconnect to MySQL");
|
|
}
|
|
|
|
if (!con->isValid() || con->isClosed()) {
|
|
delete con;
|
|
|
|
con = nullptr;
|
|
|
|
Connect();
|
|
Game::logger->Log("Database", "Trying to reconnect to MySQL from invalid or closed connection");
|
|
}
|
|
|
|
auto* stmt = con->prepareStatement(str);
|
|
|
|
return stmt;
|
|
} //CreatePreppedStmt
|
|
|
|
void Database::Commit() {
|
|
Database::con->commit();
|
|
}
|
|
|
|
bool Database::GetAutoCommit() {
|
|
// TODO This should not just access a pointer. A future PR should update this
|
|
// to check for null and throw an error if the connection is not valid.
|
|
return con->getAutoCommit();
|
|
}
|
|
|
|
void Database::SetAutoCommit(bool value) {
|
|
// TODO This should not just access a pointer. A future PR should update this
|
|
// to check for null and throw an error if the connection is not valid.
|
|
Database::con->setAutoCommit(value);
|
|
}
|