DDNet documentation
Loading...
Searching...
No Matches
connection.h
Go to the documentation of this file.
1#ifndef ENGINE_SERVER_DATABASES_CONNECTION_H
2#define ENGINE_SERVER_DATABASES_CONNECTION_H
3
4#include "connection_pool.h"
5
7
8#include <memory>
9#include <optional>
10
11enum
12{
13 // MAX_NAME_LENGTH includes the size with \0, which is not necessary in SQL
15};
16
17class IConsole;
18
19// can hold one PreparedStatement with Results
21{
22public:
23 IDbConnection(const char *pPrefix);
24 virtual ~IDbConnection() = default;
26 virtual void Print(IConsole *pConsole, const char *pMode) = 0;
27
28 // returns the database prefix
29 const char *GetPrefix() const { return m_aPrefix; }
30 virtual const char *BinaryCollate() const = 0;
31 // can be inserted into queries to convert a timestamp variable to the unix timestamp
32 virtual void ToUnixTimestamp(const char *pTimestamp, char *aBuf, unsigned int BufferSize) = 0;
33 // since MySQL automatically converts timestamps to utc, meanwhile sqlite code has to
34 // explicitly convert before inserting timestamps, NOTE: CURRENT_TIMESTAMP in SQLite is UTC by
35 // default and doesn't have to be converted
36 virtual const char *InsertTimestampAsUtc() const = 0;
37 // can be used in the context of `LIKE Map`, adds `? COLLATE`
38 virtual const char *CollateNocase() const = 0;
39 // syntax to insert a row into table or ignore if it already exists
40 virtual const char *InsertIgnore() const = 0;
41 // ORDER BY RANDOM()/RAND()
42 virtual const char *Random() const = 0;
43 // Get Median Map Time from l.Map
44 virtual const char *MedianMapTime(char *pBuffer, int BufferSize) const = 0;
45 virtual const char *False() const = 0;
46 virtual const char *True() const = 0;
47
48 // tries to allocate the connection from the pool established
49 //
50 // returns true on success
51 virtual bool Connect(char *pError, int ErrorSize) = 0;
52 // has to be called to return the connection back to the pool
53 virtual void Disconnect() = 0;
54
55 // ? for Placeholders, connection has to be established, can overwrite previous prepared statements
56 //
57 // returns true on success
58 virtual bool PrepareStatement(const char *pStmt, char *pError, int ErrorSize) = 0;
59
60 // PrepareStatement has to be called beforehand,
61 virtual void BindString(int Idx, const char *pString) = 0;
62 virtual void BindBlob(int Idx, unsigned char *pBlob, int Size) = 0;
63 virtual void BindInt(int Idx, int Value) = 0;
64 virtual void BindInt64(int Idx, int64_t Value) = 0;
65 virtual void BindFloat(int Idx, float Value) = 0;
66 virtual void BindNull(int Idx) = 0;
67
68 // Print expanded sql statement
69 virtual void Print() = 0;
70
71 // executes the query and returns if a result row exists and selects it
72 // when called multiple times the next row is selected
73 //
74 // returns true on success
75 virtual bool Step(bool *pEnd, char *pError, int ErrorSize) = 0;
76 // executes the query and returns the number of rows affected by the update/insert/delete
77 //
78 // returns true on success
79 virtual bool ExecuteUpdate(int *pNumUpdated, char *pError, int ErrorSize) = 0;
80
81 virtual bool IsNull(int Col) = 0;
82 virtual float GetFloat(int Col) = 0;
83 virtual int GetInt(int Col) = 0;
84 virtual int64_t GetInt64(int Col) = 0;
85 // ensures that the string is null terminated
86 virtual void GetString(int Col, char *pBuffer, int BufferSize) = 0;
87 // returns number of bytes read into the buffer
88 virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize) = 0;
89 std::optional<float> GetOptionalFloat(int Col);
90 std::optional<int> GetOptionalInt(int Col);
91 std::optional<int64_t> GetOptionalInt64(int Col);
92
93 // SQL statements, that can't be abstracted, has side effects to the result
94 virtual bool AddPoints(const char *pPlayer, int Points, char *pError, int ErrorSize) = 0;
95
96private:
97 char m_aPrefix[64];
98
99protected:
100 void FormatCreateRace(char *aBuf, unsigned int BufferSize, bool Backup) const;
101 void FormatCreateTeamrace(char *aBuf, unsigned int BufferSize, const char *pIdType, bool Backup) const;
102 void FormatCreateMaps(char *aBuf, unsigned int BufferSize) const;
103 void FormatCreateSaves(char *aBuf, unsigned int BufferSize, bool Backup) const;
104 void FormatCreatePoints(char *aBuf, unsigned int BufferSize) const;
105};
106
107bool MysqlAvailable();
108int MysqlInit();
109void MysqlUninit();
110
111std::unique_ptr<IDbConnection> CreateSqliteConnection(const char *pFilename, bool Setup);
112// Returns nullptr if MySQL support is not compiled in.
113std::unique_ptr<IDbConnection> CreateMysqlConnection(CMysqlConfig Config);
114
115#endif // ENGINE_SERVER_DATABASES_CONNECTION_H
Definition console.h:20
virtual bool Step(bool *pEnd, char *pError, int ErrorSize)=0
virtual bool Connect(char *pError, int ErrorSize)=0
virtual void BindInt64(int Idx, int64_t Value)=0
virtual bool ExecuteUpdate(int *pNumUpdated, char *pError, int ErrorSize)=0
void FormatCreateMaps(char *aBuf, unsigned int BufferSize) const
Definition connection.cpp:74
virtual bool PrepareStatement(const char *pStmt, char *pError, int ErrorSize)=0
virtual int GetInt(int Col)=0
virtual void GetString(int Col, char *pBuffer, int BufferSize)=0
virtual ~IDbConnection()=default
virtual const char * Random() const =0
IDbConnection & operator=(const IDbConnection &)=delete
virtual bool AddPoints(const char *pPlayer, int Points, char *pError, int ErrorSize)=0
char m_aPrefix[64]
Definition connection.h:97
virtual void BindFloat(int Idx, float Value)=0
virtual void Print()=0
virtual void BindBlob(int Idx, unsigned char *pBlob, int Size)=0
virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize)=0
std::optional< int > GetOptionalInt(int Col)
Definition connection.cpp:17
void FormatCreateTeamrace(char *aBuf, unsigned int BufferSize, const char *pIdType, bool Backup) const
Definition connection.cpp:57
void FormatCreateRace(char *aBuf, unsigned int BufferSize, bool Backup) const
Definition connection.cpp:31
virtual const char * CollateNocase() const =0
virtual const char * MedianMapTime(char *pBuffer, int BufferSize) const =0
IDbConnection(const char *pPrefix)
Definition connection.cpp:5
virtual const char * InsertTimestampAsUtc() const =0
virtual const char * InsertIgnore() const =0
virtual const char * True() const =0
virtual float GetFloat(int Col)=0
std::optional< int64_t > GetOptionalInt64(int Col)
Definition connection.cpp:24
virtual void BindString(int Idx, const char *pString)=0
virtual void BindNull(int Idx)=0
virtual void Print(IConsole *pConsole, const char *pMode)=0
void FormatCreatePoints(char *aBuf, unsigned int BufferSize) const
Definition connection.cpp:106
virtual void BindInt(int Idx, int Value)=0
std::optional< float > GetOptionalFloat(int Col)
Definition connection.cpp:10
virtual const char * BinaryCollate() const =0
virtual int64_t GetInt64(int Col)=0
virtual void Disconnect()=0
void FormatCreateSaves(char *aBuf, unsigned int BufferSize, bool Backup) const
Definition connection.cpp:89
virtual void ToUnixTimestamp(const char *pTimestamp, char *aBuf, unsigned int BufferSize)=0
virtual const char * False() const =0
const char * GetPrefix() const
Definition connection.h:29
virtual bool IsNull(int Col)=0
int MysqlInit()
Definition mysql.cpp:39
std::unique_ptr< IDbConnection > CreateMysqlConnection(CMysqlConfig Config)
Definition mysql.cpp:682
bool MysqlAvailable()
Definition mysql.cpp:34
void MysqlUninit()
Definition mysql.cpp:54
@ MAX_NAME_LENGTH_SQL
Definition connection.h:14
std::unique_ptr< IDbConnection > CreateSqliteConnection(const char *pFilename, bool Setup)
Definition sqlite.cpp:419
@ MAX_NAME_LENGTH
Definition protocol.h:108
Definition connection_pool.h:50
Definition score_test.cpp:476