TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MySQLConnection.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 #include "DatabaseWorkerPool.h"
19 #include "Transaction.h"
20 #include "Util.h"
21 #include "ProducerConsumerQueue.h"
22 
23 #ifndef _MYSQLCONNECTION_H
24 #define _MYSQLCONNECTION_H
25 
26 class DatabaseWorker;
27 class PreparedStatement;
29 class PingOperation;
30 
31 enum ConnectionFlags
32 {
33  CONNECTION_ASYNC = 0x1,
34  CONNECTION_SYNCH = 0x2,
35  CONNECTION_BOTH = CONNECTION_ASYNC | CONNECTION_SYNCH
36 };
37 
38 struct TC_DATABASE_API MySQLConnectionInfo
39 {
40  explicit MySQLConnectionInfo(std::string const& infoString)
41  {
42  Tokenizer tokens(infoString, ';');
43 
44  if (tokens.size() != 5)
45  return;
46 
47  uint8 i = 0;
48 
49  host.assign(tokens[i++]);
50  port_or_socket.assign(tokens[i++]);
51  user.assign(tokens[i++]);
52  password.assign(tokens[i++]);
53  database.assign(tokens[i++]);
54  }
55 
56  std::string user;
57  std::string password;
58  std::string database;
59  std::string host;
60  std::string port_or_socket;
61 };
62 
63 typedef std::map<uint32 /*index*/, std::pair<std::string /*query*/, ConnectionFlags /*sync/async*/> > PreparedStatementMap;
64 
65 class TC_DATABASE_API MySQLConnection
66 {
67  template <class T> friend class DatabaseWorkerPool;
68  friend class PingOperation;
69 
70  public:
71  MySQLConnection(MySQLConnectionInfo& connInfo);
72  MySQLConnection(ProducerConsumerQueue<SQLOperation*>* queue, MySQLConnectionInfo& connInfo);
73  virtual ~MySQLConnection();
74 
75  virtual uint32 Open();
76  void Close();
77 
78  bool PrepareStatements();
79 
80  public:
81  bool Execute(const char* sql);
82  bool Execute(PreparedStatement* stmt);
83  ResultSet* Query(const char* sql);
85  bool _Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount);
86  bool _Query(PreparedStatement* stmt, MYSQL_RES **pResult, uint64* pRowCount, uint32* pFieldCount);
87 
88  void BeginTransaction();
89  void RollbackTransaction();
90  void CommitTransaction();
91  int ExecuteTransaction(SQLTransaction& transaction);
92 
93  operator bool () const { return m_Mysql != NULL; }
94  void Ping() { mysql_ping(m_Mysql); }
95 
96  uint32 GetLastError() { return mysql_errno(m_Mysql); }
97 
98  protected:
99  bool LockIfReady()
100  {
103  return m_Mutex.try_lock();
104  }
105 
106  void Unlock()
107  {
109  m_Mutex.unlock();
110  }
111 
112  MYSQL* GetHandle() { return m_Mysql; }
113  MySQLPreparedStatement* GetPreparedStatement(uint32 index);
114  void PrepareStatement(uint32 index, const char* sql, ConnectionFlags flags);
115 
116  virtual void DoPrepareStatements() = 0;
117 
118  protected:
119  std::vector<std::unique_ptr<MySQLPreparedStatement>> m_stmts;
120  PreparedStatementMap m_queries;
121  bool m_reconnecting;
122  bool m_prepareError;
123 
124  private:
125  bool _HandleMySQLErrno(uint32 errNo, uint8 attempts = 5);
126 
127  private:
129  std::unique_ptr<DatabaseWorker> m_worker;
130  MYSQL* m_Mysql;
131  MySQLConnectionInfo& m_connectionInfo;
132  ConnectionFlags m_connectionFlags;
133  std::mutex m_Mutex;
134 
135  MySQLConnection(MySQLConnection const& right) = delete;
136  MySQLConnection& operator=(MySQLConnection const& right) = delete;
137 };
138 
139 #endif
#define TC_DATABASE_API
Definition: Define.h:122
Definition: Util.h:45
arena_t NULL
Definition: jemalloc_internal.h:624
Definition: PreparedStatement.h:74
Definition: QueryResult.h:30
#define bool
Definition: CascPort.h:16
Vector2int16 & operator=(const Any &a)
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
Definition: PreparedStatement.h:114
bool Execute() override
Operation for idle delaythreads.
Definition: DatabaseWorkerPool.h:40
Definition: DatabaseWorker.h:27
int GetLastError()
Definition: Common.cpp:70
Definition: DatabaseWorkerPool.h:37
uint8_t uint8
Definition: Define.h:152
uint8 flags
Definition: DisableMgr.cpp:44
Definition: QueryResult.h:63
Definition: DatabaseWorkerPool.h:48
std::shared_ptr< Transaction > SQLTransaction
Definition: Transaction.h:58