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 "stg/module_settings.h"
44 #include "stg/plugin_creator.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"),
69 database("stargazer"),
72 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)
98 std::string param(ToLower(i->param));
99 if (param == "server")
100 server = *(i->value.begin());
101 else if (param == "database")
102 database = *(i->value.begin());
103 else if (param == "user")
104 user = *(i->value.begin());
105 else if (param == "password")
106 password = *(i->value.begin());
107 else if (param == "retries")
108 if (str2x(*(i->value.begin()), retries))
110 strError = "Invalid 'retries' value";
111 printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
116 clientEncoding = "KOI8";
120 //-----------------------------------------------------------------------------
121 int POSTGRESQL_STORE::Connect()
124 params = "host=" + server + " "
125 + "dbname=" + database + " "
126 + "user=" + user + " "
127 + "password=" + password;
129 connection = PQconnectdb(params.c_str());
131 if (PQstatus(connection) != CONNECTION_OK)
133 strError = PQerrorMessage(connection);
134 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
135 // Will try to connect later
139 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
141 strError = PQerrorMessage(connection);
142 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
146 return CheckVersion();
148 //-----------------------------------------------------------------------------
149 int POSTGRESQL_STORE::Reset() const
151 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
153 struct timespec ts = {1, 0};
154 nanosleep(&ts, NULL);
158 if (PQstatus(connection) != CONNECTION_OK)
160 strError = PQerrorMessage(connection);
161 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
165 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
167 strError = PQerrorMessage(connection);
168 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
172 return CheckVersion();
174 //-----------------------------------------------------------------------------
175 int POSTGRESQL_STORE::CheckVersion() const
178 if (StartTransaction())
180 strError = "Failed to start transaction";
181 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
185 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
187 if (PQresultStatus(result) != PGRES_TUPLES_OK)
189 strError = PQresultErrorMessage(result);
191 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
192 RollbackTransaction();
196 if (str2x(PQgetvalue(result, 0, 0), version))
198 strError = "Invalid DB version";
200 RollbackTransaction();
201 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
207 if (version < DB_MIN_VERSION)
209 strError = "DB version too old";
210 RollbackTransaction();
211 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
217 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);
220 if (CommitTransaction())
222 strError = "Failed to commit transaction";
223 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
229 //-----------------------------------------------------------------------------