OregonCore  revision fb2a440-git
Your Favourite TBC server
DatabaseImpl.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 #include "Database/Database.h"
19 #include "Database/SqlOperations.h"
20 
21 // Function body definitions for the template function members of the Database class
22 
23 #define ASYNC_QUERY_BODY(sql, queue_itr) \
24  if (!sql) return false; \
25  \
26  QueryQueues::iterator queue_itr; \
27  \
28  { \
29  ACE_Based::Thread * queryThread = ACE_Based::Thread::current(); \
30  queue_itr = m_queryQueues.find(queryThread); \
31  if (queue_itr == m_queryQueues.end()) return false; \
32  }
33 
34 #define ASYNC_PQUERY_BODY(format, szQuery) \
35  if (!format) return false; \
36  \
37  char szQuery [MAX_QUERY_LEN]; \
38  \
39  { \
40  va_list ap; \
41  \
42  va_start(ap, format); \
43  int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap ); \
44  va_end(ap); \
45  \
46  if (res==-1) \
47  { \
48  sLog.outError("SQL Query truncated (and not execute) for format: %s",format); \
49  return false; \
50  } \
51  }
52 
53 #define ASYNC_DELAYHOLDER_BODY(holder, queue_itr) \
54  if (!holder) return false; \
55  \
56  QueryQueues::iterator queue_itr; \
57  \
58  { \
59  ACE_Based::Thread * queryThread = ACE_Based::Thread::current(); \
60  queue_itr = m_queryQueues.find(queryThread); \
61  if (queue_itr == m_queryQueues.end()) return false; \
62  }
63 
64 // Query / member
65 
66 template<class Class>
67 bool
68 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr), const char* sql)
69 {
70  ASYNC_QUERY_BODY(sql, itr)
71  return m_threadBody->Delay(new SqlQuery(sql, new Oregon::QueryCallback<Class>(object, method), itr->second));
72 }
73 
74 template<class Class, typename ParamType1>
75 bool
76 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char* sql)
77 {
78  ASYNC_QUERY_BODY(sql, itr)
79  return m_threadBody->Delay(new SqlQuery(sql, new Oregon::QueryCallback<Class, ParamType1>(object, method, QueryResult_AutoPtr(NULL), param1), itr->second));
80 }
81 
82 template<class Class, typename ParamType1, typename ParamType2>
83 bool
84 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* sql)
85 {
86  ASYNC_QUERY_BODY(sql, itr)
87  return m_threadBody->Delay(new SqlQuery(sql, new Oregon::QueryCallback<Class, ParamType1, ParamType2>(object, method, QueryResult_AutoPtr(NULL), param1, param2), itr->second));
88 }
89 
90 template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
91 bool
92 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* sql)
93 {
94  ASYNC_QUERY_BODY(sql, itr)
95  return m_threadBody->Delay(new SqlQuery(sql, new Oregon::QueryCallback<Class, ParamType1, ParamType2, ParamType3>(object, method, QueryResult_AutoPtr(NULL), param1, param2, param3), itr->second));
96 }
97 
98 // Query / static
99 
100 template<typename ParamType1>
101 bool
102 Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char* sql)
103 {
104  ASYNC_QUERY_BODY(sql, itr)
105  return m_threadBody->Delay(new SqlQuery(sql, new Oregon::SQueryCallback<ParamType1>(method, QueryResult_AutoPtr(NULL), param1), itr->second));
106 }
107 
108 template<typename ParamType1, typename ParamType2>
109 bool
110 Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* sql)
111 {
112  ASYNC_QUERY_BODY(sql, itr)
113  return m_threadBody->Delay(new SqlQuery(sql, new Oregon::SQueryCallback<ParamType1, ParamType2>(method, QueryResult_AutoPtr(NULL), param1, param2), itr->second));
114 }
115 
116 template<typename ParamType1, typename ParamType2, typename ParamType3>
117 bool
118 Database::AsyncQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* sql)
119 {
120  ASYNC_QUERY_BODY(sql, itr)
121  return m_threadBody->Delay(new SqlQuery(sql, new Oregon::SQueryCallback<ParamType1, ParamType2, ParamType3>(method, QueryResult_AutoPtr(NULL), param1, param2, param3), itr->second));
122 }
123 
124 // PQuery / member
125 
126 template<class Class>
127 bool
128 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr), const char* format, ...)
129 {
130  ASYNC_PQUERY_BODY(format, szQuery)
131  return AsyncQuery(object, method, szQuery);
132 }
133 
134 template<class Class, typename ParamType1>
135 bool
136 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char* format, ...)
137 {
138  ASYNC_PQUERY_BODY(format, szQuery)
139  return AsyncQuery(object, method, param1, szQuery);
140 }
141 
142 template<class Class, typename ParamType1, typename ParamType2>
143 bool
144 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* format, ...)
145 {
146  ASYNC_PQUERY_BODY(format, szQuery)
147  return AsyncQuery(object, method, param1, param2, szQuery);
148 }
149 
150 template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
151 bool
152 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* format, ...)
153 {
154  ASYNC_PQUERY_BODY(format, szQuery)
155  return AsyncQuery(object, method, param1, param2, param3, szQuery);
156 }
157 
158 // PQuery / static
159 
160 template<typename ParamType1>
161 bool
162 Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1), ParamType1 param1, const char* format, ...)
163 {
164  ASYNC_PQUERY_BODY(format, szQuery)
165  return AsyncQuery(method, param1, szQuery);
166 }
167 
168 template<typename ParamType1, typename ParamType2>
169 bool
170 Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* format, ...)
171 {
172  ASYNC_PQUERY_BODY(format, szQuery)
173  return AsyncQuery(method, param1, param2, szQuery);
174 }
175 
176 template<typename ParamType1, typename ParamType2, typename ParamType3>
177 bool
178 Database::AsyncPQuery(void (*method)(QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* format, ...)
179 {
180  ASYNC_PQUERY_BODY(format, szQuery)
181  return AsyncQuery(method, param1, param2, param3, szQuery);
182 }
183 
184 // QueryHolder
185 
186 template<class Class>
187 bool
188 Database::DelayQueryHolder(Class* object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*), SqlQueryHolder* holder)
189 {
190  ASYNC_DELAYHOLDER_BODY(holder, itr)
191  return holder->Execute(new Oregon::QueryCallback<Class, SqlQueryHolder*>(object, method, QueryResult_AutoPtr(NULL), holder), m_threadBody, itr->second);
192 }
193 
194 template<class Class, typename ParamType1>
195 bool
196 Database::DelayQueryHolder(Class* object, void (Class::*method)(QueryResult_AutoPtr, SqlQueryHolder*, ParamType1), SqlQueryHolder* holder, ParamType1 param1)
197 {
198  ASYNC_DELAYHOLDER_BODY(holder, itr)
199  return holder->Execute(new Oregon::QueryCallback<Class, SqlQueryHolder*, ParamType1>(object, method, QueryResult_AutoPtr(NULL), holder, param1), m_threadBody, itr->second);
200 }
201 
202 #undef ASYNC_QUERY_BODY
203 #undef ASYNC_PQUERY_BODY
204 #undef ASYNC_DELAYHOLDER_BODY
205 
bool AsyncQuery(Class *object, void(Class::*method)(QueryResult_AutoPtr), const char *sql)
Definition: DatabaseImpl.h:68
bool Execute(Oregon::IQueryCallback *callback, SqlDelayThread *thread, SqlResultQueue *queue)
SqlDelayThread * m_threadBody
Definition: Database.h:51
#define ASYNC_PQUERY_BODY(format, szQuery)
Definition: DatabaseImpl.h:34
#define ASYNC_QUERY_BODY(sql, queue_itr)
Definition: DatabaseImpl.h:23
#define ASYNC_DELAYHOLDER_BODY(holder, queue_itr)
Definition: DatabaseImpl.h:53
bool AsyncPQuery(Class *object, void(Class::*method)(QueryResult_AutoPtr), const char *format,...) ATTR_PRINTF(4
Definition: DatabaseImpl.h:128
bool Delay(SqlOperation *sql)
ACE_Refcounted_Auto_Ptr< QueryResult, ACE_Null_Mutex > QueryResult_AutoPtr
Definition: QueryResult.h:113
bool bool bool bool bool bool bool bool DelayQueryHolder(Class *object, void(Class::*method)(QueryResult_AutoPtr, SqlQueryHolder *), SqlQueryHolder *holder)