From 3e48e60d32d80df38d3e034e95ca4cdc16d79ed5 Mon Sep 17 00:00:00 2001 From: insunaa Date: Sat, 6 Jan 2024 14:23:27 +0100 Subject: [PATCH] SQLite: Fix incompatible sql statements --- src/game/BattleGround/BattleGround.cpp | 2 +- src/game/Chat/Level3.cpp | 12 ++++++------ src/shared/Database/DatabaseEnv.h | 3 +++ src/shared/Database/DatabaseSqlite.cpp | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/game/BattleGround/BattleGround.cpp b/src/game/BattleGround/BattleGround.cpp index b8ce20c92ad..226ab0a6215 100644 --- a/src/game/BattleGround/BattleGround.cpp +++ b/src/game/BattleGround/BattleGround.cpp @@ -809,7 +809,7 @@ void BattleGround::EndBattleGround(Team winner) { static SqlStatementID insPvPstatsBattleground; - SqlStatement stmt = CharacterDatabase.CreateStatement(insPvPstatsBattleground, "INSERT INTO pvpstats_battlegrounds (id, winner_team, bracket_id, type, date) VALUES (?, ?, ?, ?, NOW())"); + SqlStatement stmt = CharacterDatabase.CreateStatement(insPvPstatsBattleground, "INSERT INTO pvpstats_battlegrounds (id, winner_team, bracket_id, type, date) VALUES (?, ?, ?, ?, " _NOW_ ")"); uint8 battleground_bracket = GetMinLevel() / 10; uint8 battleground_type = (uint8)GetTypeId(); diff --git a/src/game/Chat/Level3.cpp b/src/game/Chat/Level3.cpp index d223eaa80f2..2f4e5b18110 100644 --- a/src/game/Chat/Level3.cpp +++ b/src/game/Chat/Level3.cpp @@ -5778,7 +5778,7 @@ bool ChatHandler::HandleBanInfoCharacterCommand(char* args) bool ChatHandler::HandleBanInfoHelper(uint32 accountid, char const* accountname) { - auto queryResult = LoginDatabase.PQuery("SELECT FROM_UNIXTIME(banned_at),expires_at-banned_at,active,expires_at,reason,banned_by,unbanned_at,unbanned_by " + auto queryResult = LoginDatabase.PQuery("SELECT " _FROM_UNIXTIME_("banned_at") ",expires_at-banned_at,active,expires_at,reason,banned_by,unbanned_at,unbanned_by " "FROM account_banned WHERE account_id = '%u' ORDER BY banned_at ASC", accountid); if (!queryResult) { @@ -5829,7 +5829,7 @@ bool ChatHandler::HandleBanInfoIPCommand(char* args) std::string IP = cIP; LoginDatabase.escape_string(IP); - auto queryResult = LoginDatabase.PQuery("SELECT ip, FROM_UNIXTIME(banned_at), FROM_UNIXTIME(expires_at), expires_at-UNIX_TIMESTAMP(), reason,banned_by,expires_at-banned_at" + auto queryResult = LoginDatabase.PQuery("SELECT ip, " _FROM_UNIXTIME_("banned_at")", " _FROM_UNIXTIME_("expires_at")", expires_at-" _UNIXTIME_ ", reason,banned_by,expires_at-banned_at" "FROM ip_banned WHERE ip = '%s'", IP.c_str()); if (!queryResult) { @@ -5867,7 +5867,7 @@ bool ChatHandler::HandleBanListCharacterCommand(char* args) bool ChatHandler::HandleBanListAccountCommand(char* args) { - LoginDatabase.Execute("DELETE FROM ip_banned WHERE expires_at<=UNIX_TIMESTAMP() AND expires_at<>banned_at"); + LoginDatabase.Execute("DELETE FROM ip_banned WHERE expires_at<=" _UNIXTIME_ " AND expires_at<>banned_at"); char* cFilter = ExtractLiteralArg(&args); std::string filter = cFilter ? cFilter : ""; @@ -5976,7 +5976,7 @@ bool ChatHandler::HandleBanListHelper(std::unique_ptr queryResult) bool ChatHandler::HandleBanListIPCommand(char* args) { - LoginDatabase.Execute("DELETE FROM ip_banned WHERE expires_at<=UNIX_TIMESTAMP() AND expires_at<>banned_at"); + LoginDatabase.Execute("DELETE FROM ip_banned WHERE expires_at<=" _UNIXTIME_ " AND expires_at<>banned_at"); char* cFilter = ExtractLiteralArg(&args); std::string filter = cFilter ? cFilter : ""; @@ -5987,13 +5987,13 @@ bool ChatHandler::HandleBanListIPCommand(char* args) if (filter.empty()) { queryResult = LoginDatabase.Query("SELECT ip,banned_at,expires_at,banned_by,reason FROM ip_banned" - " WHERE (banned_at=expires_at OR expires_at>UNIX_TIMESTAMP())" + " WHERE (banned_at=expires_at OR expires_at>" _UNIXTIME_ ")" " ORDER BY expires_at"); } else { queryResult = LoginDatabase.PQuery("SELECT ip,banned_at,expires_at,banned_by,reason FROM ip_banned" - " WHERE (banned_at=expires_at OR expires_at>UNIX_TIMESTAMP()) AND ip " _LIKE_ " " _CONCAT3_("'%%'", "'%s'", "'%%'") + " WHERE (banned_at=expires_at OR expires_at>" _UNIXTIME_ ") AND ip " _LIKE_ " " _CONCAT3_("'%%'", "'%s'", "'%%'") " ORDER BY expires_at", filter.c_str()); } diff --git a/src/shared/Database/DatabaseEnv.h b/src/shared/Database/DatabaseEnv.h index a21b4d03564..5cfd5cabeb8 100644 --- a/src/shared/Database/DatabaseEnv.h +++ b/src/shared/Database/DatabaseEnv.h @@ -39,6 +39,7 @@ typedef DatabasePostgre DatabaseType; #define _NOW_ "NOW()" #define _UNIXTIME_ "UNIX_TIMESTAMP()" #define _UNIXNOW_ "UNIX_TIMESTAMP(NOW())" +#define _FROM_UNIXTIME_(X) "TO_TIMESTAMP(" X ")" #elif DO_SQLITE #include "Database/QueryResultSqlite.h" #include "Database/Database.h" @@ -52,6 +53,7 @@ typedef DatabaseSqlite DatabaseType; #define _NOW_ "datetime()" #define _UNIXTIME_ "unixepoch()" #define _UNIXNOW_ "unixepoch('now')" +#define _FROM_UNIXTIME_(X) "datetime(" X ",'unixepoch')" #else #include "Database/QueryResultMysql.h" #include "Database/Database.h" @@ -65,6 +67,7 @@ typedef DatabaseMysql DatabaseType; #define _NOW_ "NOW()" #define _UNIXTIME_ "UNIX_TIMESTAMP()" #define _UNIXNOW_ "UNIX_TIMESTAMP(NOW())" +#define _FROM_UNIXTIME_(X) "FROM_UNIXTIME(" X ")" #endif extern DatabaseType WorldDatabase; diff --git a/src/shared/Database/DatabaseSqlite.cpp b/src/shared/Database/DatabaseSqlite.cpp index a0e05a2edee..8943876fe3e 100644 --- a/src/shared/Database/DatabaseSqlite.cpp +++ b/src/shared/Database/DatabaseSqlite.cpp @@ -19,8 +19,8 @@ #include "QueryResultSqlite.h" #include #include -#include #ifdef DO_SQLITE +#include #include "Util/Util.h" #include "Policies/Singleton.h"