[7017] Updated copyright notice for new year
[getmangos.git] / src / shared / Database / DatabasePostgre.cpp
bloba572a8320b063557d9c323a943bd1557fb1b4500
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
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.
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.
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
19 #ifdef DO_POSTGRESQL
21 #include "Util.h"
22 #include "Policies/SingletonImp.h"
23 #include "Platform/Define.h"
24 #include "../src/zthread/ThreadImpl.h"
25 #include "DatabaseEnv.h"
26 #include "Database/PGSQLDelayThread.h"
27 #include "Database/SqlOperations.h"
28 #include "Timer.h"
30 void DatabasePostgre::ThreadStart()
34 void DatabasePostgre::ThreadEnd()
38 size_t DatabasePostgre::db_count = 0;
40 DatabasePostgre::DatabasePostgre() : Database(), mPGconn(NULL)
42 // before first connection
43 if( db_count++ == 0 )
46 if (!PQisthreadsafe())
48 sLog.outError("FATAL ERROR: PostgreSQL libpq isn't thread-safe.");
49 exit(1);
54 DatabasePostgre::~DatabasePostgre()
57 if (m_delayThread)
58 HaltDelayThread();
60 if( mPGconn )
62 PQfinish(mPGconn);
63 mPGconn = NULL;
67 bool DatabasePostgre::Initialize(const char *infoString)
69 if(!Database::Initialize(infoString))
70 return false;
72 tranThread = NULL;
74 InitDelayThread();
76 Tokens tokens = StrSplit(infoString, ";");
78 Tokens::iterator iter;
80 std::string host, port_or_socket, user, password, database;
82 iter = tokens.begin();
84 if(iter != tokens.end())
85 host = *iter++;
86 if(iter != tokens.end())
87 port_or_socket = *iter++;
88 if(iter != tokens.end())
89 user = *iter++;
90 if(iter != tokens.end())
91 password = *iter++;
92 if(iter != tokens.end())
93 database = *iter++;
95 mPGconn = PQsetdbLogin(host.c_str(), port_or_socket.c_str(), NULL, NULL, database.c_str(), user.c_str(), password.c_str());
97 /* check to see that the backend connection was successfully made */
98 if (PQstatus(mPGconn) != CONNECTION_OK)
100 sLog.outError( "Could not connect to Postgre database at %s: %s",
101 host.c_str(), PQerrorMessage(mPGconn));
102 PQfinish(mPGconn);
103 return false;
105 else
107 sLog.outDetail( "Connected to Postgre database at %s",
108 host.c_str());
109 sLog.outString( "PostgreSQL server ver: %d",PQserverVersion(mPGconn));
110 return true;
115 QueryResult* DatabasePostgre::Query(const char *sql)
117 if (!mPGconn)
118 return 0;
120 uint64 rowCount = 0;
121 uint32 fieldCount = 0;
123 // guarded block for thread-safe request
124 ZThread::Guard<ZThread::FastMutex> query_connection_guard(mMutex);
125 #ifdef MANGOS_DEBUG
126 uint32 _s = getMSTime();
127 #endif
128 // Send the query
129 PGresult * result = PQexec(mPGconn, sql);
130 if (!result )
132 return NULL;
135 if (PQresultStatus(result) != PGRES_TUPLES_OK)
137 sLog.outErrorDb( "SQL : %s", sql );
138 sLog.outErrorDb( "SQL %s", PQerrorMessage(mPGconn));
139 PQclear(result);
140 return NULL;
142 else
144 #ifdef MANGOS_DEBUG
145 sLog.outDebug("[%u ms] SQL: %s", getMSTime() - _s, sql );
146 #endif
149 rowCount = PQntuples(result);
150 fieldCount = PQnfields(result);
151 // end guarded block
153 if (!rowCount)
155 PQclear(result);
156 return NULL;
159 QueryResultPostgre * queryResult = new QueryResultPostgre(result, rowCount, fieldCount);
160 queryResult->NextRow();
162 return queryResult;
165 bool DatabasePostgre::Execute(const char *sql)
168 if (!mPGconn)
169 return false;
171 // don't use queued execution if it has not been initialized
172 if (!m_threadBody) return DirectExecute(sql);
174 tranThread = ZThread::ThreadImpl::current(); // owner of this transaction
175 TransactionQueues::iterator i = m_tranQueues.find(tranThread);
176 if (i != m_tranQueues.end() && i->second != NULL)
177 { // Statement for transaction
178 i->second->DelayExecute(sql);
180 else
182 // Simple sql statement
183 m_threadBody->Delay(new SqlStatement(sql));
186 return true;
189 bool DatabasePostgre::DirectExecute(const char* sql)
191 if (!mPGconn)
192 return false;
194 // guarded block for thread-safe request
195 ZThread::Guard<ZThread::FastMutex> query_connection_guard(mMutex);
196 #ifdef MANGOS_DEBUG
197 uint32 _s = getMSTime();
198 #endif
199 PGresult *res = PQexec(mPGconn, sql);
200 if (PQresultStatus(res) != PGRES_COMMAND_OK)
202 sLog.outErrorDb( "SQL: %s", sql );
203 sLog.outErrorDb( "SQL %s", PQerrorMessage(mPGconn) );
204 return false;
206 else
208 #ifdef MANGOS_DEBUG
209 sLog.outDebug("[%u ms] SQL: %s", getMSTime() - _s, sql );
210 #endif
212 PQclear(res);
214 // end guarded block
216 return true;
219 bool DatabasePostgre::_TransactionCmd(const char *sql)
221 if (!mPGconn)
222 return false;
224 PGresult *res = PQexec(mPGconn, sql);
225 if (PQresultStatus(res) != PGRES_COMMAND_OK)
227 sLog.outError("SQL: %s", sql);
228 sLog.outError("SQL ERROR: %s", PQerrorMessage(mPGconn));
229 return false;
231 else
233 DEBUG_LOG("SQL: %s", sql);
235 return true;
238 bool DatabasePostgre::BeginTransaction()
240 if (!mPGconn)
241 return false;
242 // don't use queued execution if it has not been initialized
243 if (!m_threadBody)
245 if (tranThread==ZThread::ThreadImpl::current())
246 return false; // huh? this thread already started transaction
247 mMutex.acquire();
248 if (!_TransactionCmd("START TRANSACTION"))
250 mMutex.release(); // can't start transaction
251 return false;
253 return true;
255 // transaction started
256 tranThread = ZThread::ThreadImpl::current(); // owner of this transaction
257 TransactionQueues::iterator i = m_tranQueues.find(tranThread);
258 if (i != m_tranQueues.end() && i->second != NULL)
259 // If for thread exists queue and also contains transaction
260 // delete that transaction (not allow trans in trans)
261 delete i->second;
263 m_tranQueues[tranThread] = new SqlTransaction();
265 return true;
268 bool DatabasePostgre::CommitTransaction()
270 if (!mPGconn)
271 return false;
273 // don't use queued execution if it has not been initialized
274 if (!m_threadBody)
276 if (tranThread!=ZThread::ThreadImpl::current())
277 return false;
278 bool _res = _TransactionCmd("COMMIT");
279 tranThread = NULL;
280 mMutex.release();
281 return _res;
283 tranThread = ZThread::ThreadImpl::current();
284 TransactionQueues::iterator i = m_tranQueues.find(tranThread);
285 if (i != m_tranQueues.end() && i->second != NULL)
287 m_threadBody->Delay(i->second);
288 i->second = NULL;
289 return true;
291 else
292 return false;
295 bool DatabasePostgre::RollbackTransaction()
297 if (!mPGconn)
298 return false;
299 // don't use queued execution if it has not been initialized
300 if (!m_threadBody)
302 if (tranThread!=ZThread::ThreadImpl::current())
303 return false;
304 bool _res = _TransactionCmd("ROLLBACK");
305 tranThread = NULL;
306 mMutex.release();
307 return _res;
309 tranThread = ZThread::ThreadImpl::current();
310 TransactionQueues::iterator i = m_tranQueues.find(tranThread);
311 if (i != m_tranQueues.end() && i->second != NULL)
313 delete i->second;
314 i->second = NULL;
316 return true;
319 unsigned long DatabasePostgre::escape_string(char *to, const char *from, unsigned long length)
321 if (!mPGconn || !to || !from || !length)
322 return 0;
324 return PQescapeString(to, from, length);
327 void DatabasePostgre::InitDelayThread()
329 assert(!m_delayThread);
331 //New delay thread for delay execute
332 m_delayThread = new ZThread::Thread(m_threadBody = new PGSQLDelayThread(this));
335 void DatabasePostgre::HaltDelayThread()
337 if (!m_threadBody || !m_delayThread) return;
339 m_threadBody->Stop(); //Stop event
340 m_delayThread->wait(); //Wait for flush to DB
341 delete m_delayThread; //This also deletes m_threadBody
342 m_delayThread = NULL;
343 m_threadBody = NULL;
345 #endif