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>
23 * Services manipulation methods
26 * $Date: 2009/06/09 12:32:40 $
36 #include "postgresql_store.h"
37 #include "stg/locker.h"
39 //-----------------------------------------------------------------------------
40 int POSTGRESQL_STORE::GetServicesList(vector<string> * servicesList) const
42 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
44 if (PQstatus(connection) != CONNECTION_OK)
46 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
49 strError = "Connection lost";
50 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): '%s'\n", strError.c_str());
57 if (StartTransaction())
59 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to start transaction'\n");
63 result = PQexec(connection, "SELECT name FROM tb_services");
65 if (PQresultStatus(result) != PGRES_TUPLES_OK)
67 strError = PQresultErrorMessage(result);
69 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): '%s'\n", strError.c_str());
70 if (RollbackTransaction())
72 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to rollback transaction'\n");
77 int tuples = PQntuples(result);
79 for (int i = 0; i < tuples; ++i)
81 servicesList->push_back(PQgetvalue(result, i, 0));
86 if (CommitTransaction())
88 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to commit transaction'\n");
95 //-----------------------------------------------------------------------------
96 int POSTGRESQL_STORE::SaveService(const SERVICE_CONF & sc) const
98 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
100 if (PQstatus(connection) != CONNECTION_OK)
102 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
105 strError = "Connection lost";
106 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): '%s'\n", strError.c_str());
113 if (StartTransaction())
115 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to start transaction'\n");
119 std::string ename = sc.name;
120 std::string ecomment = sc.comment;
122 if (EscapeString(ename))
124 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to escape name'\n");
125 if (RollbackTransaction())
127 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
132 if (EscapeString(ecomment))
134 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to escape comment'\n");
135 if (RollbackTransaction())
137 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
142 std::stringstream query;
143 query << "UPDATE tb_services SET "
144 << "comment = '" << ecomment << "', "
145 << "cost = " << sc.cost << ", "
146 << "pay_day = " << sc.payDay << " "
147 << "WHERE name = '" << ename << "'";
149 result = PQexec(connection, query.str().c_str());
151 if (PQresultStatus(result) != PGRES_COMMAND_OK)
153 strError = PQresultErrorMessage(result);
155 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): '%s'\n", strError.c_str());
156 if (RollbackTransaction())
158 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
165 if (CommitTransaction())
167 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to commit transaction'\n");
174 //-----------------------------------------------------------------------------
175 int POSTGRESQL_STORE::RestoreService(SERVICE_CONF * sc,
176 const string & name) const
178 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
180 if (PQstatus(connection) != CONNECTION_OK)
182 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
185 strError = "Connection lost";
186 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): '%s'\n", strError.c_str());
193 if (StartTransaction())
195 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to start transaction'\n");
199 std::string ename = name;
201 if (EscapeString(ename))
203 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to escape name'\n");
204 if (RollbackTransaction())
206 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
211 std::stringstream query;
212 query << "SELECT comment, cost, pay_day FROM tb_services WHERE name = '" << ename << "'";
214 result = PQexec(connection, query.str().c_str());
216 if (PQresultStatus(result) != PGRES_TUPLES_OK)
218 strError = PQresultErrorMessage(result);
220 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): '%s'\n", strError.c_str());
221 if (RollbackTransaction())
223 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
228 int tuples = PQntuples(result);
232 strError = "Failed to fetch service's data";
233 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
235 if (RollbackTransaction())
237 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
242 std::stringstream tuple;
243 tuple << PQgetvalue(result, 0, 0) << " "
244 << PQgetvalue(result, 0, 1) << " "
245 << PQgetvalue(result, 0, 2);
253 if (CommitTransaction())
255 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to commit transaction'\n");
262 //-----------------------------------------------------------------------------
263 int POSTGRESQL_STORE::AddService(const string & name) const
265 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
267 if (PQstatus(connection) != CONNECTION_OK)
269 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
272 strError = "Connection lost";
273 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): '%s'\n", strError.c_str());
280 if (StartTransaction())
282 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to start transaction'\n");
286 std::string ename = name;
288 if (EscapeString(ename))
290 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to escape name'\n");
291 if (RollbackTransaction())
293 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to rollback transaction'\n");
298 std::stringstream query;
299 query << "INSERT INTO tb_services \
300 (name, comment, cost, pay_day) \
302 ('" << ename << "', '', 0, 0)";
304 result = PQexec(connection, query.str().c_str());
306 if (PQresultStatus(result) != PGRES_COMMAND_OK)
308 strError = PQresultErrorMessage(result);
310 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): '%s'\n", strError.c_str());
311 if (RollbackTransaction())
313 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to rollback transaction'\n");
320 if (CommitTransaction())
322 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to commit transaction'\n");
329 //-----------------------------------------------------------------------------
330 int POSTGRESQL_STORE::DelService(const string & name) const
332 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
334 if (PQstatus(connection) != CONNECTION_OK)
336 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
339 strError = "Connection lost";
340 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): '%s'\n", strError.c_str());
347 if (StartTransaction())
349 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to start transaction'\n");
353 std::string ename = name;
355 if (EscapeString(ename))
357 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to escape name'\n");
358 if (RollbackTransaction())
360 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to rollback transaction'\n");
365 std::stringstream query;
366 query << "DELETE FROM tb_services WHERE name = '" << ename << "'";
368 result = PQexec(connection, query.str().c_str());
370 if (PQresultStatus(result) != PGRES_COMMAND_OK)
372 strError = PQresultErrorMessage(result);
374 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): '%s'\n", strError.c_str());
375 if (RollbackTransaction())
377 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to rollback transaction'\n");
384 if (CommitTransaction())
386 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to commit transaction'\n");
392 //-----------------------------------------------------------------------------