OregonCore  revision 3611e8a-git
Your Favourite TBC server
SqlOperations.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
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 #ifndef __SQLOPERATIONS_H
19 #define __SQLOPERATIONS_H
20 
21 #include "Common.h"
22 
23 #include "ace/Thread_Mutex.h"
24 #include "ace/Method_Request.h"
25 #include "LockedQueue.h"
26 #include <queue>
27 #include "Utilities/Callback.h"
28 #include "QueryResult.h"
29 #include "Database.h"
30 
31 // BASE
32 
33 class Database;
34 class SqlDelayThread;
35 struct PreparedStatement;
36 
38 {
39  public:
40  virtual void OnRemove()
41  {
42  delete this;
43  }
44  virtual void Execute(Database* db) = 0;
45  virtual ~SqlOperation() {}
46 };
47 
48 // ASYNC STATEMENTS / TRANSACTIONS
49 
50 class SqlStatement : public SqlOperation
51 {
52  private:
53  const char* m_sql;
54  public:
55  SqlStatement(const char* sql) : m_sql(strdup(sql)) {}
57  {
58  void* tofree = const_cast<char*>(m_sql);
59  free(tofree);
60  }
61  void Execute(Database* db);
62 };
63 
65 {
66  private:
69  public:
70  SqlPreparedStatement(PreparedStatement* stmt, PreparedValues& values) : m_stmt(stmt), m_values(values) {}
71 
72  void Execute(Database* db)
73  {
74  db->DirectExecute(m_stmt, m_values, NULL);
75  }
76 };
77 
79 {
80  protected:
81  friend class Database;
82  struct QueuedItem
83  {
84  union
85  {
86  char* sql;
87  struct
88  {
91  };
92  };
93 
94  bool isStmt;
95  };
96 
97  std::queue<QueuedItem> queue;
98  ACE_Thread_Mutex mutex;
99  public:
101  {
102  while (!queue.empty())
103  {
104  QueuedItem item = queue.front();
105  if (!item.isStmt)
106  free (item.sql);
107  else
108  delete item.values;
109  queue.pop();
110  }
111  }
112 
113  void DelayExecute(const char* sql)
114  {
115  QueuedItem item;
116  item.sql = strdup(sql);
117  item.isStmt = false;
118 
119  mutex.acquire();
120  queue.push(item);
121  mutex.release();
122  }
124  {
125  QueuedItem item;
126  item.stmt = stmt;
127  item.values = new PreparedValues(values.size());
128  *item.values = values;
129  item.isStmt = true;
130 
131  mutex.acquire();
132  queue.push(item);
133  mutex.release();
134  }
135  void Execute(Database* db)
136  {
137  db->ExecuteTransaction(this);
138  }
139 };
140 
141 // ASYNC QUERIES
142 
143 class SqlQuery; // contains a single async query
144 class QueryResult; // the result of one
145 class SqlResultQueue; // queue for thread sync
146 class SqlQueryHolder; // groups several async quries
147 class SqlQueryHolderEx; // points to a holder, added to the delay thread
148 
149 class SqlResultQueue : public ACE_Based::LockedQueue<Oregon::IQueryCallback*, ACE_Thread_Mutex>
150 {
151  public:
153  void Update();
154 };
155 
156 class SqlQuery : public SqlOperation
157 {
158  private:
159  const char* m_sql;
162  public:
163  SqlQuery(const char* sql, Oregon::IQueryCallback* callback, SqlResultQueue* queue)
164  : m_sql(strdup(sql)), m_callback(callback), m_queue(queue) {}
166  {
167  void* tofree = const_cast<char*>(m_sql);
168  free(tofree);
169  }
170  void Execute(Database* db);
171 };
172 
174 {
175  friend class SqlQueryHolderEx;
176  private:
177  typedef std::pair<const char*, QueryResult_AutoPtr> SqlResultPair;
178  std::vector<SqlResultPair> m_queries;
179  public:
181  ~SqlQueryHolder();
182  bool SetQuery(size_t index, const char* sql);
183  bool SetPQuery(size_t index, const char* format, ...) ATTR_PRINTF(3, 4);
184  void SetSize(size_t size);
185  QueryResult_AutoPtr GetResult(size_t index);
186  void SetResult(size_t index, QueryResult_AutoPtr result);
187  bool Execute(Oregon::IQueryCallback* callback, SqlDelayThread* thread, SqlResultQueue* queue);
188 };
189 
191 {
192  private:
196  public:
198  : m_holder(holder), m_callback(callback), m_queue(queue) {}
199  void Execute(Database* db);
200 };
201 
202 class SqlAsyncTask : public ACE_Method_Request
203 {
204  public:
205  SqlAsyncTask(Database* db, SqlOperation* op) : m_db(db), m_op(op) {}
207  {
208  if (!m_op)
209  return;
210 
211  delete m_op;
212  m_op = NULL;
213  }
214 
215  int call()
216  {
217  if (m_db == NULL || m_op == NULL)
218  return -1;
219 
220  try
221  {
222  m_op->Execute(m_db);
223  }
224  catch (...)
225  {
226  return -1;
227  }
228 
229  return 0;
230  }
231 
232  private:
235 };
236 #endif //__SQLOPERATIONS_H
237 
void DelayExecute(PreparedStatement *stmt, PreparedValues &values)
std::queue< QueuedItem > queue
Definition: SqlOperations.h:97
bool bool DirectExecute(const char *sql)
Definition: Database.h:115
virtual void OnRemove()
Definition: SqlOperations.h:40
void Execute(Database *db)
bool ExecuteTransaction(SqlTransaction *transaction)
Atomically executed SqlTransaction. Don&#39;t call this directly, use BeginTransaction and CommitTransact...
Definition: Database.cpp:593
void Execute(Database *db)
Definition: SqlOperations.h:72
SqlOperation * m_op
Oregon::IQueryCallback * m_callback
ACE_Thread_Mutex mutex
Definition: SqlOperations.h:98
size_t size() const
SqlResultQueue * m_queue
virtual ~SqlOperation()
Definition: SqlOperations.h:45
#define ATTR_PRINTF(F, V)
Definition: Define.h:60
SqlQuery(const char *sql, Oregon::IQueryCallback *callback, SqlResultQueue *queue)
PreparedStatement * m_stmt
Definition: SqlOperations.h:67
SqlStatement(const char *sql)
Definition: SqlOperations.h:55
const char * m_sql
Definition: SqlOperations.h:53
PreparedStatement * stmt
Definition: SqlOperations.h:89
PreparedValues * values
Definition: SqlOperations.h:90
PreparedValues m_values
Definition: SqlOperations.h:68
SqlResultQueue * m_queue
void DelayExecute(const char *sql)
virtual void Execute(Database *db)=0
ACE_Refcounted_Auto_Ptr< QueryResult, ACE_Null_Mutex > QueryResult_AutoPtr
Definition: QueryResult.h:113
Oregon::IQueryCallback * m_callback
std::vector< SqlResultPair > m_queries
SqlQueryHolderEx(SqlQueryHolder *holder, Oregon::IQueryCallback *callback, SqlResultQueue *queue)
Database * m_db
SqlQueryHolder * m_holder
const char * m_sql
SqlPreparedStatement(PreparedStatement *stmt, PreparedValues &values)
Definition: SqlOperations.h:70
SqlAsyncTask(Database *db, SqlOperation *op)
std::pair< const char *, QueryResult_AutoPtr > SqlResultPair