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 $
41 #include "postgresql_store.h"
43 #include "postgresql_store_utils.h"
44 #include "postgresql_store.h"
46 #include "stg/module_settings.h"
47 #include "stg/plugin_creator.h"
56 PLUGIN_CREATOR<POSTGRESQL_STORE> pgsc;
59 extern "C" STORE * GetStore();
61 //-----------------------------------------------------------------------------
64 return pgsc.GetPlugin();
67 //-----------------------------------------------------------------------------
68 POSTGRESQL_STORE::POSTGRESQL_STORE()
69 : versionString("postgresql_store v.1.3"),
71 database("stargazer"),
74 clientEncoding("KOI8"),
78 logger(GetPluginLogger(GetStgLogger(), "store_postgresql"))
80 pthread_mutex_init(&mutex, NULL);
82 //-----------------------------------------------------------------------------
83 POSTGRESQL_STORE::~POSTGRESQL_STORE()
89 pthread_mutex_destroy(&mutex);
91 //-----------------------------------------------------------------------------
92 int POSTGRESQL_STORE::ParseSettings()
94 std::vector<PARAM_VALUE>::iterator i;
96 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
100 std::string s = ToLower(i->param);
103 server = i->value.front();
107 database = i->value.front();
111 user = i->value.front();
115 password = i->value.front();
119 if (str2x(i->value.front(), retries))
121 strError = "Invalid 'retries' value";
122 printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
128 clientEncoding = "KOI8";
132 //-----------------------------------------------------------------------------
133 int POSTGRESQL_STORE::Connect()
136 params = "host=" + server + " "
137 + "dbname=" + database + " "
138 + "user=" + user + " "
139 + "password=" + password;
141 connection = PQconnectdb(params.c_str());
143 if (PQstatus(connection) != CONNECTION_OK)
145 strError = PQerrorMessage(connection);
146 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
147 // Will try to connect later
151 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
153 strError = PQerrorMessage(connection);
154 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
158 return CheckVersion();
160 //-----------------------------------------------------------------------------
161 int POSTGRESQL_STORE::Reset() const
163 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
165 struct timespec ts = {1, 0};
166 nanosleep(&ts, NULL);
170 if (PQstatus(connection) != CONNECTION_OK)
172 strError = PQerrorMessage(connection);
173 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
177 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
179 strError = PQerrorMessage(connection);
180 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
184 return CheckVersion();
186 //-----------------------------------------------------------------------------
187 int POSTGRESQL_STORE::CheckVersion() const
190 if (StartTransaction())
192 strError = "Failed to start transaction";
193 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
197 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
199 if (PQresultStatus(result) != PGRES_TUPLES_OK)
201 strError = PQresultErrorMessage(result);
203 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
204 RollbackTransaction();
208 if (str2x(PQgetvalue(result, 0, 0), version))
210 strError = "Invalid DB version";
212 RollbackTransaction();
213 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
219 if (version < DB_MIN_VERSION)
221 strError = "DB version too old";
222 RollbackTransaction();
223 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
229 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);
232 if (CommitTransaction())
234 strError = "Failed to commit transaction";
235 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
239 logger("POSTGRESQL_STORE: Current DB schema version: %d", version);
243 //-----------------------------------------------------------------------------