root/trunk/src/shared/Database/Database.h @ 206

Revision 206, 5.9 kB (checked in by yumileroy, 17 years ago)

[svn] * Switch from hashmap to unordered map. - cleanup source - mangos. Help - Aokromes

Original author: KingPin?
Date: 2008-11-10 06:53:00-06: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#ifndef DATABASE_H
22#define DATABASE_H
23
24#include "zthread/Thread.h"
25#include "../src/zthread/ThreadImpl.h"
26#include "Utilities/UnorderedMap.h"
27#include "Database/SqlDelayThread.h"
28
29class SqlTransaction;
30class SqlResultQueue;
31class SqlQueryHolder;
32
33typedef UNORDERED_MAP<ZThread::ThreadImpl*, SqlTransaction*> TransactionQueues;
34typedef UNORDERED_MAP<ZThread::ThreadImpl*, SqlResultQueue*> QueryQueues;
35
36#define MAX_QUERY_LEN   1024
37
38class 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<class Class, typename ParamType1, typename ParamType2>
65            bool AsyncQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql);
66        template<typename ParamType1>
67            bool AsyncQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *sql);
68        template<typename ParamType1, typename ParamType2>
69            bool AsyncQuery(void (*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql);
70        template<class Class>
71            bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*), const char *format,...) ATTR_PRINTF(4,5);
72        template<class Class, typename ParamType1>
73            bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(5,6);
74        template<class Class, typename ParamType1, typename ParamType2>
75            bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...) ATTR_PRINTF(5,6);
76        template<typename ParamType1>
77            bool AsyncPQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(5,6);
78        template<typename ParamType1, typename ParamType2>
79            bool AsyncPQuery(void (*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...) ATTR_PRINTF(5,6);
80        template<class Class>
81            bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*), SqlQueryHolder *holder);
82        template<class Class, typename ParamType1>
83            bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*, ParamType1), SqlQueryHolder *holder, ParamType1 param1);
84
85        virtual bool Execute(const char *sql) = 0;
86        bool PExecute(const char *format,...) ATTR_PRINTF(2,3);
87        virtual bool DirectExecute(const char* sql) = 0;
88        bool DirectPExecute(const char *format,...) ATTR_PRINTF(2,3);
89
90        // Writes SQL commands to a LOG file (see Trinityd.conf "LogSQL")
91        bool PExecuteLog(const char *format,...) ATTR_PRINTF(2,3);
92
93        virtual bool BeginTransaction()                     // nothing do if DB not support transactions
94        {
95            return true;
96        }
97        virtual bool CommitTransaction()                    // nothing do if DB not support transactions
98        {
99            return true;
100        }
101        virtual bool RollbackTransaction()                  // can't rollback without transaction support
102        {
103            return false;
104        }
105
106        virtual operator bool () const = 0;
107
108        virtual unsigned long escape_string(char *to, const char *from, unsigned long length) { strncpy(to,from,length); return length; }
109        void escape_string(std::string& str);
110
111        // must be called before first query in thread (one time for thread using one from existed Database objects)
112        virtual void ThreadStart();
113        // must be called before finish thread run (one time for thread using one from existed Database objects)
114        virtual void ThreadEnd();
115
116        // sets the result queue of the current thread, be careful what thread you call this from
117        void SetResultQueue(SqlResultQueue * queue);
118
119    private:
120        bool m_logSQL;
121        std::string m_logsDir;
122};
123#endif
Note: See TracBrowser for help on using the browser.