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

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

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05: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])",index,m_queries.size(),m_queries[index].first,sql);
106        return false;
107    }
108
109    /// not executed yet, just stored (it's not called a holder for nothing)
110    m_queries[index] = SqlResultPair(strdup(sql), NULL);
111    return true;
112}
113
114bool SqlQueryHolder::SetPQuery(size_t index, const char *format, ...)
115{
116    if(!format)
117    {
118        sLog.outError("Query (index: %u) is empty.",index);
119        return false;
120    }
121
122    va_list ap;
123    char szQuery [MAX_QUERY_LEN];
124    va_start(ap, format);
125    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
126    va_end(ap);
127
128    if(res==-1)
129    {
130        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
131        return false;
132    }
133
134    return SetQuery(index,szQuery);
135}
136
137QueryResult* SqlQueryHolder::GetResult(size_t index)
138{
139    if(index < m_queries.size())
140    {
141        /// the query strings are freed on the first GetResult or in the destructor
142        if(m_queries[index].first != NULL)
143        {
144            free((void*)(const_cast<char*>(m_queries[index].first)));
145            m_queries[index].first = NULL;
146        }
147        /// when you get a result aways remember to delete it!
148        return m_queries[index].second;
149    }
150    else
151        return NULL;
152}
153
154void SqlQueryHolder::SetResult(size_t index, QueryResult *result)
155{
156    /// store the result in the holder
157    if(index < m_queries.size())
158        m_queries[index].second = result;
159}
160
161SqlQueryHolder::~SqlQueryHolder()
162{
163    for(size_t i = 0; i < m_queries.size(); i++)
164    {
165        /// if the result was never used, free the resources
166        /// results used already (getresult called) are expected to be deleted
167        if(m_queries[i].first != NULL)
168        {
169            free((void*)(const_cast<char*>(m_queries[i].first)));
170            if(m_queries[i].second)
171                delete m_queries[i].second;
172        }
173    }
174}
175
176void SqlQueryHolder::SetSize(size_t size)
177{
178    /// to optimize push_back, reserve the number of queries about to be executed
179    m_queries.resize(size);
180}
181
182void SqlQueryHolderEx::Execute(Database *db)
183{
184    if(!m_holder || !m_callback || !m_queue)
185        return;
186
187    /// we can do this, we are friends
188    std::vector<SqlQueryHolder::SqlResultPair> &queries = m_holder->m_queries;
189
190    for(size_t i = 0; i < queries.size(); i++)
191    {
192        /// execute all queries in the holder and pass the results
193        char const *sql = queries[i].first;
194        if(sql) m_holder->SetResult(i, db->Query(sql));
195    }
196
197    /// sync with the caller thread
198    m_queue->add(m_callback);
199}
Note: See TracBrowser for help on using the browser.