Updated Copyright year to 2013
[getmangos.git] / src / shared / Database / DatabaseImpl.h
blob2d4ce9a2dc6e04823db40897a663f7200059f4eb
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Database/Database.h"
20 #include "Database/SqlOperations.h"
22 /// Function body definitions for the template function members of the Database class
24 #define ASYNC_QUERY_BODY(sql) if (!sql || !m_pResultQueue) return false;
25 #define ASYNC_DELAYHOLDER_BODY(holder) if (!holder || !m_pResultQueue) return false;
27 #define ASYNC_PQUERY_BODY(format, szQuery) \
28 if(!format) return false; \
30 char szQuery [MAX_QUERY_LEN]; \
32 { \
33 va_list ap; \
35 va_start(ap, format); \
36 int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap ); \
37 va_end(ap); \
39 if(res==-1) \
40 { \
41 sLog.outError("SQL Query truncated (and not execute) for format: %s",format); \
42 return false; \
43 } \
46 // -- Query / member --
48 template<class Class>
49 bool
50 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult*), const char* sql)
52 ASYNC_QUERY_BODY(sql)
53 return m_threadBody->Delay(new SqlQuery(sql, new MaNGOS::QueryCallback<Class>(object, method), m_pResultQueue));
56 template<class Class, typename ParamType1>
57 bool
58 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char* sql)
60 ASYNC_QUERY_BODY(sql)
61 return m_threadBody->Delay(new SqlQuery(sql, new MaNGOS::QueryCallback<Class, ParamType1>(object, method, (QueryResult*)NULL, param1), m_pResultQueue));
64 template<class Class, typename ParamType1, typename ParamType2>
65 bool
66 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* sql)
68 ASYNC_QUERY_BODY(sql)
69 return m_threadBody->Delay(new SqlQuery(sql, new MaNGOS::QueryCallback<Class, ParamType1, ParamType2>(object, method, (QueryResult*)NULL, param1, param2), m_pResultQueue));
72 template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
73 bool
74 Database::AsyncQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* sql)
76 ASYNC_QUERY_BODY(sql)
77 return m_threadBody->Delay(new SqlQuery(sql, new MaNGOS::QueryCallback<Class, ParamType1, ParamType2, ParamType3>(object, method, (QueryResult*)NULL, param1, param2, param3), m_pResultQueue));
80 // -- Query / static --
82 template<typename ParamType1>
83 bool
84 Database::AsyncQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char* sql)
86 ASYNC_QUERY_BODY(sql)
87 return m_threadBody->Delay(new SqlQuery(sql, new MaNGOS::SQueryCallback<ParamType1>(method, (QueryResult*)NULL, param1), m_pResultQueue));
90 template<typename ParamType1, typename ParamType2>
91 bool
92 Database::AsyncQuery(void (*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* sql)
94 ASYNC_QUERY_BODY(sql)
95 return m_threadBody->Delay(new SqlQuery(sql, new MaNGOS::SQueryCallback<ParamType1, ParamType2>(method, (QueryResult*)NULL, param1, param2), m_pResultQueue));
98 template<typename ParamType1, typename ParamType2, typename ParamType3>
99 bool
100 Database::AsyncQuery(void (*method)(QueryResult*, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* sql)
102 ASYNC_QUERY_BODY(sql)
103 return m_threadBody->Delay(new SqlQuery(sql, new MaNGOS::SQueryCallback<ParamType1, ParamType2, ParamType3>(method, (QueryResult*)NULL, param1, param2, param3), m_pResultQueue));
106 // -- PQuery / member --
108 template<class Class>
109 bool
110 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult*), const char* format, ...)
112 ASYNC_PQUERY_BODY(format, szQuery)
113 return AsyncQuery(object, method, szQuery);
116 template<class Class, typename ParamType1>
117 bool
118 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char* format, ...)
120 ASYNC_PQUERY_BODY(format, szQuery)
121 return AsyncQuery(object, method, param1, szQuery);
124 template<class Class, typename ParamType1, typename ParamType2>
125 bool
126 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* format, ...)
128 ASYNC_PQUERY_BODY(format, szQuery)
129 return AsyncQuery(object, method, param1, param2, szQuery);
132 template<class Class, typename ParamType1, typename ParamType2, typename ParamType3>
133 bool
134 Database::AsyncPQuery(Class* object, void (Class::*method)(QueryResult*, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* format, ...)
136 ASYNC_PQUERY_BODY(format, szQuery)
137 return AsyncQuery(object, method, param1, param2, param3, szQuery);
140 // -- PQuery / static --
142 template<typename ParamType1>
143 bool
144 Database::AsyncPQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char* format, ...)
146 ASYNC_PQUERY_BODY(format, szQuery)
147 return AsyncQuery(method, param1, szQuery);
150 template<typename ParamType1, typename ParamType2>
151 bool
152 Database::AsyncPQuery(void (*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char* format, ...)
154 ASYNC_PQUERY_BODY(format, szQuery)
155 return AsyncQuery(method, param1, param2, szQuery);
158 template<typename ParamType1, typename ParamType2, typename ParamType3>
159 bool
160 Database::AsyncPQuery(void (*method)(QueryResult*, ParamType1, ParamType2, ParamType3), ParamType1 param1, ParamType2 param2, ParamType3 param3, const char* format, ...)
162 ASYNC_PQUERY_BODY(format, szQuery)
163 return AsyncQuery(method, param1, param2, param3, szQuery);
166 // -- QueryHolder --
168 template<class Class>
169 bool
170 Database::DelayQueryHolder(Class* object, void (Class::*method)(QueryResult*, SqlQueryHolder*), SqlQueryHolder* holder)
172 ASYNC_DELAYHOLDER_BODY(holder)
173 return holder->Execute(new MaNGOS::QueryCallback<Class, SqlQueryHolder*>(object, method, (QueryResult*)NULL, holder), m_threadBody, m_pResultQueue);
176 template<class Class, typename ParamType1>
177 bool
178 Database::DelayQueryHolder(Class* object, void (Class::*method)(QueryResult*, SqlQueryHolder*, ParamType1), SqlQueryHolder* holder, ParamType1 param1)
180 ASYNC_DELAYHOLDER_BODY(holder)
181 return holder->Execute(new MaNGOS::QueryCallback<Class, SqlQueryHolder*, ParamType1>(object, method, (QueryResult*)NULL, holder, param1), m_threadBody, m_pResultQueue);
184 #undef ASYNC_QUERY_BODY
185 #undef ASYNC_PQUERY_BODY
186 #undef ASYNC_DELAYHOLDER_BODY