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 dosconnects added
28 * Reconnection on faults added
34 * Initial implementation
37 * $Date: 2010/01/06 10:43:48 $
47 #include "postgresql_store.h"
48 #include "postgresql_store_utils.h"
49 #include "base_settings.h"
51 class POSTGRESQL_STORE_CREATOR
54 POSTGRESQL_STORE_CREATOR()
55 : pqStore(new POSTGRESQL_STORE())
58 ~POSTGRESQL_STORE_CREATOR()
62 POSTGRESQL_STORE * GetStore() { return pqStore; };
64 POSTGRESQL_STORE * pqStore;
67 //-----------------------------------------------------------------------------
68 BASE_STORE * GetStore()
70 return pqStoreeCreator.GetStore();
73 //-----------------------------------------------------------------------------
74 POSTGRESQL_STORE::POSTGRESQL_STORE()
78 database = "stargazer";
81 versionString = "postgresql_store v.1.3";
82 pthread_mutex_init(&mutex, NULL);
84 //-----------------------------------------------------------------------------
85 POSTGRESQL_STORE::~POSTGRESQL_STORE()
91 pthread_mutex_destroy(&mutex);
93 //-----------------------------------------------------------------------------
94 int POSTGRESQL_STORE::ParseSettings()
96 std::vector<PARAM_VALUE>::iterator i;
99 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
102 std::transform(s.begin(), s.end(), s.begin(), ToLower());
105 server = *(i->value.begin());
109 database = *(i->value.begin());
113 user = *(i->value.begin());
117 password = *(i->value.begin());
121 clientEncoding = "KOI8";
125 //-----------------------------------------------------------------------------
126 int POSTGRESQL_STORE::Connect()
129 params = "host=" + server + " "
130 + "dbname=" + database + " "
131 + "user=" + user + " "
132 + "password=" + password;
134 connection = PQconnectdb(params.c_str());
136 if (PQstatus(connection) != CONNECTION_OK)
138 strError = PQerrorMessage(connection);
139 printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
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
157 if (PQstatus(connection) != CONNECTION_OK)
159 strError = PQerrorMessage(connection);
160 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
164 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
166 strError = PQerrorMessage(connection);
167 printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
171 return CheckVersion();
173 //-----------------------------------------------------------------------------
174 int POSTGRESQL_STORE::CheckVersion() const
177 if (StartTransaction())
179 strError = "Failed to start transaction";
180 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
184 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
186 if (PQresultStatus(result) != PGRES_TUPLES_OK)
188 strError = PQresultErrorMessage(result);
190 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
191 RollbackTransaction();
195 if (str2x(PQgetvalue(result, 0, 0), version))
197 strError = "Invalid DB version";
199 RollbackTransaction();
200 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
206 if (version < DB_MIN_VERSION)
208 strError = "DB version too old";
209 RollbackTransaction();
210 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
216 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);
219 if (CommitTransaction())
221 strError = "Failed to commit transaction";
222 printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
228 //-----------------------------------------------------------------------------