Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
MySQLConnection.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
7#include "DatabaseQueue.h"
9#include "Transaction.h"
10#include "Util.h"
11
12#ifndef _MYSQLCONNECTION_H
13#define _MYSQLCONNECTION_H
14
15class DatabaseWorker;
18class PingOperation;
19
20enum ConnectionFlags
21{
22 CONNECTION_ASYNC = 0x1,
23 CONNECTION_SYNCH = 0x2,
24 CONNECTION_BOTH = CONNECTION_ASYNC | CONNECTION_SYNCH
25};
26
27struct MySQLConnectionInfo
28{
29 explicit MySQLConnectionInfo(const char* host, const char* port, const char* user, const char* password, const char* database)
30 {
31 _host.assign(host);
32 _port_or_socket.assign(port);
33 _user.assign(user);
34 _password.assign(password);
35 _database.assign(database);
36 }
37
38 explicit MySQLConnectionInfo(std::string const& infoString)
39 {
40 Tokenizer tokens(infoString, ';');
41
42 if (tokens.size() != 5)
43 return;
44
45 uint8 i = 0;
46
47 _host.assign(tokens[i++]);
48 _port_or_socket.assign(tokens[i++]);
49 _user.assign(tokens[i++]);
50 _password.assign(tokens[i++]);
51 _database.assign(tokens[i++]);
52 }
53
54 std::string _user;
55 std::string _password;
56 std::string _database;
57 std::string _host;
58 std::string _port_or_socket;
59};
60
61typedef std::map<uint32 /*index*/, std::pair<std::string /*query*/, ConnectionFlags /*sync/async*/> > PreparedStatementMap;
62
63class MySQLConnection
64{
65 template <class T> friend class DatabaseWorkerPool;
66 friend class PingOperation;
67
68public:
69 MySQLConnection(MySQLConnectionInfo& connInfo);
70 MySQLConnection(Skyfire::DatabaseQueue* queue, MySQLConnectionInfo& connInfo);
71 virtual ~MySQLConnection();
72
73 virtual bool Open();
74 void Close();
75
76public:
77 bool Execute(const char* sql);
78 bool Execute(PreparedStatement* stmt);
79 ResultSet* Query(const char* sql);
80 PreparedResultSet* Query(PreparedStatement* stmt);
81 bool _Query(const char* sql, MYSQL_RES** pResult, MYSQL_FIELD** pFields, uint64* pRowCount, uint32* pFieldCount);
82 bool _Query(PreparedStatement* stmt, MYSQL_RES** pResult, uint64* pRowCount, uint32* pFieldCount);
83
84 void BeginTransaction();
85 void RollbackTransaction();
86 void CommitTransaction();
87 bool ExecuteTransaction(SQLTransaction& transaction);
88
89 operator bool() const { return m_Mysql != NULL; }
90 void Ping() { mysql_ping(m_Mysql); }
91
92 uint32 GetLastError() { return mysql_errno(m_Mysql); }
93
94protected:
95 bool LockIfReady()
96 {
99 return m_Mutex.try_lock();
100 }
101
102 void Unlock()
103 {
105 m_Mutex.unlock();
106 }
107
108 MYSQL* GetHandle() { return m_Mysql; }
109 MySQLPreparedStatement* GetPreparedStatement(uint32 index);
110 void PrepareStatement(uint32 index, std::string sql, ConnectionFlags flags);
111
112 bool PrepareStatements();
113 virtual void DoPrepareStatements() = 0;
114
115protected:
116 std::vector<MySQLPreparedStatement*> m_stmts;
117 PreparedStatementMap m_queries;
118 bool m_reconnecting;
119 bool m_prepareError;
120
121private:
122 bool _HandleMySQLErrno(uint32 errNo);
123
124private:
125 Skyfire::DatabaseQueue* m_queue;
126 DatabaseWorker* m_worker;
127 MYSQL* m_Mysql;
128 MySQLConnectionInfo& m_connectionInfo;
129 ConnectionFlags m_connectionFlags;
130 Skyfire::Mutex m_Mutex;
131
132 MySQLConnection(MySQLConnection const& right) = delete;
133 MySQLConnection& operator=(MySQLConnection const& right) = delete;
134};
135
136#endif
std::uint8_t uint8
Definition Define.h:79
std::uint32_t uint32
Definition Define.h:77
std::uint64_t uint64
Definition Define.h:76
Skyfire::AutoPtr< Transaction, Skyfire::Mutex > SQLTransaction
Definition Transaction.h:42