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