12#ifndef _MYSQLCONNECTION_H
13#define _MYSQLCONNECTION_H
22 CONNECTION_ASYNC = 0x1,
23 CONNECTION_SYNCH = 0x2,
24 CONNECTION_BOTH = CONNECTION_ASYNC | CONNECTION_SYNCH
27struct MySQLConnectionInfo
29 explicit MySQLConnectionInfo(
const char* host,
const char* port,
const char* user,
const char* password,
const char* database)
32 _port_or_socket.assign(port);
34 _password.assign(password);
35 _database.assign(database);
38 explicit MySQLConnectionInfo(std::string
const& infoString)
40 Tokenizer tokens(infoString,
';');
42 if (tokens.size() != 5)
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++]);
55 std::string _password;
56 std::string _database;
58 std::string _port_or_socket;
61typedef std::map<
uint32 , std::pair<std::string , ConnectionFlags > > PreparedStatementMap;
65 template <
class T>
friend class DatabaseWorkerPool;
66 friend class PingOperation;
69 MySQLConnection(MySQLConnectionInfo& connInfo);
70 MySQLConnection(Skyfire::DatabaseQueue* queue, MySQLConnectionInfo& connInfo);
71 virtual ~MySQLConnection();
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);
84 void BeginTransaction();
85 void RollbackTransaction();
86 void CommitTransaction();
89 operator bool()
const {
return m_Mysql != NULL; }
90 void Ping() { mysql_ping(m_Mysql); }
92 uint32 GetLastError() {
return mysql_errno(m_Mysql); }
99 return m_Mutex.try_lock();
108 MYSQL* GetHandle() {
return m_Mysql; }
109 MySQLPreparedStatement* GetPreparedStatement(
uint32 index);
110 void PrepareStatement(
uint32 index, std::string sql, ConnectionFlags flags);
112 bool PrepareStatements();
113 virtual void DoPrepareStatements() = 0;
116 std::vector<MySQLPreparedStatement*> m_stmts;
117 PreparedStatementMap m_queries;
122 bool _HandleMySQLErrno(
uint32 errNo);
125 Skyfire::DatabaseQueue* m_queue;
126 DatabaseWorker* m_worker;
128 MySQLConnectionInfo& m_connectionInfo;
129 ConnectionFlags m_connectionFlags;
130 Skyfire::Mutex m_Mutex;
132 MySQLConnection(MySQLConnection
const& right) =
delete;
133 MySQLConnection& operator=(MySQLConnection
const& right) =
delete;
Skyfire::AutoPtr< Transaction, Skyfire::Mutex > SQLTransaction