mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
Move format string to CREATE TABLE into own file
This commit is contained in:
parent
2c5f892205
commit
452017a58b
|
@ -1844,6 +1844,7 @@ set_src(ENGINE_SERVER GLOB_RECURSE src/engine/server
|
|||
antibot.h
|
||||
authmanager.cpp
|
||||
authmanager.h
|
||||
databases/connection.cpp
|
||||
databases/connection.h
|
||||
databases/connection_pool.cpp
|
||||
databases/connection_pool.h
|
||||
|
|
86
src/engine/server/databases/connection.cpp
Normal file
86
src/engine/server/databases/connection.cpp
Normal file
|
@ -0,0 +1,86 @@
|
|||
#include "connection.h"
|
||||
|
||||
#include <engine/shared/protocol.h>
|
||||
|
||||
void IDbConnection::FormatCreateRace(char *aBuf, unsigned int BufferSize)
|
||||
{
|
||||
str_format(aBuf, BufferSize,
|
||||
"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;",
|
||||
GetPrefix(), MAX_NAME_LENGTH);
|
||||
}
|
||||
|
||||
void IDbConnection::FormatCreateTeamrace(char *aBuf, unsigned int BufferSize)
|
||||
{
|
||||
str_format(aBuf, BufferSize,
|
||||
"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;",
|
||||
GetPrefix(), MAX_NAME_LENGTH);
|
||||
}
|
||||
|
||||
void IDbConnection::FormatCreateMaps(char *aBuf, unsigned int BufferSize)
|
||||
{
|
||||
str_format(aBuf, BufferSize,
|
||||
"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;",
|
||||
GetPrefix());
|
||||
}
|
||||
|
||||
void IDbConnection::FormatCreateSaves(char *aBuf, unsigned int BufferSize)
|
||||
{
|
||||
str_format(aBuf, BufferSize,
|
||||
"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;",
|
||||
GetPrefix());
|
||||
}
|
||||
|
||||
void IDbConnection::FormatCreatePoints(char *aBuf, unsigned int BufferSize)
|
||||
{
|
||||
str_format(aBuf, BufferSize,
|
||||
"CREATE TABLE IF NOT EXISTS %s_points ("
|
||||
"Name VARCHAR(%d) BINARY NOT NULL, "
|
||||
"Points INT DEFAULT 0, "
|
||||
"UNIQUE KEY Name (Name)"
|
||||
") CHARACTER SET utf8mb4;",
|
||||
GetPrefix(), MAX_NAME_LENGTH);
|
||||
}
|
|
@ -54,67 +54,15 @@ public:
|
|||
// returns number of bytes read into the buffer
|
||||
virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize) const = 0;
|
||||
|
||||
protected:
|
||||
private:
|
||||
char m_aPrefix[64];
|
||||
|
||||
static constexpr const char *m_pCreateRace =
|
||||
"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;";
|
||||
static constexpr const char *m_pCreateTeamrace =
|
||||
"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;";
|
||||
static constexpr const char *m_pCreateMaps =
|
||||
"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;";
|
||||
static constexpr const char *m_pCreateSaves =
|
||||
"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;";
|
||||
static constexpr const char *m_pCreatePoints =
|
||||
"CREATE TABLE IF NOT EXISTS %s_points ("
|
||||
"Name VARCHAR(%d) BINARY NOT NULL, "
|
||||
"Points INT DEFAULT 0, "
|
||||
"UNIQUE KEY Name (Name)"
|
||||
") CHARACTER SET utf8mb4;";
|
||||
protected:
|
||||
void FormatCreateRace(char *aBuf, unsigned int BufferSize);
|
||||
void FormatCreateTeamrace(char *aBuf, unsigned int BufferSize);
|
||||
void FormatCreateMaps(char *aBuf, unsigned int BufferSize);
|
||||
void FormatCreateSaves(char *aBuf, unsigned int BufferSize);
|
||||
void FormatCreatePoints(char *aBuf, unsigned int BufferSize);
|
||||
};
|
||||
|
||||
#endif // ENGINE_SERVER_DATABASES_CONNECTION_H
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
#if defined(CONF_SQL)
|
||||
#include <cppconn/driver.h>
|
||||
#include <engine/shared/protocol.h>
|
||||
#endif
|
||||
|
||||
lock CMysqlConnection::m_SqlDriverLock;
|
||||
|
@ -39,7 +38,7 @@ CMysqlConnection::~CMysqlConnection()
|
|||
|
||||
CMysqlConnection *CMysqlConnection::Copy()
|
||||
{
|
||||
return new CMysqlConnection(m_aDatabase, m_aPrefix, m_aUser, m_aPass, m_aIp, m_Port, m_Setup);
|
||||
return new CMysqlConnection(m_aDatabase, GetPrefix(), m_aUser, m_aPass, m_aIp, m_Port, m_Setup);
|
||||
}
|
||||
|
||||
IDbConnection::Status CMysqlConnection::Connect()
|
||||
|
@ -118,15 +117,15 @@ IDbConnection::Status CMysqlConnection::Connect()
|
|||
m_pStmt->execute(aBuf);
|
||||
// Connect to specific database
|
||||
m_pConnection->setSchema(m_aDatabase);
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateRace, GetPrefix(), MAX_NAME_LENGTH);
|
||||
FormatCreateRace(aBuf, sizeof(aBuf));
|
||||
m_pStmt->execute(aBuf);
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateTeamrace, GetPrefix(), MAX_NAME_LENGTH);
|
||||
FormatCreateTeamrace(aBuf, sizeof(aBuf));
|
||||
m_pStmt->execute(aBuf);
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateMaps, GetPrefix());
|
||||
FormatCreateMaps(aBuf, sizeof(aBuf));
|
||||
m_pStmt->execute(aBuf);
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateSaves, GetPrefix());
|
||||
FormatCreateSaves(aBuf, sizeof(aBuf));
|
||||
m_pStmt->execute(aBuf);
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreatePoints, GetPrefix(), MAX_NAME_LENGTH);
|
||||
FormatCreatePoints(aBuf, sizeof(aBuf));
|
||||
m_pStmt->execute(aBuf);
|
||||
m_Setup = false;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#include "sqlite.h"
|
||||
|
||||
#include <base/math.h>
|
||||
#include <engine/shared/protocol.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sqlite3.h>
|
||||
|
@ -51,19 +50,19 @@ IDbConnection::Status CSqliteConnection::Connect()
|
|||
if(m_Setup)
|
||||
{
|
||||
char aBuf[1024];
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateRace, GetPrefix(), MAX_NAME_LENGTH);
|
||||
FormatCreateRace(aBuf, sizeof(aBuf));
|
||||
if(!Execute(aBuf))
|
||||
return Status::ERROR;
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateTeamrace, GetPrefix(), MAX_NAME_LENGTH);
|
||||
FormatCreateTeamrace(aBuf, sizeof(aBuf));
|
||||
if(!Execute(aBuf))
|
||||
return Status::ERROR;
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateMaps, GetPrefix());
|
||||
FormatCreateMaps(aBuf, sizeof(aBuf));
|
||||
if(!Execute(aBuf))
|
||||
return Status::ERROR;
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreateSaves, GetPrefix());
|
||||
FormatCreateSaves(aBuf, sizeof(aBuf));
|
||||
if(!Execute(aBuf))
|
||||
return Status::ERROR;
|
||||
str_format(aBuf, sizeof(aBuf), m_pCreatePoints, GetPrefix(), MAX_NAME_LENGTH);
|
||||
FormatCreatePoints(aBuf, sizeof(aBuf));
|
||||
if(!Execute(aBuf))
|
||||
return Status::ERROR;
|
||||
m_Setup = false;
|
||||
|
|
Loading…
Reference in a new issue