aboutsummaryrefslogtreecommitdiff
path: root/src/shared/Database/Database.h
blob: 1b9968da720ac62664e4a95e26fbc92ddbee3526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* 
 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
 *
 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef DATABASE_H
#define DATABASE_H

#include "zthread/Thread.h"
#include "../src/zthread/ThreadImpl.h"
#include "Utilities/HashMap.h"
#include "Database/SqlDelayThread.h"

class SqlTransaction;
class SqlResultQueue;
class SqlQueryHolder;

typedef HM_NAMESPACE::hash_map<ZThread::ThreadImpl*, SqlTransaction*> TransactionQueues;
typedef HM_NAMESPACE::hash_map<ZThread::ThreadImpl*, SqlResultQueue*> QueryQueues;

#define MAX_QUERY_LEN   1024

class TRINITY_DLL_SPEC Database
{
    protected:
        Database() : m_threadBody(NULL), m_delayThread(NULL) {};

        TransactionQueues m_tranQueues;                     ///< Transaction queues from diff. threads
        QueryQueues m_queryQueues;                          ///< Query queues from diff threads
        SqlDelayThread* m_threadBody;                       ///< Pointer to delay sql executer
        ZThread::Thread* m_delayThread;                     ///< Pointer to executer thread

    public:

        virtual ~Database();

        virtual bool Initialize(const char *infoString);
        virtual void InitDelayThread() = 0;
        virtual void HaltDelayThread() = 0;

        virtual QueryResult* Query(const char *sql) = 0;
        QueryResult* PQuery(const char *format,...) ATTR_PRINTF(2,3);

        /// Async queries and query holders, implemented in DatabaseImpl.h
        template<class Class>
            bool AsyncQuery(Class *object, void (Class::*method)(QueryResult*), const char *sql);
        template<class Class, typename ParamType1>
            bool AsyncQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char *sql);
        template<class Class, typename ParamType1, typename ParamType2>
            bool AsyncQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql);
        template<typename ParamType1>
            bool AsyncQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *sql);
        template<typename ParamType1, typename ParamType2>
            bool AsyncQuery(void (*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *sql);
        template<class Class>
            bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*), const char *format,...) ATTR_PRINTF(4,5);
        template<class Class, typename ParamType1>
            bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(5,6);
        template<class Class, typename ParamType1, typename ParamType2>
            bool AsyncPQuery(Class *object, void (Class::*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...) ATTR_PRINTF(5,6);
        template<typename ParamType1>
            bool AsyncPQuery(void (*method)(QueryResult*, ParamType1), ParamType1 param1, const char *format,...) ATTR_PRINTF(5,6);
        template<typename ParamType1, typename ParamType2>
            bool AsyncPQuery(void (*method)(QueryResult*, ParamType1, ParamType2), ParamType1 param1, ParamType2 param2, const char *format,...) ATTR_PRINTF(5,6);
        template<class Class>
            bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*), SqlQueryHolder *holder);
        template<class Class, typename ParamType1>
            bool DelayQueryHolder(Class *object, void (Class::*method)(QueryResult*, SqlQueryHolder*, ParamType1), SqlQueryHolder *holder, ParamType1 param1);

        virtual bool Execute(const char *sql) = 0;
        bool PExecute(const char *format,...) ATTR_PRINTF(2,3);
        virtual bool DirectExecute(const char* sql) = 0;
        bool DirectPExecute(const char *format,...) ATTR_PRINTF(2,3);

        // Writes SQL commands to a LOG file (see Trinityd.conf "LogSQL")
        bool PExecuteLog(const char *format,...) ATTR_PRINTF(2,3);

        virtual bool BeginTransaction()                     // nothing do if DB not support transactions
        {
            return true;
        }
        virtual bool CommitTransaction()                    // nothing do if DB not support transactions
        {
            return true;
        }
        virtual bool RollbackTransaction()                  // can't rollback without transaction support
        {
            return false;
        }

        virtual operator bool () const = 0;

        virtual unsigned long escape_string(char *to, const char *from, unsigned long length) { strncpy(to,from,length); return length; }
        void escape_string(std::string& str);

        // must be called before first query in thread (one time for thread using one from existed Database objects)
        virtual void ThreadStart();
        // must be called before finish thread run (one time for thread using one from existed Database objects)
        virtual void ThreadEnd();

        // sets the result queue of the current thread, be careful what thread you call this from
        void SetResultQueue(SqlResultQueue * queue);

    private:
        bool m_logSQL;
        std::string m_logsDir;
};
#endif