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 : Maksym Mamontov <stg@madf.info>
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 $
46 #include "stg/module_settings.h"
47 #include "stg/plugin_creator.h"
48 #include "postgresql_store_utils.h"
49 #include "postgresql_store.h"
53 PLUGIN_CREATOR<POSTGRESQL_STORE> pgsc;
56 extern "C" STORE * GetStore();
58 //-----------------------------------------------------------------------------
61 return pgsc.GetPlugin();
64 //-----------------------------------------------------------------------------
65 POSTGRESQL_STORE::POSTGRESQL_STORE()
66 : versionString("postgresql_store v.1.3"),
68 database("stargazer"),
71 clientEncoding("KOI8"),
75 logger(GetPluginLogger(GetStgLogger(), "store_postgresql"))
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)
98 s = ToLower(i->param);
101 server = i->value.front();
105 database = i->value.front();
109 user = i->value.front();
113 password = i->value.front();
117 if (str2x(i->value.front(), retries))
119 strError = "Invalid 'retries' value";
120 printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
126 clientEncoding = "KOI8";
130 //-----------------------------------------------------------------------------
131 int POSTGRESQL_STORE::Connect()
134 params = "host=" + server + " "
135 + "dbname=" + database + " "
136 + "user=" + user + " "
137 + "password=" + password;
139 connection = PQconnectdb(params.c_str());
141 if (PQstatus(connection) != CONNECTION_OK)
143 strError = PQerrorMessage(connection);
144 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
145 // Will try to connect later
149 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
151 strError = PQerrorMessage(connection);
152 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
156 return CheckVersion();
158 //-----------------------------------------------------------------------------
159 int POSTGRESQL_STORE::Reset() const
161 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
163 struct timespec ts = {1, 0};
164 nanosleep(&ts, NULL);
168 if (PQstatus(connection) != CONNECTION_OK)
170 strError = PQerrorMessage(connection);
171 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
175 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
177 strError = PQerrorMessage(connection);
178 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
182 return CheckVersion();
184 //-----------------------------------------------------------------------------
185 int POSTGRESQL_STORE::CheckVersion() const
188 if (StartTransaction())
190 strError = "Failed to start transaction";
191 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
195 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
197 if (PQresultStatus(result) != PGRES_TUPLES_OK)
199 strError = PQresultErrorMessage(result);
201 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
202 RollbackTransaction();
206 if (str2x(PQgetvalue(result, 0, 0), version))
208 strError = "Invalid DB version";
210 RollbackTransaction();
211 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
217 if (version < DB_MIN_VERSION)
219 strError = "DB version too old";
220 RollbackTransaction();
221 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
227 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);
230 if (CommitTransaction())
232 strError = "Failed to commit transaction";
233 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
237 logger("POSTGRESQL_STORE: Current DB schema version: %d", version);
241 //-----------------------------------------------------------------------------