]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
Fix postgresql store plugin compilation errors
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  This file contains a realization of a base postgresql-storage plugin class
23  *
24  *  v. 1.3
25  *  FreeMb logging on disconnects added
26  *
27  *  v. 1.2
28  *  Reconnection on faults added
29  *
30  *  v. 1.1
31  *  tb_stats removed
32  *
33  *  v. 1.0
34  *  Initial implementation
35  *
36  *  $Revision: 1.5 $
37  *  $Date: 2010/01/06 10:43:48 $
38  *
39  */
40
41 #include <string>
42 #include <vector>
43 #include <algorithm>
44
45 #include <libpq-fe.h>
46
47 #include "postgresql_store.h"
48 #include "postgresql_store_utils.h"
49 #include "stg/module_settings.h"
50
51 class POSTGRESQL_STORE_CREATOR
52 {
53 public:
54     POSTGRESQL_STORE_CREATOR()
55         : pqStore(new POSTGRESQL_STORE())
56         {
57         };
58     ~POSTGRESQL_STORE_CREATOR()
59         {
60         delete pqStore;
61         };
62     POSTGRESQL_STORE * GetStore() { return pqStore; };
63 private:
64     POSTGRESQL_STORE * pqStore;
65 } pqStoreeCreator;
66
67 //-----------------------------------------------------------------------------
68 STORE * GetStore()
69 {
70 return pqStoreeCreator.GetStore();
71 }
72
73 //-----------------------------------------------------------------------------
74 POSTGRESQL_STORE::POSTGRESQL_STORE()
75     : versionString("postgresql_store v.1.3"),
76       server("localhost"),
77       database("stargazer"),
78       user("stg"),
79       password("123456"),
80       version(0),
81       connection(NULL)
82 {
83 pthread_mutex_init(&mutex, NULL);
84 }
85 //-----------------------------------------------------------------------------
86 POSTGRESQL_STORE::~POSTGRESQL_STORE()
87 {
88 if (connection)
89     {
90     PQfinish(connection);
91     }
92 pthread_mutex_destroy(&mutex);
93 }
94 //-----------------------------------------------------------------------------
95 int POSTGRESQL_STORE::ParseSettings()
96 {
97 std::vector<PARAM_VALUE>::iterator i;
98 string s;
99
100 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
101     {
102     s = i->param;
103     std::transform(s.begin(), s.end(), s.begin(), ToLower());
104     if (s == "server")
105         {
106         server = *(i->value.begin());
107         }
108     if (s == "database")
109         {
110         database = *(i->value.begin());
111         }
112     if (s == "user")
113         {
114         user = *(i->value.begin());
115         }
116     if (s == "password")
117         {
118         password = *(i->value.begin());
119         }
120     }
121
122 clientEncoding = "KOI8";
123
124 return Connect();
125 }
126 //-----------------------------------------------------------------------------
127 int POSTGRESQL_STORE::Connect()
128 {
129 std::string params;
130 params = "host=" + server + " "
131        + "dbname=" + database + " "
132        + "user=" + user + " "
133        + "password=" + password;
134
135 connection = PQconnectdb(params.c_str());
136
137 if (PQstatus(connection) != CONNECTION_OK)
138     {
139     strError = PQerrorMessage(connection);
140     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
141     return 1;
142     }
143
144 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
145     {
146     strError = PQerrorMessage(connection);
147     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
148     return 1;
149     }
150
151 return CheckVersion();
152 }
153 //-----------------------------------------------------------------------------
154 int POSTGRESQL_STORE::Reset() const
155 {
156 PQreset(connection);
157
158 if (PQstatus(connection) != CONNECTION_OK)
159     {
160     strError = PQerrorMessage(connection);
161     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
162     return 1;
163     }
164
165 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
166     {
167     strError = PQerrorMessage(connection);
168     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
169     return 1;
170     }
171
172 return CheckVersion();
173 }
174 //-----------------------------------------------------------------------------
175 int POSTGRESQL_STORE::CheckVersion() const
176 {
177
178 if (StartTransaction())
179     {
180     strError = "Failed to start transaction";
181     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
182     return -1;
183     }
184
185 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
186
187 if (PQresultStatus(result) != PGRES_TUPLES_OK)
188     {
189     strError = PQresultErrorMessage(result);
190     PQclear(result);
191     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
192     RollbackTransaction();
193     return -1;
194     }
195
196 if (str2x(PQgetvalue(result, 0, 0), version))
197     {
198     strError = "Invalid DB version";
199     PQclear(result);
200     RollbackTransaction();
201     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
202     return -1;
203     }
204
205 PQclear(result);
206
207 if (version < DB_MIN_VERSION)
208     {
209     strError = "DB version too old";
210     RollbackTransaction();
211     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
212     return -1;
213     }
214
215 if (version < 6)
216     {
217     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): I recommend you to upgrade your DB to higher version to support FreeMb logging on disconnect. Current version is %d\n", version);
218     }
219
220 if (CommitTransaction())
221     {
222     strError = "Failed to commit transaction";
223     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
224     return -1;
225     }
226
227 return 0;
228 }
229 //-----------------------------------------------------------------------------