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"
52 PLUGIN_CREATOR<POSTGRESQL_STORE> pqStoreeCreator;
54 //-----------------------------------------------------------------------------
57 return pqStoreeCreator.GetPlugin();
60 //-----------------------------------------------------------------------------
61 POSTGRESQL_STORE::POSTGRESQL_STORE()
62 : versionString("postgresql_store v.1.3"),
65 database("stargazer"),
68 clientEncoding("KOI8"),
74 logger(GetPluginLogger(GetStgLogger(), "store_postgresql"))
76 pthread_mutex_init(&mutex, NULL);
78 //-----------------------------------------------------------------------------
79 POSTGRESQL_STORE::~POSTGRESQL_STORE()
85 pthread_mutex_destroy(&mutex);
87 //-----------------------------------------------------------------------------
88 int POSTGRESQL_STORE::ParseSettings()
90 std::vector<PARAM_VALUE>::iterator i;
93 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
96 std::transform(s.begin(), s.end(), s.begin(), ToLower());
99 server = *(i->value.begin());
103 database = *(i->value.begin());
107 user = *(i->value.begin());
111 password = *(i->value.begin());
115 if (str2x(*(i->value.begin()), retries))
117 strError = "Invalid 'retries' value";
118 printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
124 clientEncoding = "KOI8";
128 //-----------------------------------------------------------------------------
129 int POSTGRESQL_STORE::Connect()
132 params = "host=" + server + " "
133 + "dbname=" + database + " "
134 + "user=" + user + " "
135 + "password=" + password;
137 connection = PQconnectdb(params.c_str());
139 if (PQstatus(connection) != CONNECTION_OK)
141 strError = PQerrorMessage(connection);
142 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
143 // Will try to connect later
147 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
149 strError = PQerrorMessage(connection);
150 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
154 return CheckVersion();
156 //-----------------------------------------------------------------------------
157 int POSTGRESQL_STORE::Reset() const
159 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
161 struct timespec ts = {1, 0};
162 nanosleep(&ts, NULL);
166 if (PQstatus(connection) != CONNECTION_OK)
168 strError = PQerrorMessage(connection);
169 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
173 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
175 strError = PQerrorMessage(connection);
176 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
180 return CheckVersion();
182 //-----------------------------------------------------------------------------
183 int POSTGRESQL_STORE::CheckVersion() const
186 if (StartTransaction())
188 strError = "Failed to start transaction";
189 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
193 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
195 if (PQresultStatus(result) != PGRES_TUPLES_OK)
197 strError = PQresultErrorMessage(result);
199 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
200 RollbackTransaction();
204 if (str2x(PQgetvalue(result, 0, 0), version))
206 strError = "Invalid DB version";
208 RollbackTransaction();
209 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
215 if (version < DB_MIN_VERSION)
217 strError = "DB version too old";
218 RollbackTransaction();
219 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
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);
228 if (CommitTransaction())
230 strError = "Failed to commit transaction";
231 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
237 //-----------------------------------------------------------------------------