TrinityCore
Loading...
Searching...
No Matches
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 "Define.h"
22#include "DatabaseEnvFwd.h"
23#include <map>
24#include <memory>
25#include <mutex>
26#include <string>
27#include <vector>
28
29template <typename T>
31
32class DatabaseWorker;
34class SQLOperation;
35
42
44{
45 explicit MySQLConnectionInfo(std::string const& infoString);
46
47 std::string user;
48 std::string password;
49 std::string database;
50 std::string host;
51 std::string port_or_socket;
52 std::string ssl;
53};
54
56{
57 template <class T> friend class DatabaseWorkerPool;
58 friend class PingOperation;
59
60 public:
63 virtual ~MySQLConnection();
64
65 virtual uint32 Open();
66 void Close();
67
68 bool PrepareStatements();
69
70 bool Execute(char const* sql);
71 bool Execute(PreparedStatementBase* stmt);
72 ResultSet* Query(char const* sql);
74 bool _Query(char const* sql, MySQLResult** pResult, MySQLField** pFields, uint64* pRowCount, uint32* pFieldCount);
75 bool _Query(PreparedStatementBase* stmt, MySQLPreparedStatement** mysqlStmt, MySQLResult** pResult, uint64* pRowCount, uint32* pFieldCount);
76
77 void BeginTransaction();
78 void RollbackTransaction();
79 void CommitTransaction();
80 int ExecuteTransaction(std::shared_ptr<TransactionBase> transaction);
81 size_t EscapeString(char* to, const char* from, size_t length);
82 void Ping();
83
84 uint32 GetLastError();
85
86 protected:
89 bool LockIfReady();
90
92 void Unlock();
93
94 uint32 GetServerVersion() const;
95 MySQLPreparedStatement* GetPreparedStatement(uint32 index);
96 void PrepareStatement(uint32 index, std::string const& sql, ConnectionFlags flags);
97
98 virtual void DoPrepareStatements() = 0;
99
100 typedef std::vector<std::unique_ptr<MySQLPreparedStatement>> PreparedStatementContainer;
101
105
106 private:
107 bool _HandleMySQLErrno(uint32 errNo, uint8 attempts = 5);
108
110 std::unique_ptr<DatabaseWorker> m_worker;
114 std::mutex m_Mutex;
115
116 MySQLConnection(MySQLConnection const& right) = delete;
118};
119
120#endif
uint8_t uint8
Definition Define.h:135
#define TC_DATABASE_API
Definition Define.h:102
uint64_t uint64
Definition Define.h:132
uint32_t uint32
Definition Define.h:133
uint16 flags
ConnectionFlags
@ CONNECTION_SYNCH
@ CONNECTION_BOTH
@ CONNECTION_ASYNC
std::vector< std::unique_ptr< MySQLPreparedStatement > > PreparedStatementContainer
ProducerConsumerQueue< SQLOperation * > * m_queue
std::unique_ptr< DatabaseWorker > m_worker
Queue shared with other asynchronous connections.
MySQLConnection & operator=(MySQLConnection const &right)=delete
MySQLConnectionInfo & m_connectionInfo
MySQL Handle.
PreparedStatementContainer m_stmts
ConnectionFlags m_connectionFlags
Connection info (used for logging)
std::mutex m_Mutex
Connection flags (for preparing relevant statements)
bool m_prepareError
Are we reconnecting?
MySQLHandle * m_Mysql
Core worker task.
virtual void DoPrepareStatements()=0
MySQLConnection(MySQLConnection const &right)=delete
bool m_reconnecting
PreparedStatements storage.
std::string port_or_socket