Remove unused MySQL interface

This commit is contained in:
Zwelf 2020-07-07 20:28:50 +02:00
parent 7c31a15c93
commit b898f8c7c2
7 changed files with 3 additions and 450 deletions

View file

@ -1857,10 +1857,6 @@ set_src(ENGINE_SERVER GLOB_RECURSE src/engine/server
register.h
server.cpp
server.h
sql_connector.cpp
sql_connector.h
sql_server.cpp
sql_server.h
sql_string_helpers.cpp
sql_string_helpers.h
upnp.cpp

View file

@ -1,41 +0,0 @@
#if defined(CONF_SQL)
#include <base/system.h>
#include "sql_connector.h"
CSqlServer** CSqlConnector::ms_ppSqlReadServers = 0;
CSqlServer** CSqlConnector::ms_ppSqlWriteServers = 0;
int CSqlConnector::ms_ReachableReadServer = 0;
int CSqlConnector::ms_ReachableWriteServer = 0;
CSqlConnector::CSqlConnector() :
m_pSqlServer(0),
m_NumReadRetries(0),
m_NumWriteRetries(0)
{}
bool CSqlConnector::ConnectSqlServer(bool ReadOnly)
{
ReadOnly ? ++m_NumReadRetries : ++m_NumWriteRetries;
int& ReachableServer = ReadOnly ? ms_ReachableReadServer : ms_ReachableWriteServer;
int NumServers = ReadOnly ? CSqlServer::ms_NumReadServer : CSqlServer::ms_NumWriteServer;
for (int i = ReachableServer, ID = ReachableServer; i < ReachableServer + NumServers && SqlServer(i % NumServers, ReadOnly); i++, ID = i % NumServers)
{
if (SqlServer(ID, ReadOnly) && SqlServer(ID, ReadOnly)->Connect())
{
m_pSqlServer = SqlServer(ID, ReadOnly);
ReachableServer = ID;
return true;
}
if (SqlServer(ID, ReadOnly))
dbg_msg("sql", "Warning: Unable to connect to Sql%sServer %d ('%s'), trying next...", ReadOnly ? "Read" : "Write", ID, SqlServer(ID, ReadOnly)->GetIP());
}
dbg_msg("sql", "FATAL ERROR: No Sql%sServers available", ReadOnly ? "Read" : "Write");
m_pSqlServer = 0;
return false;
}
#endif

View file

@ -1,47 +0,0 @@
#ifndef ENGINE_SERVER_SQL_CONNECTOR_H
#define ENGINE_SERVER_SQL_CONNECTOR_H
#include "sql_server.h"
enum
{
MAX_SQLSERVERS=15
};
// implementation to provide sqlservers
class CSqlConnector
{
public:
CSqlConnector();
CSqlServer* SqlServer(int i, bool ReadOnly = true) { return ReadOnly ? ms_ppSqlReadServers[i] : ms_ppSqlWriteServers[i]; }
// always returns the last connected sql-server
CSqlServer* SqlServer() { return m_pSqlServer; }
static void SetReadServers(CSqlServer** ppReadServers) { ms_ppSqlReadServers = ppReadServers; }
static void SetWriteServers(CSqlServer** ppWriteServers) { ms_ppSqlWriteServers = ppWriteServers; }
static void ResetReachable() { ms_ReachableReadServer = 0; ms_ReachableWriteServer = 0; }
bool ConnectSqlServer(bool ReadOnly = true);
bool MaxTriesReached(bool ReadOnly = true) { return ReadOnly ? m_NumReadRetries >= CSqlServer::ms_NumReadServer : m_NumWriteRetries >= CSqlServer::ms_NumWriteServer; }
private:
CSqlServer *m_pSqlServer;
static CSqlServer **ms_ppSqlReadServers;
static CSqlServer **ms_ppSqlWriteServers;
static int ms_NumReadServer;
static int ms_NumWriteServer;
static int ms_ReachableReadServer;
static int ms_ReachableWriteServer;
int m_NumReadRetries;
int m_NumWriteRetries;
};
#endif

View file

@ -1,223 +0,0 @@
#if defined(CONF_SQL)
#include <base/system.h>
#include <engine/shared/protocol.h>
#include <engine/shared/config.h>
#include "sql_server.h"
int CSqlServer::ms_NumReadServer = 0;
int CSqlServer::ms_NumWriteServer = 0;
CSqlServer::CSqlServer(const char *pDatabase, const char *pPrefix, const char *pUser, const char *pPass, const char *pIp, int Port, lock *pGlobalLock, bool ReadOnly, bool SetUpDb) :
m_Port(Port),
m_SetUpDB(SetUpDb),
m_SqlLock(),
m_pGlobalLock(pGlobalLock)
{
str_copy(m_aDatabase, pDatabase, sizeof(m_aDatabase));
str_copy(m_aPrefix, pPrefix, sizeof(m_aPrefix));
str_copy(m_aUser, pUser, sizeof(m_aUser));
str_copy(m_aPass, pPass, sizeof(m_aPass));
str_copy(m_aIp, pIp, sizeof(m_aIp));
m_pDriver = 0;
m_pConnection = 0;
m_pResults = 0;
m_pStatement = 0;
ReadOnly ? ms_NumReadServer++ : ms_NumWriteServer++;
}
CSqlServer::~CSqlServer()
{
scope_lock LockScope(&m_SqlLock);
try
{
if (m_pResults)
delete m_pResults;
if (m_pConnection)
{
delete m_pConnection;
m_pConnection = 0;
}
dbg_msg("sql", "SQL connection disconnected");
}
catch (sql::SQLException &e)
{
dbg_msg("sql", "ERROR: No SQL connection: %s", e.what());
}
catch (const std::exception& ex)
{
dbg_msg("sql", "ERROR: No SQL connection: %s", ex.what());
}
catch (const std::string& ex)
{
dbg_msg("sql", "ERROR: No SQL connection: %s", ex.c_str());
}
catch (...)
{
dbg_msg("sql", "Unknown Error cause by the MySQL/C++ Connector");
}
}
bool CSqlServer::Connect()
{
m_SqlLock.take();
if (m_pDriver != NULL && m_pConnection != NULL)
{
try
{
// Connect to specific database
m_pConnection->setSchema(m_aDatabase);
return true;
}
catch (sql::SQLException &e)
{
dbg_msg("sql", "MySQL Error: %s", e.what());
}
catch (const std::exception& ex)
{
dbg_msg("sql", "MySQL Error: %s", ex.what());
}
catch (const std::string& ex)
{
dbg_msg("sql", "MySQL Error: %s", ex.c_str());
}
catch (...)
{
dbg_msg("sql", "Unknown Error cause by the MySQL/C++ Connector");
}
m_SqlLock.release();
dbg_msg("sql", "ERROR: SQL connection failed");
return false;
}
try
{
m_pDriver = 0;
m_pConnection = 0;
m_pStatement = 0;
sql::ConnectOptionsMap connection_properties;
connection_properties["hostName"] = sql::SQLString(m_aIp);
connection_properties["port"] = m_Port;
connection_properties["userName"] = sql::SQLString(m_aUser);
connection_properties["password"] = sql::SQLString(m_aPass);
connection_properties["OPT_CONNECT_TIMEOUT"] = 10;
connection_properties["OPT_READ_TIMEOUT"] = 10;
connection_properties["OPT_WRITE_TIMEOUT"] = 20;
connection_properties["OPT_RECONNECT"] = true;
connection_properties["OPT_CHARSET_NAME"] = sql::SQLString("utf8mb4");
connection_properties["OPT_SET_CHARSET_NAME"] = sql::SQLString("utf8mb4");
// Create connection
{
scope_lock GlobalLockScope(m_pGlobalLock);
m_pDriver = get_driver_instance();
}
m_pConnection = m_pDriver->connect(connection_properties);
// Create Statement
m_pStatement = m_pConnection->createStatement();
// Apparently OPT_CHARSET_NAME and OPT_SET_CHARSET_NAME are not enough
m_pStatement->execute("SET CHARACTER SET utf8mb4;");
if (m_SetUpDB)
{
char aBuf[128];
// create database
str_format(aBuf, sizeof(aBuf), "CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8mb4", m_aDatabase);
m_pStatement->execute(aBuf);
}
// Connect to specific database
m_pConnection->setSchema(m_aDatabase);
dbg_msg("sql", "sql connection established");
return true;
}
catch (sql::SQLException &e)
{
dbg_msg("sql", "MySQL Error: %s", e.what());
}
catch (const std::exception& ex)
{
dbg_msg("sql", "MySQL Error: %s", ex.what());
}
catch (const std::string& ex)
{
dbg_msg("sql", "MySQL Error: %s", ex.c_str());
}
catch (...)
{
dbg_msg("sql", "Unknown Error cause by the MySQL/C++ Connector");
}
dbg_msg("sql", "ERROR: sql connection failed");
m_SqlLock.release();
return false;
}
void CSqlServer::Disconnect()
{
m_SqlLock.release();
}
bool CSqlServer::CreateTables()
{
if (!Connect())
return false;
bool Success = false;
try
{
char aBuf[1024];
// create tables
str_format(aBuf, sizeof(aBuf), "CREATE TABLE IF NOT EXISTS %s_race (Map VARCHAR(128) BINARY NOT NULL, Name VARCHAR(%d) BINARY NOT NULL, Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, Time FLOAT DEFAULT 0, Server CHAR(4), cp1 FLOAT DEFAULT 0, cp2 FLOAT DEFAULT 0, cp3 FLOAT DEFAULT 0, cp4 FLOAT DEFAULT 0, cp5 FLOAT DEFAULT 0, cp6 FLOAT DEFAULT 0, cp7 FLOAT DEFAULT 0, cp8 FLOAT DEFAULT 0, cp9 FLOAT DEFAULT 0, cp10 FLOAT DEFAULT 0, cp11 FLOAT DEFAULT 0, cp12 FLOAT DEFAULT 0, cp13 FLOAT DEFAULT 0, cp14 FLOAT DEFAULT 0, cp15 FLOAT DEFAULT 0, cp16 FLOAT DEFAULT 0, cp17 FLOAT DEFAULT 0, cp18 FLOAT DEFAULT 0, cp19 FLOAT DEFAULT 0, cp20 FLOAT DEFAULT 0, cp21 FLOAT DEFAULT 0, cp22 FLOAT DEFAULT 0, cp23 FLOAT DEFAULT 0, cp24 FLOAT DEFAULT 0, cp25 FLOAT DEFAULT 0, GameID VARCHAR(64), DDNet7 BOOL DEFAULT FALSE, KEY (Map, Name)) CHARACTER SET utf8mb4;", m_aPrefix, MAX_NAME_LENGTH);
executeSql(aBuf);
str_format(aBuf, sizeof(aBuf), "CREATE TABLE IF NOT EXISTS %s_teamrace (Map VARCHAR(128) BINARY NOT NULL, Name VARCHAR(%d) BINARY NOT NULL, Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, Time FLOAT DEFAULT 0, ID VARBINARY(16) NOT NULL, GameID VARCHAR(64), DDNet7 BOOL DEFAULT FALSE, KEY Map (Map)) CHARACTER SET utf8mb4;", m_aPrefix, MAX_NAME_LENGTH);
executeSql(aBuf);
str_format(aBuf, sizeof(aBuf), "CREATE TABLE IF NOT EXISTS %s_maps (Map VARCHAR(128) BINARY NOT NULL, Server VARCHAR(32) BINARY NOT NULL, Mapper VARCHAR(128) BINARY NOT NULL, Points INT DEFAULT 0, Stars INT DEFAULT 0, Timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY Map (Map)) CHARACTER SET utf8mb4;", m_aPrefix);
executeSql(aBuf);
str_format(aBuf, sizeof(aBuf), "CREATE TABLE IF NOT EXISTS %s_saves (Savegame TEXT CHARACTER SET utf8mb4 BINARY NOT NULL, Map VARCHAR(128) BINARY NOT NULL, Code VARCHAR(128) BINARY NOT NULL, Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, Server CHAR(4), DDNet7 BOOL DEFAULT FALSE, SaveID VARCHAR(36) DEFAULT NULL, UNIQUE KEY (Map, Code)) CHARACTER SET utf8mb4;", m_aPrefix);
executeSql(aBuf);
str_format(aBuf, sizeof(aBuf), "CREATE TABLE IF NOT EXISTS %s_points (Name VARCHAR(%d) BINARY NOT NULL, Points INT DEFAULT 0, UNIQUE KEY Name (Name)) CHARACTER SET utf8mb4;", m_aPrefix, MAX_NAME_LENGTH);
executeSql(aBuf);
dbg_msg("sql", "Tables were created successfully");
Success = true;
}
catch (sql::SQLException &e)
{
dbg_msg("sql", "MySQL Error: %s", e.what());
}
Disconnect();
return Success;
}
void CSqlServer::executeSql(const char *pCommand)
{
m_pStatement->execute(pCommand);
}
void CSqlServer::executeSqlQuery(const char *pQuery)
{
if (m_pResults)
delete m_pResults;
// set it to 0, so exceptions raised from executeQuery can not make m_pResults point to invalid memory
m_pResults = 0;
m_pResults = m_pStatement->executeQuery(pQuery);
}
#endif

View file

@ -1,58 +0,0 @@
#ifndef ENGINE_SERVER_SQL_SERVER_H
#define ENGINE_SERVER_SQL_SERVER_H
#include <base/tl/threading.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/statement.h>
class CSqlServer
{
public:
CSqlServer(const char *pDatabase, const char *pPrefix, const char *pUser, const char *pPass, const char *pIp, int Port, lock *pGlobalLock, bool ReadOnly = true, bool SetUpDb = false);
~CSqlServer();
bool Connect();
void Disconnect();
bool CreateTables();
void executeSql(const char *pCommand);
void executeSqlQuery(const char *pQuery);
sql::ResultSet* GetResults() { return m_pResults; }
const char* GetDatabase() { return m_aDatabase; }
const char* GetPrefix() { return m_aPrefix; }
const char* GetUser() { return m_aUser; }
const char* GetPass() { return m_aPass; }
const char* GetIP() { return m_aIp; }
int GetPort() { return m_Port; }
sql::Connection *Connection() const { return m_pConnection; }
static int ms_NumReadServer;
static int ms_NumWriteServer;
private:
sql::Driver *m_pDriver;
sql::Connection *m_pConnection;
sql::Statement *m_pStatement;
sql::ResultSet *m_pResults;
// copy of config vars
char m_aDatabase[64];
char m_aPrefix[64];
char m_aUser[64];
char m_aPass[64];
char m_aIp[64];
int m_Port;
bool m_SetUpDB;
lock m_SqlLock;
lock *m_pGlobalLock;
};
#endif

View file

@ -1,8 +1,8 @@
#include "sql_string_helpers.h"
#include <base/system.h>
#include <cmath>
#include <cstring>
#include <base/system.h>
#include "sql_string_helpers.h"
void sqlstr::FuzzyString(char *pString, int size)
{
@ -41,41 +41,6 @@ int sqlstr::EscapeLike(char *pDst, const char *pSrc, int DstSize)
return DstPos;
}
// anti SQL injection
void sqlstr::ClearString(char *pString, int size)
{
char *newString = new char [size * 2 - 1];
int pos = 0;
for(int i = 0; i < size; i++)
{
if(pString[i] == '\\')
{
newString[pos++] = '\\';
newString[pos++] = '\\';
}
else if(pString[i] == '\'')
{
newString[pos++] = '\\';
newString[pos++] = '\'';
}
else if(pString[i] == '"')
{
newString[pos++] = '\\';
newString[pos++] = '"';
}
else
{
newString[pos++] = pString[i];
}
}
newString[pos] = '\0';
str_copy(pString, newString, size);
delete [] newString;
}
void sqlstr::AgoTimeToString(int AgoTime, char *pAgoString)
{
char aBuf[20];

View file

@ -1,55 +1,16 @@
#ifndef ENGINE_SERVER_SQL_STRING_HELPERS_H
#define ENGINE_SERVER_SQL_STRING_HELPERS_H
#include <base/system.h>
namespace sqlstr
{
void FuzzyString(char *pString, int size);
// anti SQL injection
void ClearString(char *pString, int size = 32);
// written number of added bytes
int EscapeLike(char *pDst, const char *pSrc, int DstSize);
void AgoTimeToString(int agoTime, char *pAgoString);
template<unsigned int size>
class CSqlString
{
public:
CSqlString() {}
CSqlString(const char *pStr)
{
str_copy(m_aString, pStr, size);
str_copy(m_aClearString, pStr, size);
ClearString(m_aClearString, sizeof(m_aClearString));
}
const char* Str() const { return m_aString; }
const char* ClrStr() const { return m_aClearString; }
CSqlString& operator=(const char *pStr)
{
str_copy(m_aString, pStr, size);
str_copy(m_aClearString, pStr, size);
ClearString(m_aClearString, sizeof(m_aClearString));
return *this;
}
bool operator<(const CSqlString& other) const
{
return str_comp(m_aString, other.m_aString) < 0;
}
private:
char m_aString[size];
char m_aClearString[size * 2 - 1];
};
}
#endif