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 "postgresql_store_utils.h"
50 #include "postgresql_store.h"
54 PLUGIN_CREATOR<POSTGRESQL_STORE> pgsc;
57 extern "C" STORE * GetStore();
59 //-----------------------------------------------------------------------------
62 return pgsc.GetPlugin();
65 //-----------------------------------------------------------------------------
66 POSTGRESQL_STORE::POSTGRESQL_STORE()
67 : versionString("postgresql_store v.1.3"),
70 database("stargazer"),
73 clientEncoding("KOI8"),
79 logger(GetPluginLogger(GetStgLogger(), "store_postgresql"))
81 pthread_mutex_init(&mutex, NULL);
83 //-----------------------------------------------------------------------------
84 POSTGRESQL_STORE::~POSTGRESQL_STORE()
90 pthread_mutex_destroy(&mutex);
92 //-----------------------------------------------------------------------------
93 int POSTGRESQL_STORE::ParseSettings()
95 std::vector<PARAM_VALUE>::iterator i;
98 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
101 std::transform(s.begin(), s.end(), s.begin(), ToLower());
104 server = *(i->value.begin());
108 database = *(i->value.begin());
112 user = *(i->value.begin());
116 password = *(i->value.begin());
120 if (str2x(*(i->value.begin()), retries))
122 strError = "Invalid 'retries' value";
123 printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
129 clientEncoding = "KOI8";
133 //-----------------------------------------------------------------------------
134 int POSTGRESQL_STORE::Connect()
137 params = "host=" + server + " "
138 + "dbname=" + database + " "
139 + "user=" + user + " "
140 + "password=" + password;
142 connection = PQconnectdb(params.c_str());
144 if (PQstatus(connection) != CONNECTION_OK)
146 strError = PQerrorMessage(connection);
147 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
148 // Will try to connect later
152 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
154 strError = PQerrorMessage(connection);
155 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
159 return CheckVersion();
161 //-----------------------------------------------------------------------------
162 int POSTGRESQL_STORE::Reset() const
164 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
166 struct timespec ts = {1, 0};
167 nanosleep(&ts, NULL);
171 if (PQstatus(connection) != CONNECTION_OK)
173 strError = PQerrorMessage(connection);
174 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
178 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
180 strError = PQerrorMessage(connection);
181 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
185 return CheckVersion();
187 //-----------------------------------------------------------------------------
188 int POSTGRESQL_STORE::CheckVersion() const
191 if (StartTransaction())
193 strError = "Failed to start transaction";
194 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
198 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
200 if (PQresultStatus(result) != PGRES_TUPLES_OK)
202 strError = PQresultErrorMessage(result);
204 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
205 RollbackTransaction();
209 if (str2x(PQgetvalue(result, 0, 0), version))
211 strError = "Invalid DB version";
213 RollbackTransaction();
214 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
220 if (version < DB_MIN_VERSION)
222 strError = "DB version too old";
223 RollbackTransaction();
224 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
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);
233 if (CommitTransaction())
235 strError = "Failed to commit transaction";
236 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
240 WriteServLog("POSTGRESQL_STORE: Current DB schema version: %d", version);
244 //-----------------------------------------------------------------------------