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/common.h" // str2x, printfd
53 extern "C" STG::Store* GetStore()
55 static POSTGRESQL_STORE plugin;
59 //-----------------------------------------------------------------------------
60 POSTGRESQL_STORE::POSTGRESQL_STORE()
61 : versionString("postgresql_store v.1.3"),
63 database("stargazer"),
66 clientEncoding("KOI8"),
70 logger(STG::PluginLogger::get("store_postgresql"))
72 pthread_mutex_init(&mutex, NULL);
74 //-----------------------------------------------------------------------------
75 POSTGRESQL_STORE::~POSTGRESQL_STORE()
81 pthread_mutex_destroy(&mutex);
83 //-----------------------------------------------------------------------------
84 int POSTGRESQL_STORE::ParseSettings()
86 std::vector<STG::ParamValue>::iterator i;
88 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
92 std::string s = ToLower(i->param);
95 server = i->value.front();
99 database = i->value.front();
103 user = i->value.front();
107 password = i->value.front();
111 if (str2x(i->value.front(), retries))
113 strError = "Invalid 'retries' value";
114 printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
120 clientEncoding = "KOI8";
124 //-----------------------------------------------------------------------------
125 int POSTGRESQL_STORE::Connect()
128 params = "host=" + server + " "
129 + "dbname=" + database + " "
130 + "user=" + user + " "
131 + "password=" + password;
133 connection = PQconnectdb(params.c_str());
135 if (PQstatus(connection) != CONNECTION_OK)
137 strError = PQerrorMessage(connection);
138 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
139 // Will try to connect later
143 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
145 strError = PQerrorMessage(connection);
146 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
150 return CheckVersion();
152 //-----------------------------------------------------------------------------
153 int POSTGRESQL_STORE::Reset() const
155 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
157 struct timespec ts = {1, 0};
158 nanosleep(&ts, NULL);
162 if (PQstatus(connection) != CONNECTION_OK)
164 strError = PQerrorMessage(connection);
165 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
169 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
171 strError = PQerrorMessage(connection);
172 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
176 return CheckVersion();
178 //-----------------------------------------------------------------------------
179 int POSTGRESQL_STORE::CheckVersion() const
182 if (StartTransaction())
184 strError = "Failed to start transaction";
185 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
189 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
191 if (PQresultStatus(result) != PGRES_TUPLES_OK)
193 strError = PQresultErrorMessage(result);
195 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
196 RollbackTransaction();
200 if (str2x(PQgetvalue(result, 0, 0), version))
202 strError = "Invalid DB version";
204 RollbackTransaction();
205 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
211 if (version < DB_MIN_VERSION)
213 strError = "DB version too old";
214 RollbackTransaction();
215 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
221 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);
224 if (CommitTransaction())
226 strError = "Failed to commit transaction";
227 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
231 logger("POSTGRESQL_STORE: Current DB schema version: %d", version);
235 //-----------------------------------------------------------------------------