root/trunk/src/shared/Database/SqlOperations.cpp @ 230

Revision 230, 5.6 kB (checked in by yumileroy, 17 years ago)

[svn] *** Source: MaNGOS ***
* Implement localization of creature/gameobject name that say/yell. Author: evilstar (rewrited by: Vladimir)
* Fix auth login queue. Author: Derex
* Allowed switching INVTYPE_HOLDABLE items during combat, used correct spells for triggering global cooldown at weapon switch. Author: mobel/simak
* Fixed some format arg type/value pairs. Other warnings. Author: Vladimir
* [238_world.sql] Allow have team dependent graveyards at entrance map for instances. Author: Vladimir

NOTE:
Entrance map graveyards selected by same way as local (by distance from entrance) Until DB support will work in old way base at current DB data.

Original author: visagalis
Date: 2008-11-14 17:03:03-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#include "SqlOperations.h"
22#include "SqlDelayThread.h"
23#include "DatabaseEnv.h"
24#include "DatabaseImpl.h"
25
26/// ---- ASYNC STATEMENTS / TRANSACTIONS ----
27
28void SqlStatement::Execute(Database *db)
29{
30    /// just do it
31    db->DirectExecute(m_sql);
32}
33
34void SqlTransaction::Execute(Database *db)
35{
36    if(m_queue.empty())
37        return;
38    db->DirectExecute("START TRANSACTION");
39    while(!m_queue.empty())
40    {
41        char const *sql = m_queue.front();
42        m_queue.pop();
43
44        if(!db->DirectExecute(sql))
45        {
46            free((void*)const_cast<char*>(sql));
47            db->DirectExecute("ROLLBACK");
48            while(!m_queue.empty())
49            {
50                free((void*)const_cast<char*>(m_queue.front()));
51                m_queue.pop();
52            }
53            return;
54        }
55
56        free((void*)const_cast<char*>(sql));
57    }
58    db->DirectExecute("COMMIT");
59}
60
61/// ---- ASYNC QUERIES ----
62
63void SqlQuery::Execute(Database *db)
64{
65    if(!m_callback || !m_queue)
66        return;
67    /// execute the query and store the result in the callback
68    m_callback->SetResult(db->Query(m_sql));
69    /// add the callback to the sql result queue of the thread it originated from
70    m_queue->add(m_callback);
71}
72
73void SqlResultQueue::Update()
74{
75    /// execute the callbacks waiting in the synchronization queue
76    while(!empty())
77    {
78        Trinity::IQueryCallback * callback = next();
79        callback->Execute();
80        delete callback;
81    }
82}
83
84void SqlQueryHolder::Execute(Trinity::IQueryCallback * callback, SqlDelayThread *thread, SqlResultQueue *queue)
85{
86    if(!callback || !thread || !queue)
87        return;
88
89    /// delay the execution of the queries, sync them with the delay thread
90    /// which will in turn resync on execution (via the queue) and call back
91    SqlQueryHolderEx *holderEx = new SqlQueryHolderEx(this, callback, queue);
92    thread->Delay(holderEx);
93}
94
95bool SqlQueryHolder::SetQuery(size_t index, const char *sql)
96{
97    if(m_queries.size() <= index)
98    {
99        sLog.outError("Query index (%u) out of range (size: %u) for query: %s",index,m_queries.size(),sql);
100        return false;
101    }
102
103    if(m_queries[index].first != NULL)
104    {
105        sLog.outError("Attempt assign query to holder index (%u) where other query stored (Old: [%s] New: [%s])",
106            index,m_queries[index].first,sql);
107        return false;
108    }
109
110    /// not executed yet, just stored (it's not called a holder for nothing)
111    m_queries[index] = SqlResultPair(strdup(sql), NULL);
112    return true;
113}
114
115bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
116{
117    if(!format)
118    {
119        sLog.outError("Query (index: %u) is empty.",index);
120        return false;
121    }
122
123    va_list ap;
124    char szQuery [MAX_QUERY_LEN];
125    va_start(ap, format);
126    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
127    va_end(ap);
128
129    if(res==-1)
130    {
131        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
132        return false;
133    }
134
135    return SetQuery(index,szQuery);
136}
137
138QueryResult* SqlQueryHolder::GetResult(size_t index)
139{
140    if(index < m_queries.size())
141    {
142        /// the query strings are freed on the first GetResult or in the destructor
143        if(m_queries[index].first != NULL)
144        {
145            free((void*)(const_cast<char*>(m_queries[index].first)));
146            m_queries[index].first = NULL;
147        }
148        /// when you get a result aways remember to delete it!
149        return m_queries[index].second;
150    }
151    else
152        return NULL;
153}
154
155void SqlQueryHolder::SetResult(size_t index, QueryResult *result)
156{
157    /// store the result in the holder
158    if(index < m_queries.size())
159        m_queries[index].second = result;
160}
161
162SqlQueryHolder::~SqlQueryHolder()
163{
164    for(size_t i = 0; i < m_queries.size(); i++)
165    {
166        /// if the result was never used, free the resources
167        /// results used already (getresult called) are expected to be deleted
168        if(m_queries[i].first != NULL)
169        {
170            free((void*)(const_cast<char*>(m_queries[i].first)));
171            if(m_queries[i].second)
172                delete m_queries[i].second;
173        }
174    }
175}
176
177void SqlQueryHolder::SetSize(size_t size)
178{
179    /// to optimize push_back, reserve the number of queries about to be executed
180    m_queries.resize(size);
181}
182
183void SqlQueryHolderEx::Execute(Database *db)
184{
185    if(!m_holder || !m_callback || !m_queue)
186        return;
187
188    /// we can do this, we are friends
189    std::vector<SqlQueryHolder::SqlResultPair> &queries = m_holder->m_queries;
190
191    for(size_t i = 0; i < queries.size(); i++)
192    {
193        /// execute all queries in the holder and pass the results
194        char const *sql = queries[i].first;
195        if(sql) m_holder->SetResult(i, db->Query(sql));
196    }
197
198    /// sync with the caller thread
199    m_queue->add(m_callback);
200}
Note: See TracBrowser for help on using the browser.