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