Otclient  14/8/2020
mysql.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef DATABASEMYSQL_H
24 #define DATABASEMYSQL_H
25 
26 #include "database.h"
27 
28 #include <framework/global.h>
29 
30 #ifdef WINDOWS
31  #include <winsock2.h>
32 #endif
33 
34 #include <mysql/mysql.h>
35 
36 class DatabaseMySQL : public Database
37 {
38 public:
39  DatabaseMySQL();
40  virtual ~DatabaseMySQL();
41 
42  virtual void connect(const std::string& host, const std::string& user, const std::string& pass,
43  const std::string& db, uint16 port, const std::string& unix_socket = "");
44 
45  virtual bool beginTransaction();
46  virtual bool rollback();
47  virtual bool commit();
48 
49  virtual bool executeQuery(const std::string& query);
50  virtual DBResultPtr storeQuery(const std::string& query);
51 
52  virtual std::string escapeString(const std::string &s);
53  virtual std::string escapeBlob(const char* s, uint32 length);
54 
55  virtual uint64 getLastInsertedRowID();
56  virtual Fw::DatabaseEngine getDatabaseEngine() {return Fw::DatabaseMySQL;}
57 
58 protected:
59  bool handleError();
60  bool internalExecuteQuery(const std::string &query);
61 
62  MYSQL* m_handle;
63 };
64 
65 class MySQLResult : public DBResult
66 {
67 
68 friend class DatabaseMySQL;
69 
70 public:
71  MySQLResult(MYSQL_RES* result);
72  virtual ~MySQLResult();
73 
74  virtual int32 getDataInt(const std::string& s);
75  virtual int64 getDataLong(const std::string& s);
76  virtual std::string getDataString(const std::string& s);
77  virtual const char* getDataStream(const std::string& s, uint64& size);
78 
79  virtual void free();
80  virtual bool next();
81  virtual int getRowCount() { return mysql_num_rows(m_result); }
82 
83 protected:
84  typedef std::map<const std::string, uint32> RowNames_t;
86 
87  MYSQL_RES* m_result;
88  MYSQL_ROW m_row;
89 };
90 
91 #endif
MySQLResult::getDataString
virtual std::string getDataString(const std::string &s)
Definition: mysql.cpp:218
DatabaseMySQL::getDatabaseEngine
virtual Fw::DatabaseEngine getDatabaseEngine()
Definition: mysql.h:56
DatabaseMySQL::internalExecuteQuery
bool internalExecuteQuery(const std::string &query)
Definition: mysql.cpp:120
MySQLResult::~MySQLResult
virtual ~MySQLResult()
Definition: mysql.cpp:261
DatabaseMySQL::executeQuery
virtual bool executeQuery(const std::string &query)
Definition: mysql.cpp:131
DatabaseMySQL::DatabaseMySQL
DatabaseMySQL()
Definition: mysql.cpp:29
DatabaseMySQL::getLastInsertedRowID
virtual uint64 getLastInsertedRowID()
Definition: mysql.cpp:171
DatabaseMySQL::commit
virtual bool commit()
Definition: mysql.cpp:110
MySQLResult::MySQLResult
MySQLResult(MYSQL_RES *result)
Definition: mysql.cpp:267
DatabaseMySQL::storeQuery
virtual DBResultPtr storeQuery(const std::string &query)
Definition: mysql.cpp:148
MySQLResult::RowNames_t
std::map< const std::string, uint32 > RowNames_t
Definition: mysql.h:84
DatabaseMySQL
Definition: mysql.h:36
uint32
uint32_t uint32
Definition: types.h:35
MySQLResult::m_result
MYSQL_RES * m_result
Definition: mysql.h:87
DatabaseMySQL::rollback
virtual bool rollback()
Definition: mysql.cpp:100
MySQLResult
Definition: mysql.h:65
MySQLResult::free
virtual void free()
Definition: mysql.cpp:244
MySQLResult::getDataStream
virtual const char * getDataStream(const std::string &s, uint64 &size)
Definition: mysql.cpp:228
MySQLResult::m_row
MYSQL_ROW m_row
Definition: mysql.h:88
DatabaseMySQL::escapeString
virtual std::string escapeString(const std::string &s)
Definition: mysql.cpp:176
int64
int64_t int64
Definition: types.h:38
uint16
uint16_t uint16
Definition: types.h:36
int32
int32_t int32
Definition: types.h:39
MySQLResult::next
virtual bool next()
Definition: mysql.cpp:255
DatabaseMySQL::~DatabaseMySQL
virtual ~DatabaseMySQL()
Definition: mysql.cpp:40
DatabaseMySQL::beginTransaction
virtual bool beginTransaction()
Definition: mysql.cpp:95
MySQLResult::getDataLong
virtual int64 getDataLong(const std::string &s)
Definition: mysql.cpp:208
DatabaseMySQL::connect
virtual void connect(const std::string &host, const std::string &user, const std::string &pass, const std::string &db, uint16 port, const std::string &unix_socket="")
Definition: mysql.cpp:46
uint64
uint64_t uint64
Definition: types.h:34
database.h
Database
Definition: database.h:31
stdext::shared_object_ptr
Definition: shared_object.h:39
DatabaseMySQL::handleError
bool handleError()
Definition: mysql.cpp:63
MySQLResult::getRowCount
virtual int getRowCount()
Definition: mysql.h:81
MySQLResult::getDataInt
virtual int32 getDataInt(const std::string &s)
Definition: mysql.cpp:198
DatabaseMySQL::escapeBlob
virtual std::string escapeBlob(const char *s, uint32 length)
Definition: mysql.cpp:181
global.h
MySQLResult::m_names
RowNames_t m_names
Definition: mysql.h:85
DatabaseMySQL::m_handle
MYSQL * m_handle
Definition: mysql.h:62
DBResult
Definition: database.h:153