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 $
30 #include "postgresql_store.h"
32 #include "stg/service_conf.h"
33 #include "stg/common.h"
34 #include "stg/locker.h"
42 //-----------------------------------------------------------------------------
43 int POSTGRESQL_STORE::GetServicesList(std::vector<std::string> * servicesList) const
45 STG_LOCKER lock(&mutex);
47 if (PQstatus(connection) != CONNECTION_OK)
49 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
52 strError = "Connection lost";
53 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): '%s'\n", strError.c_str());
60 if (StartTransaction())
62 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to start transaction'\n");
66 result = PQexec(connection, "SELECT name FROM tb_services");
68 if (PQresultStatus(result) != PGRES_TUPLES_OK)
70 strError = PQresultErrorMessage(result);
72 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): '%s'\n", strError.c_str());
73 if (RollbackTransaction())
75 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to rollback transaction'\n");
80 int tuples = PQntuples(result);
82 for (int i = 0; i < tuples; ++i)
84 servicesList->push_back(PQgetvalue(result, i, 0));
89 if (CommitTransaction())
91 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to commit transaction'\n");
98 //-----------------------------------------------------------------------------
99 int POSTGRESQL_STORE::SaveService(const STG::ServiceConf & sc) const
101 STG_LOCKER lock(&mutex);
103 if (PQstatus(connection) != CONNECTION_OK)
105 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
108 strError = "Connection lost";
109 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): '%s'\n", strError.c_str());
116 if (StartTransaction())
118 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to start transaction'\n");
122 std::string ename = sc.name;
123 std::string ecomment = sc.comment;
125 if (EscapeString(ename))
127 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to escape name'\n");
128 if (RollbackTransaction())
130 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
135 if (EscapeString(ecomment))
137 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to escape comment'\n");
138 if (RollbackTransaction())
140 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
145 std::ostringstream query;
146 query << "UPDATE tb_services SET "
147 << "comment = '" << ecomment << "', "
148 << "cost = " << sc.cost << ", "
149 << "pay_day = " << sc.payDay << " "
150 << "WHERE name = '" << ename << "'";
152 result = PQexec(connection, query.str().c_str());
154 if (PQresultStatus(result) != PGRES_COMMAND_OK)
156 strError = PQresultErrorMessage(result);
158 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): '%s'\n", strError.c_str());
159 if (RollbackTransaction())
161 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
168 if (CommitTransaction())
170 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to commit transaction'\n");
177 //-----------------------------------------------------------------------------
178 int POSTGRESQL_STORE::RestoreService(STG::ServiceConf * sc,
179 const std::string & name) const
181 STG_LOCKER lock(&mutex);
183 if (PQstatus(connection) != CONNECTION_OK)
185 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
188 strError = "Connection lost";
189 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): '%s'\n", strError.c_str());
196 if (StartTransaction())
198 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to start transaction'\n");
202 std::string ename = name;
204 if (EscapeString(ename))
206 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to escape name'\n");
207 if (RollbackTransaction())
209 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
214 std::ostringstream query;
215 query << "SELECT comment, cost, pay_day FROM tb_services WHERE name = '" << ename << "'";
217 result = PQexec(connection, query.str().c_str());
219 if (PQresultStatus(result) != PGRES_TUPLES_OK)
221 strError = PQresultErrorMessage(result);
223 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): '%s'\n", strError.c_str());
224 if (RollbackTransaction())
226 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
231 int tuples = PQntuples(result);
235 strError = "Failed to fetch service's data";
236 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
238 if (RollbackTransaction())
240 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
245 std::stringstream tuple;
246 tuple << PQgetvalue(result, 0, 0) << " "
247 << PQgetvalue(result, 0, 1) << " "
248 << PQgetvalue(result, 0, 2);
256 if (CommitTransaction())
258 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to commit transaction'\n");
265 //-----------------------------------------------------------------------------
266 int POSTGRESQL_STORE::AddService(const std::string & name) const
268 STG_LOCKER lock(&mutex);
270 if (PQstatus(connection) != CONNECTION_OK)
272 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
275 strError = "Connection lost";
276 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): '%s'\n", strError.c_str());
283 if (StartTransaction())
285 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to start transaction'\n");
289 std::string ename = name;
291 if (EscapeString(ename))
293 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to escape name'\n");
294 if (RollbackTransaction())
296 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to rollback transaction'\n");
301 std::ostringstream query;
302 query << "INSERT INTO tb_services \
303 (name, comment, cost, pay_day) \
305 ('" << ename << "', '', 0, 0)";
307 result = PQexec(connection, query.str().c_str());
309 if (PQresultStatus(result) != PGRES_COMMAND_OK)
311 strError = PQresultErrorMessage(result);
313 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): '%s'\n", strError.c_str());
314 if (RollbackTransaction())
316 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to rollback transaction'\n");
323 if (CommitTransaction())
325 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to commit transaction'\n");
332 //-----------------------------------------------------------------------------
333 int POSTGRESQL_STORE::DelService(const std::string & name) const
335 STG_LOCKER lock(&mutex);
337 if (PQstatus(connection) != CONNECTION_OK)
339 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
342 strError = "Connection lost";
343 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): '%s'\n", strError.c_str());
350 if (StartTransaction())
352 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to start transaction'\n");
356 std::string ename = name;
358 if (EscapeString(ename))
360 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to escape name'\n");
361 if (RollbackTransaction())
363 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to rollback transaction'\n");
368 std::ostringstream query;
369 query << "DELETE FROM tb_services WHERE name = '" << ename << "'";
371 result = PQexec(connection, query.str().c_str());
373 if (PQresultStatus(result) != PGRES_COMMAND_OK)
375 strError = PQresultErrorMessage(result);
377 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): '%s'\n", strError.c_str());
378 if (RollbackTransaction())
380 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to rollback transaction'\n");
387 if (CommitTransaction())
389 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to commit transaction'\n");
395 //-----------------------------------------------------------------------------