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

Revision 2, 5.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

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