Otclient  14/8/2020
database.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 DATABASE_H
24 #define DATABASE_H
25 #include "declarations.h"
26 
28 
29 #include <boost/thread.hpp>
30 
31 class Database : public LuaObject
32 {
33 public:
34  friend class DBTransaction;
35 
36  Database(): m_connected(false) {}
37  virtual ~Database() { m_connected = false; }
38 
44  virtual void connect(const std::string& host, const std::string& user, const std::string& pass,
45  const std::string& db, uint16 port, const std::string& unix_socket = "") {}
46 
57  virtual bool beginTransaction() { return false; }
58  virtual bool rollback() { return false; }
59  virtual bool commit() { return false; }
60 
69  virtual bool executeQuery(const std::string& query) { return false; }
70 
79  virtual DBResultPtr storeQuery(const std::string& query) { return nullptr; }
80 
89  virtual std::string escapeString(const std::string&) { return "''"; }
90 
100  virtual std::string escapeBlob(const char*, uint32) { return "''"; }
101 
107  virtual uint64 getLastInsertedRowID() { return 0; }
108 
114  virtual std::string getStringComparer() { return "= "; }
115  virtual std::string getUpdateLimiter() { return " LIMIT 1;"; }
116 
122  virtual Fw::DatabaseEngine getDatabaseEngine() { return Fw::DatabaseNone; }
123 
131  bool isConnected() { return m_connected; }
132 
133 protected:
139  void setConnected(bool connected) { m_connected = connected; }
140 
141  virtual bool handleError() { return false; }
142  virtual bool internalExecuteQuery(const std::string &query) { return false; }
143 
145 
148 
149 private:
150  static DatabasePtr m_instance;
151 };
152 
153 class DBResult : public LuaObject
154 {
155 public:
156  DBResult() {}
157  virtual ~DBResult() {}
158 
163  virtual int32 getDataInt(const std::string&) { return 0; }
164 
169  virtual int64 getDataLong(const std::string&) { return 0; }
170 
175  virtual std::string getDataString(const std::string&) { return ""; }
176 
181  virtual const char* getDataStream(const std::string&, uint64&) { return ""; }
182 
185  virtual void free() {}
186 
190  virtual bool next() { return false; }
191 
195  virtual int getRowCount() { return 0; }
196 };
197 
203 class DBQuery : public std::stringstream, public LuaObject
204 {
205 friend class Database;
206 public:
207  DBQuery() { databaseLock.lock(); }
208  ~DBQuery() { databaseLock.unlock(); }
209 
210  void set(std::string& query) { str(query); }
211  void append(char query) { putback(query); }
212 
213 protected:
214  static boost::recursive_mutex databaseLock;
215 };
216 
222 class DBInsert
223 {
224 public:
230  DBInsert(const DatabasePtr& db): m_db(db), m_rows(0) {}
232 
238  void setQuery(const std::string& query);
239 
247  bool addRow(const std::string& row);
248 
252  bool addRow(std::stringstream& row);
253 
257  bool execute();
258 
259 protected:
261 
263  std::string m_query;
264  std::string m_buf;
265 };
266 
268 {
269 public:
271  {
272  m_db = database;
273  m_state = STATE_FRESH;
274  }
275 
277  {
278  if(m_state == STATE_READY)
279  m_db->rollback();
280  }
281 
282  bool begin()
283  {
284  m_state = STATE_READY;
285  return m_db->beginTransaction();
286  }
287 
288  bool commit()
289  {
290  if(m_state != STATE_READY)
291  return false;
292 
293  m_state = STATE_DONE;
294  return m_db->commit();
295  }
296 
297 private:
298  DatabasePtr m_db;
299  enum TransactionStates_t
300  {
301  STATE_FRESH,
302  STATE_READY,
303  STATE_DONE
304  } m_state;
305 };
306 
307 #endif
DBInsert::m_buf
std::string m_buf
Definition: database.h:264
Database::setConnected
void setConnected(bool connected)
Definition: database.h:139
Database::rollback
virtual bool rollback()
Definition: database.h:58
Database::escapeBlob
virtual std::string escapeBlob(const char *, uint32)
Definition: database.h:100
DBResult::getDataStream
virtual const char * getDataStream(const std::string &, uint64 &)
Definition: database.h:181
Database::getLastInsertedRowID
virtual uint64 getLastInsertedRowID()
Definition: database.h:107
DBInsert
Definition: database.h:222
Database::handleError
virtual bool handleError()
Definition: database.h:141
uint32
uint32_t uint32
Definition: types.h:35
DBTransaction::begin
bool begin()
Definition: database.h:282
DBQuery::append
void append(char query)
Definition: database.h:211
ticks_t
int64 ticks_t
Definition: types.h:43
DBInsert::m_db
DatabasePtr m_db
Definition: database.h:260
int64
int64_t int64
Definition: types.h:38
DBQuery::set
void set(std::string &query)
Definition: database.h:210
DBInsert::addRow
bool addRow(const std::string &row)
Definition: database.cpp:43
Database::escapeString
virtual std::string escapeString(const std::string &)
Definition: database.h:89
DBTransaction::DBTransaction
DBTransaction(DatabasePtr database)
Definition: database.h:270
DBInsert::setQuery
void setQuery(const std::string &query)
Definition: database.cpp:36
luaobject.h
DBQuery::DBQuery
DBQuery()
Definition: database.h:207
DBInsert::~DBInsert
~DBInsert()
Definition: database.h:231
uint16
uint16_t uint16
Definition: types.h:36
Database::m_connected
bool m_connected
Definition: database.h:147
DBInsert::execute
bool execute()
Definition: database.cpp:69
Database::storeQuery
virtual DBResultPtr storeQuery(const std::string &query)
Definition: database.h:79
DBInsert::DBInsert
DBInsert(const DatabasePtr &db)
Definition: database.h:230
DBTransaction::~DBTransaction
~DBTransaction()
Definition: database.h:276
int32
int32_t int32
Definition: types.h:39
Database::executeQuery
virtual bool executeQuery(const std::string &query)
Definition: database.h:69
DBResult::getRowCount
virtual int getRowCount()
Definition: database.h:195
DBResult::getDataInt
virtual int32 getDataInt(const std::string &)
Definition: database.h:163
Database::verifyResult
DBResultPtr verifyResult(DBResultPtr result)
Definition: database.cpp:27
Database::~Database
virtual ~Database()
Definition: database.h:37
DBQuery
Definition: database.h:203
uint64
uint64_t uint64
Definition: types.h:34
DBResult::getDataString
virtual std::string getDataString(const std::string &)
Definition: database.h:175
DBTransaction::commit
bool commit()
Definition: database.h:288
DBResult::next
virtual bool next()
Definition: database.h:190
Database
Definition: database.h:31
stdext::shared_object_ptr
Definition: shared_object.h:39
Database::beginTransaction
virtual bool beginTransaction()
Definition: database.h:57
DBQuery::databaseLock
static boost::recursive_mutex databaseLock
Definition: database.h:214
Database::getDatabaseEngine
virtual Fw::DatabaseEngine getDatabaseEngine()
Definition: database.h:122
DBResult::getDataLong
virtual int64 getDataLong(const std::string &)
Definition: database.h:169
DBResult::DBResult
DBResult()
Definition: database.h:156
DBResult::free
virtual void free()
Definition: database.h:185
Database::getUpdateLimiter
virtual std::string getUpdateLimiter()
Definition: database.h:115
DBResult::~DBResult
virtual ~DBResult()
Definition: database.h:157
Database::Database
Database()
Definition: database.h:36
Database::getStringComparer
virtual std::string getStringComparer()
Definition: database.h:114
Database::m_use
ticks_t m_use
Definition: database.h:146
DBInsert::m_query
std::string m_query
Definition: database.h:263
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
DBInsert::m_rows
uint32 m_rows
Definition: database.h:262
DBQuery::~DBQuery
~DBQuery()
Definition: database.h:208
Database::commit
virtual bool commit()
Definition: database.h:59
Database::isConnected
bool isConnected()
Definition: database.h:131
Database::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: database.h:44
DBTransaction
Definition: database.h:267
declarations.h
Database::internalExecuteQuery
virtual bool internalExecuteQuery(const std::string &query)
Definition: database.h:142
DBResult
Definition: database.h:153