From 2c1c391597f9334705198ca965424b5a791df6ba Mon Sep 17 00:00:00 2001 From: Machiavelli Date: Fri, 3 Sep 2010 00:44:27 +0200 Subject: [PATCH] Core/DBLayer: - Example implementation of prepared statement in ObjectMgr::SaveCreatureRespawntime - #include "MYSQLThreading.h" -> #include "MySQLThreading.h" (*really* fixes Linux build) --HG-- branch : trunk --- src/server/game/Globals/ObjectMgr.cpp | 14 ++++++++++++-- .../Database/Implementation/WorldDatabase.cpp | 9 ++++++++- .../shared/Database/Implementation/WorldDatabase.h | 2 ++ src/server/shared/Database/MySQLConnection.cpp | 2 +- src/server/shared/Database/MySQLConnection.h | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/server/game/Globals/ObjectMgr.cpp b/src/server/game/Globals/ObjectMgr.cpp index 961472c60ec..eee64145ada 100644 --- a/src/server/game/Globals/ObjectMgr.cpp +++ b/src/server/game/Globals/ObjectMgr.cpp @@ -7221,9 +7221,19 @@ void ObjectMgr::SaveCreatureRespawnTime(uint32 loguid, uint32 instance, time_t t mCreatureRespawnTimes[MAKE_PAIR64(loguid,instance)] = t; } - WorldDatabase.PExecute("DELETE FROM creature_respawn WHERE guid = '%u' AND instance = '%u'", loguid, instance); + PreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_DEL_CRESPAWNTIME); + stmt->setUInt32(0, loguid); + stmt->setUInt32(1, instance); + WorldDatabase.Execute(stmt); + if (t) - WorldDatabase.PExecute("INSERT INTO creature_respawn VALUES ('%u', '" UI64FMTD "', '%u')", loguid, uint64(t), instance); + { + stmt = WorldDatabase.GetPreparedStatement(WORLD_ADD_CRESPAWNTIME); + stmt->setUInt32(0, loguid); + stmt->setUInt64(1, uint64(t)); + stmt->setUInt32(2, instance); + WorldDatabase.Execute(stmt); + } } void ObjectMgr::DeleteCreatureData(uint32 guid) diff --git a/src/server/shared/Database/Implementation/WorldDatabase.cpp b/src/server/shared/Database/Implementation/WorldDatabase.cpp index 26a270f565a..12383490e91 100644 --- a/src/server/shared/Database/Implementation/WorldDatabase.cpp +++ b/src/server/shared/Database/Implementation/WorldDatabase.cpp @@ -20,11 +20,18 @@ bool WorldDatabaseConnection::Open(const std::string& infoString) { + if (!MySQLConnection::Open(infoString)) + return false; + + m_stmts.resize(MAX_WORLDDATABASE_STATEMENTS); + /* ################################## LOAD YOUR PREPARED STATEMENTS HERE ################################## */ + PrepareStatement(WORLD_DEL_CRESPAWNTIME, "DELETE FROM creature_respawn WHERE guid = ? AND instance = ?"); + PrepareStatement(WORLD_ADD_CRESPAWNTIME, "INSERT INTO creature_respawn VALUES (?, ?, ?)"); - return MySQLConnection::Open(infoString); + return true; } diff --git a/src/server/shared/Database/Implementation/WorldDatabase.h b/src/server/shared/Database/Implementation/WorldDatabase.h index eb76791faaa..fb40869cc19 100644 --- a/src/server/shared/Database/Implementation/WorldDatabase.h +++ b/src/server/shared/Database/Implementation/WorldDatabase.h @@ -37,6 +37,8 @@ typedef DatabaseWorkerPool WorldDatabaseWorkerPool; enum WorldDatabaseStatements { + WORLD_DEL_CRESPAWNTIME, + WORLD_ADD_CRESPAWNTIME, MAX_WORLDDATABASE_STATEMENTS, }; diff --git a/src/server/shared/Database/MySQLConnection.cpp b/src/server/shared/Database/MySQLConnection.cpp index 4e5f26c0e8d..b2e52ea7eb7 100644 --- a/src/server/shared/Database/MySQLConnection.cpp +++ b/src/server/shared/Database/MySQLConnection.cpp @@ -25,7 +25,7 @@ #include #include "MySQLConnection.h" -#include "MYSQLThreading.h" +#include "MySQLThreading.h" #include "QueryResult.h" #include "SQLOperation.h" #include "PreparedStatement.h" diff --git a/src/server/shared/Database/MySQLConnection.h b/src/server/shared/Database/MySQLConnection.h index c0f792b1191..bbb9824943b 100644 --- a/src/server/shared/Database/MySQLConnection.h +++ b/src/server/shared/Database/MySQLConnection.h @@ -50,13 +50,13 @@ class MySQLConnection MYSQL* GetHandle() { return m_Mysql; } MySQLPreparedStatement* GetPreparedStatement(uint32 index); void PrepareStatement(uint32 index, const char* sql); + std::vector m_stmts; //! PreparedStatements storage private: ACE_Activation_Queue* m_queue; //! Queue shared with other asynchroneous connections. DatabaseWorker* m_worker; //! Core worker task. MYSQL * m_Mysql; //! MySQL Handle. ACE_Thread_Mutex m_Mutex; - std::vector m_stmts; //! PreparedStatements storage }; #endif \ No newline at end of file