mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
Define new database interface
This commit is contained in:
parent
6e6e51e626
commit
d59cdf47b5
|
@ -1834,11 +1834,14 @@ set_src(ANTIBOT_SRC GLOB src/antibot
|
|||
antibot_null.cpp
|
||||
)
|
||||
|
||||
set_src(ENGINE_SERVER GLOB src/engine/server
|
||||
set_src(ENGINE_SERVER GLOB_RECURSE src/engine/server
|
||||
antibot.cpp
|
||||
antibot.h
|
||||
authmanager.cpp
|
||||
authmanager.h
|
||||
databases/connection.h
|
||||
databases/mysql.cpp
|
||||
databases/mysql.h
|
||||
name_ban.cpp
|
||||
name_ban.h
|
||||
register.cpp
|
||||
|
|
58
src/engine/server/databases/connection.h
Normal file
58
src/engine/server/databases/connection.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
#ifndef ENGINE_SERVER_DATABASES_CONNECTION_H
|
||||
#define ENGINE_SERVER_DATABASES_CONNECTION_H
|
||||
|
||||
#include <base/system.h>
|
||||
|
||||
// can hold one PreparedStatement with Results
|
||||
class IDbConnection
|
||||
{
|
||||
public:
|
||||
IDbConnection(const char *pPrefix)
|
||||
{
|
||||
str_copy(m_aPrefix, pPrefix, sizeof(m_aPrefix));
|
||||
}
|
||||
virtual ~IDbConnection() {}
|
||||
|
||||
// copies the credentials, not the active connection
|
||||
virtual IDbConnection *Copy() = 0;
|
||||
|
||||
// returns the database prefix
|
||||
const char *GetPrefix() { return m_aPrefix; }
|
||||
|
||||
enum Status
|
||||
{
|
||||
IN_USE,
|
||||
SUCCESS,
|
||||
ERROR,
|
||||
};
|
||||
// returns true if connection was established
|
||||
virtual Status Connect() = 0;
|
||||
virtual void Disconnect() = 0;
|
||||
|
||||
// get exclusive read/write access to the database
|
||||
virtual void Lock() = 0;
|
||||
virtual void Unlock() = 0;
|
||||
|
||||
// ? for Placeholders, connection has to be established, can overwrite previous prepared statements
|
||||
virtual void PrepareStatement(const char *pStmt) = 0;
|
||||
|
||||
// PrepareStatement has to be called beforehand,
|
||||
virtual void BindString(int Idx, const char *pString) = 0;
|
||||
virtual void BindInt(int Idx, int Value) = 0;
|
||||
|
||||
virtual void Execute() = 0;
|
||||
|
||||
// if true if another result row exists and selects it
|
||||
virtual bool Step() = 0;
|
||||
|
||||
virtual int GetInt(int Col) const = 0;
|
||||
// ensures that the string is null terminated
|
||||
virtual void GetString(int Col, char *pBuffer, int BufferSize) const = 0;
|
||||
// returns number of bytes read into the buffer
|
||||
virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize) const = 0;
|
||||
|
||||
protected:
|
||||
char m_aPrefix[64];
|
||||
};
|
||||
|
||||
#endif // ENGINE_SERVER_DATABASES_CONNECTION_H
|
88
src/engine/server/databases/mysql.cpp
Normal file
88
src/engine/server/databases/mysql.cpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
#include "mysql.h"
|
||||
|
||||
#include <cppconn/driver.h>
|
||||
|
||||
CMysqlConnection::CMysqlConnection(
|
||||
const char *pDatabase,
|
||||
const char *pPrefix,
|
||||
const char *pUser,
|
||||
const char *pPass,
|
||||
const char *pIp,
|
||||
int Port,
|
||||
bool Setup) :
|
||||
IDbConnection(pPrefix),
|
||||
m_pDriver(nullptr),
|
||||
m_Port(Port),
|
||||
m_Setup(Setup),
|
||||
m_InUse(false)
|
||||
{
|
||||
str_copy(m_aDatabase, pDatabase, sizeof(m_aDatabase));
|
||||
str_copy(m_aUser, pUser, sizeof(m_aUser));
|
||||
str_copy(m_aPass, pPass, sizeof(m_aPass));
|
||||
str_copy(m_aIp, pIp, sizeof(m_aIp));
|
||||
}
|
||||
|
||||
CMysqlConnection::~CMysqlConnection()
|
||||
{
|
||||
}
|
||||
|
||||
CMysqlConnection *CMysqlConnection::Copy()
|
||||
{
|
||||
return new CMysqlConnection(m_aDatabase, m_aPrefix, m_aUser, m_aPass, m_aIp, m_Port, m_Setup);
|
||||
}
|
||||
|
||||
IDbConnection::Status CMysqlConnection::Connect()
|
||||
{
|
||||
if(m_InUse.exchange(true))
|
||||
return Status::IN_USE;
|
||||
|
||||
return Status::ERROR;
|
||||
}
|
||||
|
||||
void CMysqlConnection::Disconnect()
|
||||
{
|
||||
m_InUse.store(false);
|
||||
}
|
||||
|
||||
void CMysqlConnection::Lock()
|
||||
{
|
||||
}
|
||||
|
||||
void CMysqlConnection::Unlock()
|
||||
{
|
||||
}
|
||||
|
||||
void CMysqlConnection::PrepareStatement(const char *pStmt)
|
||||
{
|
||||
}
|
||||
|
||||
void CMysqlConnection::BindString(int Idx, const char *pString)
|
||||
{
|
||||
}
|
||||
|
||||
void CMysqlConnection::BindInt(int Idx, int Value)
|
||||
{
|
||||
}
|
||||
|
||||
void CMysqlConnection::Execute()
|
||||
{
|
||||
}
|
||||
|
||||
bool CMysqlConnection::Step()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int CMysqlConnection::GetInt(int Col) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CMysqlConnection::GetString(int Col, char *pBuffer, int BufferSize) const
|
||||
{
|
||||
}
|
||||
|
||||
int CMysqlConnection::GetBlob(int Col, unsigned char *pBuffer, int BufferSize) const
|
||||
{
|
||||
return 0;
|
||||
}
|
67
src/engine/server/databases/mysql.h
Normal file
67
src/engine/server/databases/mysql.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
#ifndef ENGINE_SERVER_DATABASES_MYSQL_H
|
||||
#define ENGINE_SERVER_DATABASES_MYSQL_H
|
||||
|
||||
#include "connection.h"
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
#include <cppconn/exception.h>
|
||||
#include <cppconn/prepared_statement.h>
|
||||
#include <cppconn/statement.h>
|
||||
|
||||
namespace sql {
|
||||
class Driver;
|
||||
} /* namespace sql */
|
||||
|
||||
class CMysqlConnection : public IDbConnection
|
||||
{
|
||||
public:
|
||||
CMysqlConnection(
|
||||
const char *pDatabase,
|
||||
const char *pPrefix,
|
||||
const char *pUser,
|
||||
const char *pPass,
|
||||
const char *pIp,
|
||||
int Port,
|
||||
bool Setup);
|
||||
virtual ~CMysqlConnection();
|
||||
|
||||
virtual CMysqlConnection *Copy();
|
||||
|
||||
virtual Status Connect();
|
||||
virtual void Disconnect();
|
||||
|
||||
virtual void Lock();
|
||||
virtual void Unlock();
|
||||
|
||||
virtual void PrepareStatement(const char *pStmt);
|
||||
|
||||
virtual void BindString(int Idx, const char *pString);
|
||||
virtual void BindInt(int Idx, int Value);
|
||||
|
||||
virtual void Execute();
|
||||
|
||||
virtual bool Step();
|
||||
|
||||
virtual int GetInt(int Col) const;
|
||||
virtual void GetString(int Col, char *pBuffer, int BufferSize) const;
|
||||
virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize) const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<sql::Connection> m_pConnection;
|
||||
sql::Driver *m_pDriver;
|
||||
std::unique_ptr<sql::PreparedStatement> m_pPreparedStmt;
|
||||
std::unique_ptr<sql::ResultSet> m_pResults;
|
||||
|
||||
// copy of config vars
|
||||
char m_aDatabase[64];
|
||||
char m_aUser[64];
|
||||
char m_aPass[64];
|
||||
char m_aIp[64];
|
||||
int m_Port;
|
||||
bool m_Setup;
|
||||
|
||||
std::atomic_bool m_InUse;
|
||||
};
|
||||
|
||||
#endif // ENGINE_SERVER_DATABASES_MYSQL_H
|
Loading…
Reference in a new issue