]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
Members initialization order fixed in PostgreSQL storage plugin
[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       retries(3),
82       connection(NULL)
83 {
84 pthread_mutex_init(&mutex, NULL);
85 }
86 //-----------------------------------------------------------------------------
87 POSTGRESQL_STORE::~POSTGRESQL_STORE()
88 {
89 if (connection)
90     {
91     PQfinish(connection);
92     }
93 pthread_mutex_destroy(&mutex);
94 }
95 //-----------------------------------------------------------------------------
96 int POSTGRESQL_STORE::ParseSettings()
97 {
98 std::vector<PARAM_VALUE>::iterator i;
99 string s;
100
101 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
102     {
103     s = i->param;
104     std::transform(s.begin(), s.end(), s.begin(), ToLower());
105     if (s == "server")
106         {
107         server = *(i->value.begin());
108         }
109     if (s == "database")
110         {
111         database = *(i->value.begin());
112         }
113     if (s == "user")
114         {
115         user = *(i->value.begin());
116         }
117     if (s == "password")
118         {
119         password = *(i->value.begin());
120         }
121     if (s == "retries")
122         {
123         if (str2x(*(i->value.begin()), retries))
124             {
125             strError = "Invalid 'retries' value";
126             printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
127             return -1;
128             }
129         }
130     }
131
132 clientEncoding = "KOI8";
133
134 return Connect();
135 }
136 //-----------------------------------------------------------------------------
137 int POSTGRESQL_STORE::Connect()
138 {
139 std::string params;
140 params = "host=" + server + " "
141        + "dbname=" + database + " "
142        + "user=" + user + " "
143        + "password=" + password;
144
145 connection = PQconnectdb(params.c_str());
146
147 if (PQstatus(connection) != CONNECTION_OK)
148     {
149     strError = PQerrorMessage(connection);
150     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
151     // Will try to connect later
152     return 0;
153     }
154
155 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
156     {
157     strError = PQerrorMessage(connection);
158     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
159     return 1;
160     }
161
162 return CheckVersion();
163 }
164 //-----------------------------------------------------------------------------
165 int POSTGRESQL_STORE::Reset() const
166 {
167 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
168     {
169     struct timespec ts = {1, 0};
170     nanosleep(&ts, NULL);
171     PQreset(connection);
172     }
173
174 if (PQstatus(connection) != CONNECTION_OK)
175     {
176     strError = PQerrorMessage(connection);
177     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
178     return 1;
179     }
180
181 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
182     {
183     strError = PQerrorMessage(connection);
184     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
185     return -1;
186     }
187
188 return CheckVersion();
189 }
190 //-----------------------------------------------------------------------------
191 int POSTGRESQL_STORE::CheckVersion() const
192 {
193
194 if (StartTransaction())
195     {
196     strError = "Failed to start transaction";
197     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
198     return -1;
199     }
200
201 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
202
203 if (PQresultStatus(result) != PGRES_TUPLES_OK)
204     {
205     strError = PQresultErrorMessage(result);
206     PQclear(result);
207     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
208     RollbackTransaction();
209     return -1;
210     }
211
212 if (str2x(PQgetvalue(result, 0, 0), version))
213     {
214     strError = "Invalid DB version";
215     PQclear(result);
216     RollbackTransaction();
217     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
218     return -1;
219     }
220
221 PQclear(result);
222
223 if (version < DB_MIN_VERSION)
224     {
225     strError = "DB version too old";
226     RollbackTransaction();
227     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
228     return -1;
229     }
230
231 if (version < 6)
232     {
233     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);
234     }
235
236 if (CommitTransaction())
237     {
238     strError = "Failed to commit transaction";
239     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
240     return -1;
241     }
242
243 return 0;
244 }
245 //-----------------------------------------------------------------------------