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 | #ifndef DATABASE_H |
---|
22 | #define DATABASE_H |
---|
23 | |
---|
24 | #include "zthread/Thread.h" |
---|
25 | #include "../src/zthread/ThreadImpl.h" |
---|
26 | #include "Utilities/HashMap.h" |
---|
27 | #include "Database/SqlDelayThread.h" |
---|
28 | |
---|
29 | class SqlTransaction; |
---|
30 | class SqlResultQueue; |
---|
31 | class SqlQueryHolder; |
---|
32 | |
---|
33 | typedef HM_NAMESPACE::hash_map<ZThread::ThreadImpl*, SqlTransaction*> TransactionQueues; |
---|
34 | typedef HM_NAMESPACE::hash_map<ZThread::ThreadImpl*, SqlResultQueue*> QueryQueues; |
---|
35 | |
---|
36 | #define MAX_QUERY_LEN 1024 |
---|
37 | |
---|
38 | class TRINITY_DLL_SPEC Database |
---|
39 | { |
---|
40 | protected: |
---|
41 | Database() : m_threadBody(NULL), m_delayThread(NULL) {}; |
---|
42 | |
---|
43 | TransactionQueues m_tranQueues; ///< Transaction queues from diff. threads |
---|
44 | QueryQueues m_queryQueues; ///< Query queues from diff threads |
---|
45 | SqlDelayThread* m_threadBody; ///< Pointer to delay sql executer |
---|
46 | ZThread::Thread* m_delayThread; ///< Pointer to executer thread |
---|
47 | |
---|
48 | public: |
---|
49 | |
---|
50 | virtual ~Database(); |
---|
51 | |
---|
52 | virtual bool Initialize(const char *infoString); |
---|
53 | virtual void InitDelayThread() = 0; |
---|
54 | virtual void HaltDelayThread() = 0; |
---|
55 | |
---|
56 | virtual QueryResult* Query(const char *sql) = 0; |
---|
57 | QueryResult* PQuery(const char *format,...) ATTR_PRINTF(2,3); |
---|
58 | |
---|
59 | /// Async queries and query holders, implemented in DatabaseImpl.h |
---|
60 | template<class Class> |
---|
61 | bool AsyncQuery(Class *object, void (Class::*method)(QueryResult*), const char *sql); |
---|
62 | template<class Class, typename ParamType1> |
---|
63 | bool AsyncQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char *sql); |
---|
64 | template<typename ParamType1> |
---|
65 | bool AsyncQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *sql); |
---|
66 | template<class Class> |
---|
67 | bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*), const char *format,...) ATTR_PRINTF(4,5); |
---|
68 | template<class Class, typename ParamType1> |
---|
69 | bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(5,6); |
---|
70 | template<typename ParamType1> |
---|
71 | bool AsyncPQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(5,6); |
---|
72 | template<class Class> |
---|
73 | bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*), SqlQueryHolder *holder); |
---|
74 | template<class Class, typename ParamType1> |
---|
75 | bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*, ParamType1), SqlQueryHolder *holder, ParamType1 param1); |
---|
76 | |
---|
77 | virtual bool Execute(const char *sql) = 0; |
---|
78 | bool PExecute(const char *format,...) ATTR_PRINTF(2,3); |
---|
79 | virtual bool DirectExecute(const char* sql) = 0; |
---|
80 | bool DirectPExecute(const char *format,...) ATTR_PRINTF(2,3); |
---|
81 | |
---|
82 | // Writes SQL commands to a LOG file (see Trinityd.conf "LogSQL") |
---|
83 | bool PExecuteLog(const char *format,...) ATTR_PRINTF(2,3); |
---|
84 | |
---|
85 | virtual bool BeginTransaction() // nothing do if DB not support transactions |
---|
86 | { |
---|
87 | return true; |
---|
88 | } |
---|
89 | virtual bool CommitTransaction() // nothing do if DB not support transactions |
---|
90 | { |
---|
91 | return true; |
---|
92 | } |
---|
93 | virtual bool RollbackTransaction() // can't rollback without transaction support |
---|
94 | { |
---|
95 | return false; |
---|
96 | } |
---|
97 | |
---|
98 | virtual operator bool () const = 0; |
---|
99 | |
---|
100 | virtual unsigned long escape_string(char *to, const char *from, unsigned long length) { strncpy(to,from,length); return length; } |
---|
101 | void escape_string(std::string& str); |
---|
102 | |
---|
103 | // must be called before first query in thread (one time for thread using one from existed Database objects) |
---|
104 | virtual void ThreadStart(); |
---|
105 | // must be called before finish thread run (one time for thread using one from existed Database objects) |
---|
106 | virtual void ThreadEnd(); |
---|
107 | |
---|
108 | // sets the result queue of the current thread, be careful what thread you call this from |
---|
109 | void SetResultQueue(SqlResultQueue * queue); |
---|
110 | |
---|
111 | private: |
---|
112 | bool m_logSQL; |
---|
113 | std::string m_logsDir; |
---|
114 | }; |
---|
115 | #endif |
---|