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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * This file contains a realization of a base postgresql-storage plugin class
25 * FreeMb logging on disconnects added
28 * Reconnection on faults added
34 * Initial implementation
37 * $Date: 2010/01/06 10:43:48 $
47 #include "stg/module_settings.h"
48 #include "stg/plugin_creator.h"
49 #include "stg/logger.h"
50 #include "postgresql_store_utils.h"
51 #include "postgresql_store.h"
53 PLUGIN_CREATOR<POSTGRESQL_STORE> pqStoreeCreator;
55 //-----------------------------------------------------------------------------
58 return pqStoreeCreator.GetPlugin();
61 //-----------------------------------------------------------------------------
62 POSTGRESQL_STORE::POSTGRESQL_STORE()
63 : versionString("postgresql_store v.1.3"),
66 database("stargazer"),
69 clientEncoding("KOI8"),
75 WriteServLog(GetStgLogger())
77 pthread_mutex_init(&mutex, NULL);
79 //-----------------------------------------------------------------------------
80 POSTGRESQL_STORE::~POSTGRESQL_STORE()
86 pthread_mutex_destroy(&mutex);
88 //-----------------------------------------------------------------------------
89 int POSTGRESQL_STORE::ParseSettings()
91 std::vector<PARAM_VALUE>::iterator i;
94 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
97 std::transform(s.begin(), s.end(), s.begin(), ToLower());
100 server = *(i->value.begin());
104 database = *(i->value.begin());
108 user = *(i->value.begin());
112 password = *(i->value.begin());
116 if (str2x(*(i->value.begin()), retries))
118 strError = "Invalid 'retries' value";
119 printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
125 clientEncoding = "KOI8";
129 //-----------------------------------------------------------------------------
130 int POSTGRESQL_STORE::Connect()
133 params = "host=" + server + " "
134 + "dbname=" + database + " "
135 + "user=" + user + " "
136 + "password=" + password;
138 connection = PQconnectdb(params.c_str());
140 if (PQstatus(connection) != CONNECTION_OK)
142 strError = PQerrorMessage(connection);
143 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
144 // Will try to connect later
148 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
150 strError = PQerrorMessage(connection);
151 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
155 return CheckVersion();
157 //-----------------------------------------------------------------------------
158 int POSTGRESQL_STORE::Reset() const
160 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
162 struct timespec ts = {1, 0};
163 nanosleep(&ts, NULL);
167 if (PQstatus(connection) != CONNECTION_OK)
169 strError = PQerrorMessage(connection);
170 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
174 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
176 strError = PQerrorMessage(connection);
177 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
181 return CheckVersion();
183 //-----------------------------------------------------------------------------
184 int POSTGRESQL_STORE::CheckVersion() const
187 if (StartTransaction())
189 strError = "Failed to start transaction";
190 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
194 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
196 if (PQresultStatus(result) != PGRES_TUPLES_OK)
198 strError = PQresultErrorMessage(result);
200 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
201 RollbackTransaction();
205 if (str2x(PQgetvalue(result, 0, 0), version))
207 strError = "Invalid DB version";
209 RollbackTransaction();
210 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
216 if (version < DB_MIN_VERSION)
218 strError = "DB version too old";
219 RollbackTransaction();
220 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
226 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);
229 if (CommitTransaction())
231 strError = "Failed to commit transaction";
232 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
236 WriteServLog("POSTGRESQL_STORE: Current DB schema version: %d", version);
240 //-----------------------------------------------------------------------------