TrinityCore
MySQLConnection.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _MYSQLCONNECTION_H
19#define _MYSQLCONNECTION_H
20
21#include "AsioHacksFwd.h"
22#include "Define.h"
23#include "DatabaseEnvFwd.h"
24#include <memory>
25#include <mutex>
26#include <string>
27#include <thread>
28#include <vector>
29
31
33{
37};
38
40{
41 explicit MySQLConnectionInfo(std::string const& infoString);
42
43 std::string user;
44 std::string password;
45 std::string database;
46 std::string host;
47 std::string port_or_socket;
48 std::string ssl;
49};
50
52{
53 template <class T> friend class DatabaseWorkerPool;
54 friend class PingOperation;
55
56 public:
57 MySQLConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags);
58 virtual ~MySQLConnection();
59
60 uint32 Open();
61 void Close();
62
63 bool PrepareStatements();
64
65 bool Execute(char const* sql);
67 ResultSet* Query(char const* sql);
69 bool _Query(char const* sql, MySQLResult** pResult, MySQLField** pFields, uint64* pRowCount, uint32* pFieldCount);
70 bool _Query(PreparedStatementBase* stmt, MySQLPreparedStatement** mysqlStmt, MySQLResult** pResult, uint64* pRowCount, uint32* pFieldCount);
71
72 void BeginTransaction();
73 void RollbackTransaction();
74 void CommitTransaction();
75 int ExecuteTransaction(std::shared_ptr<TransactionBase> transaction);
76 size_t EscapeString(char* to, const char* from, size_t length);
77 void Ping();
78
79 uint32 GetLastError();
80
81 void StartWorkerThread(Trinity::Asio::IoContext* context);
82 std::thread::id GetWorkerThreadId() const;
83
84 protected:
87 bool LockIfReady();
88
90 void Unlock();
91
92 uint32 GetServerVersion() const;
93 MySQLPreparedStatement* GetPreparedStatement(uint32 index);
94 void PrepareStatement(uint32 index, std::string_view sql, ConnectionFlags flags);
95
96 virtual void DoPrepareStatements() = 0;
97
98 typedef std::vector<std::unique_ptr<MySQLPreparedStatement>> PreparedStatementContainer;
99
103
104 private:
105 bool _HandleMySQLErrno(uint32 errNo, uint8 attempts = 5);
106
107 struct WorkerThread;
108 std::unique_ptr<WorkerThread> m_workerThread;
112 std::mutex m_Mutex;
113
114 MySQLConnection(MySQLConnection const& right) = delete;
116};
117
118#endif
uint8_t uint8
Definition: Define.h:144
#define TC_DATABASE_API
Definition: Define.h:111
uint64_t uint64
Definition: Define.h:141
uint32_t uint32
Definition: Define.h:142
uint16 flags
Definition: DisableMgr.cpp:49
ConnectionFlags
@ CONNECTION_SYNCH
@ CONNECTION_BOTH
@ CONNECTION_ASYNC
std::unique_ptr< WorkerThread > m_workerThread
Core worker thread.
std::vector< std::unique_ptr< MySQLPreparedStatement > > PreparedStatementContainer
MySQLConnection & operator=(MySQLConnection const &right)=delete
MySQLConnectionInfo & m_connectionInfo
Connection info (used for logging)
PreparedStatementContainer m_stmts
PreparedStatements storage.
ConnectionFlags m_connectionFlags
Connection flags (for preparing relevant statements)
std::mutex m_Mutex
bool m_prepareError
Was there any error while preparing statements?
MySQLHandle * m_Mysql
MySQL Handle.
virtual void DoPrepareStatements()=0
MySQLConnection(MySQLConnection const &right)=delete
bool m_reconnecting
Are we reconnecting?
void Execute(Creature *me, EventMap &events, uint32 eventId)
std::string port_or_socket