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 _DatabasePostgre_H |
---|
22 | #define _DatabasePostgre_H |
---|
23 | |
---|
24 | #include "Policies/Singleton.h" |
---|
25 | #include "zthread/FastMutex.h" |
---|
26 | #include <stdarg.h> |
---|
27 | |
---|
28 | #ifdef WIN32 |
---|
29 | #define FD_SETSIZE 1024 |
---|
30 | #include <winsock2.h> |
---|
31 | #include <postgre/libpq-fe.h> |
---|
32 | #else |
---|
33 | #include <libpq-fe.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | class DatabasePostgre : public Database |
---|
37 | { |
---|
38 | friend class Trinity::OperatorNew<DatabasePostgre>; |
---|
39 | |
---|
40 | public: |
---|
41 | DatabasePostgre(); |
---|
42 | ~DatabasePostgre(); |
---|
43 | |
---|
44 | //! Initializes Postgres and connects to a server. |
---|
45 | /*! infoString should be formated like hostname;username;password;database. */ |
---|
46 | bool Initialize(const char *infoString); |
---|
47 | void InitDelayThread(); |
---|
48 | void HaltDelayThread(); |
---|
49 | QueryResult* Query(const char *sql); |
---|
50 | bool Execute(const char *sql); |
---|
51 | bool DirectExecute(const char* sql); |
---|
52 | bool BeginTransaction(); |
---|
53 | bool CommitTransaction(); |
---|
54 | bool RollbackTransaction(); |
---|
55 | |
---|
56 | operator bool () const { return mPGconn != NULL; } |
---|
57 | |
---|
58 | unsigned long escape_string(char *to, const char *from, unsigned long length); |
---|
59 | using Database::escape_string; |
---|
60 | |
---|
61 | // must be call before first query in thread |
---|
62 | void ThreadStart(); |
---|
63 | // must be call before finish thread run |
---|
64 | void ThreadEnd(); |
---|
65 | private: |
---|
66 | ZThread::FastMutex mMutex; |
---|
67 | ZThread::FastMutex tranMutex; |
---|
68 | |
---|
69 | ZThread::ThreadImpl* tranThread; |
---|
70 | |
---|
71 | PGconn *mPGconn; |
---|
72 | |
---|
73 | static size_t db_count; |
---|
74 | |
---|
75 | bool _TransactionCmd(const char *sql); |
---|
76 | }; |
---|
77 | #endif |
---|