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"
41 //-----------------------------------------------------------------------------
42 int POSTGRESQL_STORE::GetServicesList(std::vector<std::string> * servicesList) const
44 std::lock_guard lock(m_mutex);
46 if (PQstatus(connection) != CONNECTION_OK)
48 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
51 strError = "Connection lost";
52 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): '%s'\n", strError.c_str());
59 if (StartTransaction())
61 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to start transaction'\n");
65 result = PQexec(connection, "SELECT name FROM tb_services");
67 if (PQresultStatus(result) != PGRES_TUPLES_OK)
69 strError = PQresultErrorMessage(result);
71 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): '%s'\n", strError.c_str());
72 if (RollbackTransaction())
74 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to rollback transaction'\n");
79 int tuples = PQntuples(result);
81 for (int i = 0; i < tuples; ++i)
83 servicesList->push_back(PQgetvalue(result, i, 0));
88 if (CommitTransaction())
90 printfd(__FILE__, "POSTGRESQL_STORE::GetServicesList(): 'Failed to commit transaction'\n");
97 //-----------------------------------------------------------------------------
98 int POSTGRESQL_STORE::SaveService(const STG::ServiceConf & sc) const
100 std::lock_guard lock(m_mutex);
102 if (PQstatus(connection) != CONNECTION_OK)
104 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
107 strError = "Connection lost";
108 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): '%s'\n", strError.c_str());
115 if (StartTransaction())
117 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to start transaction'\n");
121 std::string ename = sc.name;
122 std::string ecomment = sc.comment;
124 if (EscapeString(ename))
126 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to escape name'\n");
127 if (RollbackTransaction())
129 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
134 if (EscapeString(ecomment))
136 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to escape comment'\n");
137 if (RollbackTransaction())
139 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
144 std::ostringstream query;
145 query << "UPDATE tb_services SET "
146 << "comment = '" << ecomment << "', "
147 << "cost = " << sc.cost << ", "
148 << "pay_day = " << sc.payDay << " "
149 << "WHERE name = '" << ename << "'";
151 result = PQexec(connection, query.str().c_str());
153 if (PQresultStatus(result) != PGRES_COMMAND_OK)
155 strError = PQresultErrorMessage(result);
157 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): '%s'\n", strError.c_str());
158 if (RollbackTransaction())
160 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to rollback transaction'\n");
167 if (CommitTransaction())
169 printfd(__FILE__, "POSTGRESQL_STORE::SaveService(): 'Failed to commit transaction'\n");
176 //-----------------------------------------------------------------------------
177 int POSTGRESQL_STORE::RestoreService(STG::ServiceConf * sc,
178 const std::string & name) const
180 std::lock_guard lock(m_mutex);
182 if (PQstatus(connection) != CONNECTION_OK)
184 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
187 strError = "Connection lost";
188 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): '%s'\n", strError.c_str());
195 if (StartTransaction())
197 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to start transaction'\n");
201 std::string ename = name;
203 if (EscapeString(ename))
205 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to escape name'\n");
206 if (RollbackTransaction())
208 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
213 std::ostringstream query;
214 query << "SELECT comment, cost, pay_day FROM tb_services WHERE name = '" << ename << "'";
216 result = PQexec(connection, query.str().c_str());
218 if (PQresultStatus(result) != PGRES_TUPLES_OK)
220 strError = PQresultErrorMessage(result);
222 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): '%s'\n", strError.c_str());
223 if (RollbackTransaction())
225 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
230 int tuples = PQntuples(result);
234 strError = "Failed to fetch service's data";
235 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
237 if (RollbackTransaction())
239 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to rollback transaction'\n");
244 std::stringstream tuple;
245 tuple << PQgetvalue(result, 0, 0) << " "
246 << PQgetvalue(result, 0, 1) << " "
247 << PQgetvalue(result, 0, 2);
255 if (CommitTransaction())
257 printfd(__FILE__, "POSTGRESQL_STORE::RestoreService(): 'Failed to commit transaction'\n");
264 //-----------------------------------------------------------------------------
265 int POSTGRESQL_STORE::AddService(const std::string & name) const
267 std::lock_guard lock(m_mutex);
269 if (PQstatus(connection) != CONNECTION_OK)
271 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
274 strError = "Connection lost";
275 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): '%s'\n", strError.c_str());
282 if (StartTransaction())
284 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to start transaction'\n");
288 std::string ename = name;
290 if (EscapeString(ename))
292 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to escape name'\n");
293 if (RollbackTransaction())
295 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to rollback transaction'\n");
300 std::ostringstream query;
301 query << "INSERT INTO tb_services \
302 (name, comment, cost, pay_day) \
304 ('" << ename << "', '', 0, 0)";
306 result = PQexec(connection, query.str().c_str());
308 if (PQresultStatus(result) != PGRES_COMMAND_OK)
310 strError = PQresultErrorMessage(result);
312 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): '%s'\n", strError.c_str());
313 if (RollbackTransaction())
315 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to rollback transaction'\n");
322 if (CommitTransaction())
324 printfd(__FILE__, "POSTGRESQL_STORE::AddService(): 'Failed to commit transaction'\n");
331 //-----------------------------------------------------------------------------
332 int POSTGRESQL_STORE::DelService(const std::string & name) const
334 std::lock_guard lock(m_mutex);
336 if (PQstatus(connection) != CONNECTION_OK)
338 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
341 strError = "Connection lost";
342 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): '%s'\n", strError.c_str());
349 if (StartTransaction())
351 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to start transaction'\n");
355 std::string ename = name;
357 if (EscapeString(ename))
359 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to escape name'\n");
360 if (RollbackTransaction())
362 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to rollback transaction'\n");
367 std::ostringstream query;
368 query << "DELETE FROM tb_services WHERE name = '" << ename << "'";
370 result = PQexec(connection, query.str().c_str());
372 if (PQresultStatus(result) != PGRES_COMMAND_OK)
374 strError = PQresultErrorMessage(result);
376 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): '%s'\n", strError.c_str());
377 if (RollbackTransaction())
379 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to rollback transaction'\n");
386 if (CommitTransaction())
388 printfd(__FILE__, "POSTGRESQL_STORE::DelService(): 'Failed to commit transaction'\n");
394 //-----------------------------------------------------------------------------