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