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 <vector>
28
30
32{
36};
37
39{
40 explicit MySQLConnectionInfo(std::string const& infoString);
41
42 std::string user;
43 std::string password;
44 std::string database;
45 std::string host;
46 std::string port_or_socket;
47 std::string ssl;
48};
49
51{
52 template <class T> friend class DatabaseWorkerPool;
53 friend class PingOperation;
54
55 public:
56 MySQLConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags);
57 virtual ~MySQLConnection();
58
59 uint32 Open();
60 void Close();
61
62 bool PrepareStatements();
63
64 bool Execute(char const* sql);
65 bool Execute(PreparedStatementBase* stmt);
66 ResultSet* Query(char const* sql);
68 bool _Query(char const* sql, MySQLResult** pResult, MySQLField** pFields, uint64* pRowCount, uint32* pFieldCount);
69 bool _Query(PreparedStatementBase* stmt, MySQLPreparedStatement** mysqlStmt, MySQLResult** pResult, uint64* pRowCount, uint32* pFieldCount);
70
71 void BeginTransaction();
72 void RollbackTransaction();
73 void CommitTransaction();
74 int ExecuteTransaction(std::shared_ptr<TransactionBase> transaction);
75 size_t EscapeString(char* to, const char* from, size_t length);
76 void Ping();
77
78 uint32 GetLastError();
79
80 void StartWorkerThread(Trinity::Asio::IoContext* context);
81 std::thread::id GetWorkerThreadId() const;
82
83 protected:
86 bool LockIfReady();
87
89 void Unlock();
90
91 uint32 GetServerVersion() const;
92 MySQLPreparedStatement* GetPreparedStatement(uint32 index);
93 void PrepareStatement(uint32 index, std::string_view sql, ConnectionFlags flags);
94
95 virtual void DoPrepareStatements() = 0;
96
97 typedef std::vector<std::unique_ptr<MySQLPreparedStatement>> PreparedStatementContainer;
98
102
103 private:
104 bool _HandleMySQLErrno(uint32 errNo, uint8 attempts = 5);
105
106 std::unique_ptr<std::thread> m_workerThread;
110 std::mutex m_Mutex;
111
112 MySQLConnection(MySQLConnection const& right) = delete;
114};
115
116#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::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
std::unique_ptr< std::thread > m_workerThread
Core worker thread.
MySQLConnection(MySQLConnection const &right)=delete
bool m_reconnecting
Are we reconnecting?
std::string port_or_socket