From 00da45191c15d2488f6446aa71881466248adc57 Mon Sep 17 00:00:00 2001 From: Zwelf Date: Thu, 9 Jul 2020 19:18:18 +0200 Subject: [PATCH] Make CREATE TABLE compatible with SQLite --- src/engine/server/databases/connection.cpp | 58 +++++++++++----------- src/engine/server/databases/connection.h | 3 +- src/engine/server/databases/mysql.cpp | 2 +- src/engine/server/databases/mysql.h | 2 + src/engine/server/databases/sqlite.cpp | 2 +- src/engine/server/databases/sqlite.h | 2 + 6 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/engine/server/databases/connection.cpp b/src/engine/server/databases/connection.cpp index 80eb923a2..c8b32c5d2 100644 --- a/src/engine/server/databases/connection.cpp +++ b/src/engine/server/databases/connection.cpp @@ -6,8 +6,8 @@ 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, " + "Map VARCHAR(128) COLLATE %s NOT NULL, " + "Name VARCHAR(%d) COLLATE %s NOT NULL, " "Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " "Time FLOAT DEFAULT 0, " "Server CHAR(4), " @@ -21,66 +21,64 @@ void IDbConnection::FormatCreateRace(char *aBuf, unsigned int BufferSize) "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); + "DDNet7 BOOL DEFAULT FALSE" + ");", + GetPrefix(), BinaryCollate(), MAX_NAME_LENGTH, BinaryCollate()); } -void IDbConnection::FormatCreateTeamrace(char *aBuf, unsigned int BufferSize) +void IDbConnection::FormatCreateTeamrace(char *aBuf, unsigned int BufferSize, const char *pIdType) { str_format(aBuf, BufferSize, "CREATE TABLE IF NOT EXISTS %s_teamrace (" - "Map VARCHAR(128) BINARY NOT NULL, " - "Name VARCHAR(%d) BINARY NOT NULL, " + "Map VARCHAR(128) COLLATE %s NOT NULL, " + "Name VARCHAR(%d) COLLATE %s NOT NULL, " "Timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " "Time FLOAT DEFAULT 0, " - "ID VARBINARY(16) NOT NULL, " + "ID %s NOT NULL, " // VARBINARY(16) for MySQL and BLOB for SQLite "GameID VARCHAR(64), " - "DDNet7 BOOL DEFAULT FALSE, " - "KEY Map (Map)" - ") CHARACTER SET utf8mb4;", - GetPrefix(), MAX_NAME_LENGTH); + "DDNet7 BOOL DEFAULT FALSE" + ");", + GetPrefix(), BinaryCollate(), MAX_NAME_LENGTH, BinaryCollate(), pIdType); } 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, " + "Map VARCHAR(128) COLLATE %s NOT NULL, " + "Server VARCHAR(32) COLLATE %s NOT NULL, " + "Mapper VARCHAR(128) COLLATE %s NOT NULL, " "Points INT DEFAULT 0, " "Stars INT DEFAULT 0, " "Timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " - "UNIQUE KEY Map (Map)" - ") CHARACTER SET utf8mb4;", - GetPrefix()); + "PRIMARY KEY (Map)" + ");", + GetPrefix(), BinaryCollate(), BinaryCollate(), BinaryCollate()); } 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, " + "Savegame TEXT COLLATE %s NOT NULL, " + "Map VARCHAR(128) COLLATE %s NOT NULL, " + "Code VARCHAR(128) COLLATE %s 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()); + "PRIMARY KEY (Map, Code)" + ");", + GetPrefix(), BinaryCollate(), BinaryCollate(), BinaryCollate()); } 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, " + "Name VARCHAR(%d) COLLATE %s NOT NULL, " "Points INT DEFAULT 0, " - "UNIQUE KEY Name (Name)" - ") CHARACTER SET utf8mb4;", - GetPrefix(), MAX_NAME_LENGTH); + "PRIMARY KEY (Name)" + ");", + GetPrefix(), MAX_NAME_LENGTH, BinaryCollate()); } diff --git a/src/engine/server/databases/connection.h b/src/engine/server/databases/connection.h index c58d03c95..9b14e7bc9 100644 --- a/src/engine/server/databases/connection.h +++ b/src/engine/server/databases/connection.h @@ -19,6 +19,7 @@ public: // returns the database prefix const char *GetPrefix() { return m_aPrefix; } + virtual const char *BinaryCollate() const = 0; enum Status { @@ -59,7 +60,7 @@ private: protected: void FormatCreateRace(char *aBuf, unsigned int BufferSize); - void FormatCreateTeamrace(char *aBuf, unsigned int BufferSize); + void FormatCreateTeamrace(char *aBuf, unsigned int BufferSize, const char *pIdType); void FormatCreateMaps(char *aBuf, unsigned int BufferSize); void FormatCreateSaves(char *aBuf, unsigned int BufferSize); void FormatCreatePoints(char *aBuf, unsigned int BufferSize); diff --git a/src/engine/server/databases/mysql.cpp b/src/engine/server/databases/mysql.cpp index b86530e5f..991394078 100644 --- a/src/engine/server/databases/mysql.cpp +++ b/src/engine/server/databases/mysql.cpp @@ -119,7 +119,7 @@ IDbConnection::Status CMysqlConnection::Connect() m_pConnection->setSchema(m_aDatabase); FormatCreateRace(aBuf, sizeof(aBuf)); m_pStmt->execute(aBuf); - FormatCreateTeamrace(aBuf, sizeof(aBuf)); + FormatCreateTeamrace(aBuf, sizeof(aBuf), "VARBINARY(16)"); m_pStmt->execute(aBuf); FormatCreateMaps(aBuf, sizeof(aBuf)); m_pStmt->execute(aBuf); diff --git a/src/engine/server/databases/mysql.h b/src/engine/server/databases/mysql.h index 5cd5cb2a2..a9fd55fb3 100644 --- a/src/engine/server/databases/mysql.h +++ b/src/engine/server/databases/mysql.h @@ -28,6 +28,8 @@ public: virtual CMysqlConnection *Copy(); + virtual const char *BinaryCollate() const { return "utf8mb4_bin"; } + virtual Status Connect(); virtual void Disconnect(); diff --git a/src/engine/server/databases/sqlite.cpp b/src/engine/server/databases/sqlite.cpp index 007bb60d8..f3cd29d4d 100644 --- a/src/engine/server/databases/sqlite.cpp +++ b/src/engine/server/databases/sqlite.cpp @@ -53,7 +53,7 @@ IDbConnection::Status CSqliteConnection::Connect() FormatCreateRace(aBuf, sizeof(aBuf)); if(!Execute(aBuf)) return Status::ERROR; - FormatCreateTeamrace(aBuf, sizeof(aBuf)); + FormatCreateTeamrace(aBuf, sizeof(aBuf), "BLOB"); if(!Execute(aBuf)) return Status::ERROR; FormatCreateMaps(aBuf, sizeof(aBuf)); diff --git a/src/engine/server/databases/sqlite.h b/src/engine/server/databases/sqlite.h index d080472ee..8e45c0ded 100644 --- a/src/engine/server/databases/sqlite.h +++ b/src/engine/server/databases/sqlite.h @@ -15,6 +15,8 @@ public: virtual CSqliteConnection *Copy(); + virtual const char *BinaryCollate() const { return "BINARY"; } + virtual Status Connect(); virtual void Disconnect();