]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
Hide or add proper copy ctor and assignement operator, initialize
[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 "stg/module_settings.h"
48 #include "stg/plugin_creator.h"
49 #include "postgresql_store_utils.h"
50 #include "postgresql_store.h"
51
52 PLUGIN_CREATOR<POSTGRESQL_STORE> pqStoreeCreator;
53
54 //-----------------------------------------------------------------------------
55 STORE * GetStore()
56 {
57 return pqStoreeCreator.GetPlugin();
58 }
59
60 //-----------------------------------------------------------------------------
61 POSTGRESQL_STORE::POSTGRESQL_STORE()
62     : versionString("postgresql_store v.1.3"),
63       strError(),
64       server("localhost"),
65       database("stargazer"),
66       user("stg"),
67       password("123456"),
68       clientEncoding("KOI8"),
69       settings(),
70       mutex(),
71       version(0),
72       retries(3),
73       connection(NULL)
74 {
75 pthread_mutex_init(&mutex, NULL);
76 }
77 //-----------------------------------------------------------------------------
78 POSTGRESQL_STORE::~POSTGRESQL_STORE()
79 {
80 if (connection)
81     {
82     PQfinish(connection);
83     }
84 pthread_mutex_destroy(&mutex);
85 }
86 //-----------------------------------------------------------------------------
87 int POSTGRESQL_STORE::ParseSettings()
88 {
89 std::vector<PARAM_VALUE>::iterator i;
90 string s;
91
92 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
93     {
94     s = i->param;
95     std::transform(s.begin(), s.end(), s.begin(), ToLower());
96     if (s == "server")
97         {
98         server = *(i->value.begin());
99         }
100     if (s == "database")
101         {
102         database = *(i->value.begin());
103         }
104     if (s == "user")
105         {
106         user = *(i->value.begin());
107         }
108     if (s == "password")
109         {
110         password = *(i->value.begin());
111         }
112     if (s == "retries")
113         {
114         if (str2x(*(i->value.begin()), retries))
115             {
116             strError = "Invalid 'retries' value";
117             printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
118             return -1;
119             }
120         }
121     }
122
123 clientEncoding = "KOI8";
124
125 return Connect();
126 }
127 //-----------------------------------------------------------------------------
128 int POSTGRESQL_STORE::Connect()
129 {
130 std::string params;
131 params = "host=" + server + " "
132        + "dbname=" + database + " "
133        + "user=" + user + " "
134        + "password=" + password;
135
136 connection = PQconnectdb(params.c_str());
137
138 if (PQstatus(connection) != CONNECTION_OK)
139     {
140     strError = PQerrorMessage(connection);
141     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
142     // Will try to connect later
143     return 0;
144     }
145
146 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
147     {
148     strError = PQerrorMessage(connection);
149     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
150     return 1;
151     }
152
153 return CheckVersion();
154 }
155 //-----------------------------------------------------------------------------
156 int POSTGRESQL_STORE::Reset() const
157 {
158 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
159     {
160     struct timespec ts = {1, 0};
161     nanosleep(&ts, NULL);
162     PQreset(connection);
163     }
164
165 if (PQstatus(connection) != CONNECTION_OK)
166     {
167     strError = PQerrorMessage(connection);
168     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
169     return 1;
170     }
171
172 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
173     {
174     strError = PQerrorMessage(connection);
175     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
176     return -1;
177     }
178
179 return CheckVersion();
180 }
181 //-----------------------------------------------------------------------------
182 int POSTGRESQL_STORE::CheckVersion() const
183 {
184
185 if (StartTransaction())
186     {
187     strError = "Failed to start transaction";
188     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
189     return -1;
190     }
191
192 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
193
194 if (PQresultStatus(result) != PGRES_TUPLES_OK)
195     {
196     strError = PQresultErrorMessage(result);
197     PQclear(result);
198     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
199     RollbackTransaction();
200     return -1;
201     }
202
203 if (str2x(PQgetvalue(result, 0, 0), version))
204     {
205     strError = "Invalid DB version";
206     PQclear(result);
207     RollbackTransaction();
208     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
209     return -1;
210     }
211
212 PQclear(result);
213
214 if (version < DB_MIN_VERSION)
215     {
216     strError = "DB version too old";
217     RollbackTransaction();
218     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
219     return -1;
220     }
221
222 if (version < 6)
223     {
224     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);
225     }
226
227 if (CommitTransaction())
228     {
229     strError = "Failed to commit transaction";
230     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
231     return -1;
232     }
233
234 return 0;
235 }
236 //-----------------------------------------------------------------------------