root/trunk/src/shared/Database/DatabaseImpl.h @ 102

Revision 102, 5.1 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Database/Database.h"
22#include "Database/SqlOperations.h"
23
24/// Function body definitions for the template function members of the Database class
25
26template<class Class>
27bool
28Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult*), const char *sql)
29{
30    if (!sql) return false;
31    ZThread::ThreadImpl * queryThread = ZThread::ThreadImpl::current();
32    QueryQueues::iterator itr = m_queryQueues.find(queryThread);
33    if (itr == m_queryQueues.end()) return false;
34    m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class>(object, method), itr->second));
35    return true;
36}
37
38template<class Class, typename ParamType1>
39bool
40Database::AsyncQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char *sql)
41{
42    if (!sql) return false;
43    ZThread::ThreadImpl * queryThread = ZThread::ThreadImpl::current();
44    QueryQueues::iterator itr = m_queryQueues.find(queryThread);
45    if (itr == m_queryQueues.end()) return false;
46    m_threadBody->Delay(new SqlQuery(sql, new Trinity::QueryCallback<Class, ParamType1>(object, method, (QueryResult*)NULL, param1), itr->second));
47    return true;
48}
49
50template<typename ParamType1>
51bool
52Database::AsyncQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *sql)
53{
54    if (!sql) return false;
55    ZThread::ThreadImpl * queryThread = ZThread::ThreadImpl::current();
56    QueryQueues::iterator itr = m_queryQueues.find(queryThread);
57    if (itr == m_queryQueues.end()) return false;
58    m_threadBody->Delay(new SqlQuery(sql, new Trinity::SQueryCallback<ParamType1>(method, (QueryResult*)NULL, param1), itr->second));
59    return true;
60}
61
62template<class Class>
63bool
64Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult*), const char *format,...)
65{
66    if(!format) return false;
67
68    va_list ap;
69    char szQuery [MAX_QUERY_LEN];
70    va_start(ap, format);
71    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
72    va_end(ap);
73
74    if(res==-1)
75    {
76        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
77        return false;
78    }
79
80    return AsyncQuery(object, method, szQuery);
81}
82
83template<class Class, typename ParamType1>
84bool
85Database::AsyncPQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...)
86{
87    if(!format) return false;
88
89    va_list ap;
90    char szQuery [MAX_QUERY_LEN];
91    va_start(ap, format);
92    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
93    va_end(ap);
94
95    if(res==-1)
96    {
97        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
98        return false;
99    }
100
101    return AsyncQuery(object, method, param1, szQuery);
102}
103
104template<typename ParamType1>
105bool
106Database::AsyncPQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...)
107{
108    if(!format) return false;
109
110    va_list ap;
111    char szQuery [MAX_QUERY_LEN];
112    va_start(ap, format);
113    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
114    va_end(ap);
115
116    if(res==-1)
117    {
118        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
119        return false;
120    }
121
122    return AsyncQuery(method, param1, szQuery);
123}
124
125
126template<class Class>
127bool
128Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*), SqlQueryHolder *holder)
129{
130    if (!holder) return false;
131    ZThread::ThreadImpl * queryThread = ZThread::ThreadImpl::current();
132    QueryQueues::iterator itr = m_queryQueues.find(queryThread);
133    if (itr == m_queryQueues.end()) return false;
134    holder->Execute(new Trinity::QueryCallback<Class, SqlQueryHolder*>(object, method, (QueryResult*)NULL, holder), m_threadBody, itr->second);
135    return true;
136}
137
138template<class Class, typename ParamType1>
139bool
140Database::DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*, ParamType1), SqlQueryHolder *holder, ParamType1 param1)
141{
142    if (!holder) return false;
143    ZThread::ThreadImpl * queryThread = ZThread::ThreadImpl::current();
144    QueryQueues::iterator itr = m_queryQueues.find(queryThread);
145    if (itr == m_queryQueues.end()) return false;
146    holder->Execute(new Trinity::QueryCallback<Class, SqlQueryHolder*, ParamType1>(object, method, (QueryResult*)NULL, holder, param1), m_threadBody, itr->second);
147    return true;
148}
Note: See TracBrowser for help on using the browser.