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

Revision 102, 8.9 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#ifdef DO_POSTGRESQL
22
23#include "Util.h"
24#include "Policies/SingletonImp.h"
25#include "Platform/Define.h"
26#include "../src/zthread/ThreadImpl.h"
27#include "DatabaseEnv.h"
28#include "Database/PGSQLDelayThread.h"
29#include "Database/SqlOperations.h"
30#include "Timer.h"
31
32void DatabasePostgre::ThreadStart()
33{
34}
35
36void DatabasePostgre::ThreadEnd()
37{
38}
39
40size_t DatabasePostgre::db_count = 0;
41
42DatabasePostgre::DatabasePostgre() : Database(), mPGconn(NULL)
43{
44    // before first connection
45    if( db_count++ == 0 )
46    {
47
48        if (!PQisthreadsafe())
49        {
50            sLog.outError("FATAL ERROR: PostgreSQL libpq isn't thread-safe.");
51            exit(1);
52        }
53    }
54}
55
56DatabasePostgre::~DatabasePostgre()
57{
58
59    if (m_delayThread)
60        HaltDelayThread();
61
62    if( mPGconn )
63    {
64        PQfinish(mPGconn);
65        mPGconn = NULL;
66    }
67}
68
69bool DatabasePostgre::Initialize(const char *infoString)
70{
71    if(!Database::Initialize(infoString))
72        return false;
73
74    tranThread = NULL;
75
76    InitDelayThread();
77
78    Tokens tokens = StrSplit(infoString, ";");
79
80    Tokens::iterator iter;
81
82    std::string host, port_or_socket, user, password, database;
83
84    iter = tokens.begin();
85
86    if(iter != tokens.end())
87        host = *iter++;
88    if(iter != tokens.end())
89        port_or_socket = *iter++;
90    if(iter != tokens.end())
91        user = *iter++;
92    if(iter != tokens.end())
93        password = *iter++;
94    if(iter != tokens.end())
95        database = *iter++;
96
97    mPGconn = PQsetdbLogin(host.c_str(), port_or_socket.c_str(), NULL, NULL, database.c_str(), user.c_str(), password.c_str());
98
99    /* check to see that the backend connection was successfully made */
100    if (PQstatus(mPGconn) != CONNECTION_OK)
101    {
102        sLog.outError( "Could not connect to Postgre database at %s: %s",
103            host.c_str(), PQerrorMessage(mPGconn));
104        PQfinish(mPGconn);
105        return false;
106    }
107    else
108    {
109        sLog.outDetail( "Connected to Postgre database at %s",
110            host.c_str());
111        sLog.outString( "PostgreSQL server ver: %d",PQserverVersion(mPGconn));
112        return true;
113    }
114
115}
116
117QueryResult* DatabasePostgre::Query(const char *sql)
118{
119    if (!mPGconn)
120        return 0;
121
122    uint64 rowCount = 0;
123    uint32 fieldCount = 0;
124
125    // guarded block for thread-safe request
126    ZThread::Guard<ZThread::FastMutex> query_connection_guard(mMutex);
127    #ifdef TRINITY_DEBUG
128    uint32 _s = getMSTime();
129    #endif
130    // Send the query
131    PGresult * result = PQexec(mPGconn, sql);
132    if (!result )
133    {
134        return NULL;
135    }
136
137    if (PQresultStatus(result) != PGRES_TUPLES_OK)
138    {
139        sLog.outErrorDb( "SQL : %s", sql );
140        sLog.outErrorDb( "SQL %s", PQerrorMessage(mPGconn));
141        PQclear(result);
142        return NULL;
143    }
144    else
145    {
146        #ifdef TRINITY_DEBUG
147        sLog.outDebug("[%u ms] SQL: %s", getMSTime() - _s, sql );
148        #endif
149    }
150
151    rowCount = PQntuples(result);
152    fieldCount = PQnfields(result);
153    // end guarded block
154
155    if (!rowCount)
156    {
157        PQclear(result);
158        return NULL;
159    }
160
161    QueryResultPostgre * queryResult = new QueryResultPostgre(result, rowCount, fieldCount);
162    queryResult->NextRow();
163
164    return queryResult;
165}
166
167bool DatabasePostgre::Execute(const char *sql)
168{
169
170    if (!mPGconn)
171        return false;
172
173    // don't use queued execution if it has not been initialized
174    if (!m_threadBody) return DirectExecute(sql);
175
176    tranThread = ZThread::ThreadImpl::current();            // owner of this transaction
177    TransactionQueues::iterator i = m_tranQueues.find(tranThread);
178    if (i != m_tranQueues.end() && i->second != NULL)
179    {                                                       // Statement for transaction
180        i->second->DelayExecute(sql);
181    }
182    else
183    {
184        // Simple sql statement
185        m_threadBody->Delay(new SqlStatement(sql));
186    }
187
188    return true;
189}
190
191bool DatabasePostgre::DirectExecute(const char* sql)
192{
193    if (!mPGconn)
194        return false;
195    {
196        // guarded block for thread-safe  request
197        ZThread::Guard<ZThread::FastMutex> query_connection_guard(mMutex);
198        #ifdef TRINITY_DEBUG
199        uint32 _s = getMSTime();
200        #endif
201        PGresult *res = PQexec(mPGconn, sql);
202        if (PQresultStatus(res) != PGRES_COMMAND_OK)
203        {
204            sLog.outErrorDb( "SQL: %s", sql );
205            sLog.outErrorDb( "SQL %s", PQerrorMessage(mPGconn) );
206            return false;
207        }
208        else
209        {
210            #ifdef TRINITY_DEBUG
211            sLog.outDebug("[%u ms] SQL: %s", getMSTime() - _s, sql );
212            #endif
213        }
214        PQclear(res);
215
216        // end guarded block
217    }
218    return true;
219}
220
221bool DatabasePostgre::_TransactionCmd(const char *sql)
222{
223    if (!mPGconn)
224        return false;
225
226    PGresult *res = PQexec(mPGconn, sql);
227    if (PQresultStatus(res) != PGRES_COMMAND_OK)
228    {
229        sLog.outError("SQL: %s", sql);
230        sLog.outError("SQL ERROR: %s", PQerrorMessage(mPGconn));
231        return false;
232    }
233    else
234    {
235        DEBUG_LOG("SQL: %s", sql);
236    }
237    return true;
238}
239
240bool DatabasePostgre::BeginTransaction()
241{
242    if (!mPGconn)
243        return false;
244    // don't use queued execution if it has not been initialized
245    if (!m_threadBody)
246    {
247        if (tranThread==ZThread::ThreadImpl::current())
248            return false;                                   // huh? this thread already started transaction
249        mMutex.acquire();
250        if (!_TransactionCmd("START TRANSACTION"))
251        {
252            mMutex.release();                               // can't start transaction
253            return false;
254        }
255        return true;
256    }
257    // transaction started
258    tranThread = ZThread::ThreadImpl::current();            // owner of this transaction
259    TransactionQueues::iterator i = m_tranQueues.find(tranThread);
260    if (i != m_tranQueues.end() && i->second != NULL)
261        // If for thread exists queue and also contains transaction
262        // delete that transaction (not allow trans in trans)
263        delete i->second;
264
265    m_tranQueues[tranThread] = new SqlTransaction();
266
267    return true;
268}
269
270bool DatabasePostgre::CommitTransaction()
271{
272    if (!mPGconn)
273        return false;
274
275    // don't use queued execution if it has not been initialized
276    if (!m_threadBody)
277    {
278        if (tranThread!=ZThread::ThreadImpl::current())
279            return false;
280        bool _res = _TransactionCmd("COMMIT");
281        tranThread = NULL;
282        mMutex.release();
283        return _res;
284    }
285    tranThread = ZThread::ThreadImpl::current();
286    TransactionQueues::iterator i = m_tranQueues.find(tranThread);
287    if (i != m_tranQueues.end() && i->second != NULL)
288    {
289        m_threadBody->Delay(i->second);
290        i->second = NULL;
291        return true;
292    }
293    else
294        return false;
295}
296
297bool DatabasePostgre::RollbackTransaction()
298{
299    if (!mPGconn)
300        return false;
301    // don't use queued execution if it has not been initialized
302    if (!m_threadBody)
303    {
304        if (tranThread!=ZThread::ThreadImpl::current())
305            return false;
306        bool _res = _TransactionCmd("ROLLBACK");
307        tranThread = NULL;
308        mMutex.release();
309        return _res;
310    }
311    tranThread = ZThread::ThreadImpl::current();
312    TransactionQueues::iterator i = m_tranQueues.find(tranThread);
313    if (i != m_tranQueues.end() && i->second != NULL)
314    {
315        delete i->second;
316        i->second = NULL;
317    }
318    return true;
319}
320
321unsigned long DatabasePostgre::escape_string(char *to, const char *from, unsigned long length)
322{
323    if (!mPGconn || !to || !from || !length)
324        return 0;
325
326    return PQescapeString(to, from, length);
327}
328
329void DatabasePostgre::InitDelayThread()
330{
331    assert(!m_delayThread);
332
333    //New delay thread for delay execute
334    m_delayThread = new ZThread::Thread(m_threadBody = new PGSQLDelayThread(this));
335}
336
337void DatabasePostgre::HaltDelayThread()
338{
339    if (!m_threadBody || !m_delayThread) return;
340
341    m_threadBody->Stop();                                   //Stop event
342    m_delayThread->wait();                                  //Wait for flush to DB
343    delete m_delayThread;                                   //This also deletes m_threadBody
344    m_delayThread = NULL;
345    m_threadBody = NULL;
346}
347#endif
Note: See TracBrowser for help on using the browser.