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