Skip to content

Commit

Permalink
SQLite: Fix incompatible sql statements
Browse files Browse the repository at this point in the history
  • Loading branch information
insunaa committed Jan 6, 2024
1 parent ab9ce01 commit 3e48e60
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/game/BattleGround/BattleGround.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/game/Chat/Level3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 : "";
Expand Down Expand Up @@ -5976,7 +5976,7 @@ bool ChatHandler::HandleBanListHelper(std::unique_ptr<QueryResult> 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 : "";
Expand All @@ -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());
}

Expand Down
3 changes: 3 additions & 0 deletions src/shared/Database/DatabaseEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Database/DatabaseSqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "QueryResultSqlite.h"
#include <cstdint>
#include <cstring>
#include <sqlite3.h>
#ifdef DO_SQLITE
#include <sqlite3.h>

#include "Util/Util.h"
#include "Policies/Singleton.h"
Expand Down

0 comments on commit 3e48e60

Please sign in to comment.