2020-07-04 08:13:21 +00:00
|
|
|
#ifndef ENGINE_SERVER_DATABASES_CONNECTION_H
|
|
|
|
#define ENGINE_SERVER_DATABASES_CONNECTION_H
|
|
|
|
|
|
|
|
#include <base/system.h>
|
|
|
|
|
2020-08-03 14:18:22 +00:00
|
|
|
class IConsole;
|
|
|
|
|
2020-07-04 08:13:21 +00:00
|
|
|
// can hold one PreparedStatement with Results
|
|
|
|
class IDbConnection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IDbConnection(const char *pPrefix)
|
|
|
|
{
|
|
|
|
str_copy(m_aPrefix, pPrefix, sizeof(m_aPrefix));
|
|
|
|
}
|
|
|
|
virtual ~IDbConnection() {}
|
2020-07-04 17:53:27 +00:00
|
|
|
IDbConnection& operator=(const IDbConnection&) = delete;
|
2020-08-03 14:18:22 +00:00
|
|
|
virtual void Print(IConsole *pConsole, const char *Mode) = 0;
|
2020-07-04 08:13:21 +00:00
|
|
|
|
|
|
|
// copies the credentials, not the active connection
|
|
|
|
virtual IDbConnection *Copy() = 0;
|
|
|
|
|
|
|
|
// returns the database prefix
|
|
|
|
const char *GetPrefix() { return m_aPrefix; }
|
2020-07-09 17:18:18 +00:00
|
|
|
virtual const char *BinaryCollate() const = 0;
|
2020-07-18 18:49:34 +00:00
|
|
|
// can be inserted into queries to convert a timestamp variable to the unix timestamp
|
|
|
|
virtual void ToUnixTimestamp(const char *pTimestamp, char *aBuf, unsigned int BufferSize) = 0;
|
|
|
|
// since MySQL automatically converts timestamps to utc, meanwhile sqlite code has to
|
|
|
|
// explicitly convert before inserting timestamps, NOTE: CURRENT_TIMESTAMP in SQLite is UTC by
|
|
|
|
// default and doesn't have to be converted
|
|
|
|
virtual const char *InsertTimestampAsUtc() const = 0;
|
2020-07-18 19:13:40 +00:00
|
|
|
// can be used in the context of `LIKE Map`, adds `? COLLATE`
|
|
|
|
virtual const char *CollateNocase() const = 0;
|
2020-07-04 08:13:21 +00:00
|
|
|
|
|
|
|
enum Status
|
|
|
|
{
|
|
|
|
IN_USE,
|
|
|
|
SUCCESS,
|
2020-08-03 11:11:52 +00:00
|
|
|
FAILURE,
|
2020-07-04 08:13:21 +00:00
|
|
|
};
|
2020-07-04 10:09:40 +00:00
|
|
|
// tries to allocate the connection from the pool established
|
2020-07-04 08:13:21 +00:00
|
|
|
virtual Status Connect() = 0;
|
2020-07-04 10:09:40 +00:00
|
|
|
// has to be called to return the connection back to the pool
|
2020-07-04 08:13:21 +00:00
|
|
|
virtual void Disconnect() = 0;
|
|
|
|
|
|
|
|
// get exclusive read/write access to the database
|
2020-07-04 17:53:27 +00:00
|
|
|
virtual void Lock(const char *pTable) = 0;
|
2020-07-04 08:13:21 +00:00
|
|
|
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;
|
2020-07-14 22:12:08 +00:00
|
|
|
virtual void BindBlob(int Idx, unsigned char *pBlob, int Size) = 0;
|
2020-07-04 08:13:21 +00:00
|
|
|
virtual void BindInt(int Idx, int Value) = 0;
|
2020-07-14 22:12:08 +00:00
|
|
|
virtual void BindFloat(int Idx, float Value) = 0;
|
2020-07-04 08:13:21 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
// executes the query and returns if a result row exists and selects it
|
|
|
|
// when called multiple times the next row is selected
|
2020-07-04 08:13:21 +00:00
|
|
|
virtual bool Step() = 0;
|
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
virtual bool IsNull(int Col) const = 0;
|
|
|
|
virtual float GetFloat(int Col) const = 0;
|
2020-07-04 08:13:21 +00:00
|
|
|
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;
|
2020-09-04 14:12:12 +00:00
|
|
|
// syntax to insert a row into table or ignore if it already exists
|
|
|
|
virtual const char *GetInsertIgnore() const = 0;
|
2020-07-04 08:13:21 +00:00
|
|
|
|
2020-07-12 12:07:12 +00:00
|
|
|
// SQL statements, that can't be abstracted, has side effects to the result
|
|
|
|
virtual void AddPoints(const char *pPlayer, int Points) = 0;
|
|
|
|
|
2020-07-09 17:02:28 +00:00
|
|
|
private:
|
2020-07-04 08:13:21 +00:00
|
|
|
char m_aPrefix[64];
|
2020-07-04 17:53:27 +00:00
|
|
|
|
2020-07-09 17:02:28 +00:00
|
|
|
protected:
|
|
|
|
void FormatCreateRace(char *aBuf, unsigned int BufferSize);
|
2020-07-09 17:18:18 +00:00
|
|
|
void FormatCreateTeamrace(char *aBuf, unsigned int BufferSize, const char *pIdType);
|
2020-07-09 17:02:28 +00:00
|
|
|
void FormatCreateMaps(char *aBuf, unsigned int BufferSize);
|
|
|
|
void FormatCreateSaves(char *aBuf, unsigned int BufferSize);
|
|
|
|
void FormatCreatePoints(char *aBuf, unsigned int BufferSize);
|
2020-07-04 08:13:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ENGINE_SERVER_DATABASES_CONNECTION_H
|