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