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 __SQLOPERATIONS_H |
---|
20 | #define __SQLOPERATIONS_H |
---|
21 | |
---|
22 | #include "Common.h" |
---|
23 | |
---|
24 | #include "zthread/LockedQueue.h" |
---|
25 | #include "zthread/FastMutex.h" |
---|
26 | #include "zthread/Thread.h" |
---|
27 | #include <queue> |
---|
28 | #include "Utilities/Callback.h" |
---|
29 | |
---|
30 | /// ---- BASE --- |
---|
31 | |
---|
32 | class Database; |
---|
33 | class SqlDelayThread; |
---|
34 | |
---|
35 | class SqlOperation |
---|
36 | { |
---|
37 | public: |
---|
38 | virtual void OnRemove() { delete this; } |
---|
39 | virtual void Execute(Database *db) = 0; |
---|
40 | virtual ~SqlOperation() {} |
---|
41 | }; |
---|
42 | |
---|
43 | /// ---- ASYNC STATEMENTS / TRANSACTIONS ---- |
---|
44 | |
---|
45 | class SqlStatement : public SqlOperation |
---|
46 | { |
---|
47 | private: |
---|
48 | const char *m_sql; |
---|
49 | public: |
---|
50 | SqlStatement(const char *sql) : m_sql(strdup(sql)){} |
---|
51 | ~SqlStatement() { void* tofree = const_cast<char*>(m_sql); free(tofree); } |
---|
52 | void Execute(Database *db); |
---|
53 | }; |
---|
54 | |
---|
55 | class SqlTransaction : public SqlOperation |
---|
56 | { |
---|
57 | private: |
---|
58 | std::queue<const char *> m_queue; |
---|
59 | public: |
---|
60 | SqlTransaction() {} |
---|
61 | void DelayExecute(const char *sql) { m_queue.push(strdup(sql)); } |
---|
62 | void Execute(Database *db); |
---|
63 | }; |
---|
64 | |
---|
65 | /// ---- ASYNC QUERIES ---- |
---|
66 | |
---|
67 | class SqlQuery; /// contains a single async query |
---|
68 | class QueryResult; /// the result of one |
---|
69 | class SqlResultQueue; /// queue for thread sync |
---|
70 | class SqlQueryHolder; /// groups several async quries |
---|
71 | class SqlQueryHolderEx; /// points to a holder, added to the delay thread |
---|
72 | |
---|
73 | class SqlResultQueue : public ZThread::LockedQueue<MaNGOS::IQueryCallback*, ZThread::FastMutex> |
---|
74 | { |
---|
75 | public: |
---|
76 | SqlResultQueue() {} |
---|
77 | void Update(); |
---|
78 | }; |
---|
79 | |
---|
80 | class SqlQuery : public SqlOperation |
---|
81 | { |
---|
82 | private: |
---|
83 | const char *m_sql; |
---|
84 | MaNGOS::IQueryCallback * m_callback; |
---|
85 | SqlResultQueue * m_queue; |
---|
86 | public: |
---|
87 | SqlQuery(const char *sql, MaNGOS::IQueryCallback * callback, SqlResultQueue * queue) |
---|
88 | : m_sql(strdup(sql)), m_callback(callback), m_queue(queue) {} |
---|
89 | ~SqlQuery() { void* tofree = const_cast<char*>(m_sql); free(tofree); } |
---|
90 | void Execute(Database *db); |
---|
91 | }; |
---|
92 | |
---|
93 | class SqlQueryHolder |
---|
94 | { |
---|
95 | friend class SqlQueryHolderEx; |
---|
96 | private: |
---|
97 | typedef std::pair<const char*, QueryResult*> SqlResultPair; |
---|
98 | std::vector<SqlResultPair> m_queries; |
---|
99 | public: |
---|
100 | SqlQueryHolder() {} |
---|
101 | ~SqlQueryHolder(); |
---|
102 | bool SetQuery(size_t index, const char *sql); |
---|
103 | bool SetPQuery(size_t index, const char *format, ...) ATTR_PRINTF(3,4); |
---|
104 | void SetSize(size_t size); |
---|
105 | QueryResult* GetResult(size_t index); |
---|
106 | void SetResult(size_t index, QueryResult *result); |
---|
107 | void Execute(MaNGOS::IQueryCallback * callback, SqlDelayThread *thread, SqlResultQueue *queue); |
---|
108 | }; |
---|
109 | |
---|
110 | class SqlQueryHolderEx : public SqlOperation |
---|
111 | { |
---|
112 | private: |
---|
113 | SqlQueryHolder * m_holder; |
---|
114 | MaNGOS::IQueryCallback * m_callback; |
---|
115 | SqlResultQueue * m_queue; |
---|
116 | public: |
---|
117 | SqlQueryHolderEx(SqlQueryHolder *holder, MaNGOS::IQueryCallback * callback, SqlResultQueue * queue) |
---|
118 | : m_holder(holder), m_callback(callback), m_queue(queue) {} |
---|
119 | void Execute(Database *db); |
---|
120 | }; |
---|
121 | #endif //__SQLOPERATIONS_H |
---|