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 * Corporations manipulation methods
25 * $Date: 2009/06/09 12:32:39 $
29 #include "postgresql_store.h"
31 #include "stg/corp_conf.h"
32 #include "stg/common.h"
40 //-----------------------------------------------------------------------------
41 int POSTGRESQL_STORE::GetCorpsList(std::vector<std::string> * corpsList) const
43 std::lock_guard lock(m_mutex);
45 if (PQstatus(connection) != CONNECTION_OK)
47 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
50 strError = "Connection lost";
51 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
58 if (StartTransaction())
60 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to start transaction'\n");
64 result = PQexec(connection, "SELECT name FROM tb_corporations");
66 if (PQresultStatus(result) != PGRES_TUPLES_OK)
68 strError = PQresultErrorMessage(result);
70 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
71 if (RollbackTransaction())
73 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to rollback transaction'\n");
78 int tuples = PQntuples(result);
80 for (int i = 0; i < tuples; ++i)
82 corpsList->push_back(PQgetvalue(result, i, 0));
87 if (CommitTransaction())
89 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to commit transaction'\n");
96 //-----------------------------------------------------------------------------
97 int POSTGRESQL_STORE::SaveCorp(const STG::CorpConf & cc) const
99 std::lock_guard lock(m_mutex);
101 if (PQstatus(connection) != CONNECTION_OK)
103 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
106 strError = "Connection lost";
107 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
114 if (StartTransaction())
116 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to start transaction'\n");
120 std::string ename = cc.name;
122 if (EscapeString(ename))
124 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to escape name'\n");
125 if (RollbackTransaction())
127 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
132 std::ostringstream query;
133 query << "UPDATE tb_corporations SET "
134 << "cash = " << cc.cash
135 << "WHERE name = '" << ename << "'";
137 result = PQexec(connection, query.str().c_str());
139 if (PQresultStatus(result) != PGRES_COMMAND_OK)
141 strError = PQresultErrorMessage(result);
143 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
144 if (RollbackTransaction())
146 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
153 if (CommitTransaction())
155 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to commit transaction'\n");
162 //-----------------------------------------------------------------------------
163 int POSTGRESQL_STORE::RestoreCorp(STG::CorpConf * cc, const std::string & name) const
165 std::lock_guard lock(m_mutex);
167 if (PQstatus(connection) != CONNECTION_OK)
169 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
172 strError = "Connection lost";
173 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
180 if (StartTransaction())
182 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to start transaction'\n");
186 std::string ename = name;
188 if (EscapeString(ename))
190 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to escape name'\n");
191 if (RollbackTransaction())
193 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
198 std::ostringstream query;
199 query << "SELECT cash FROM tb_corporations WHERE name = '" << ename << "'";
201 result = PQexec(connection, query.str().c_str());
203 if (PQresultStatus(result) != PGRES_TUPLES_OK)
205 strError = PQresultErrorMessage(result);
207 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
208 if (RollbackTransaction())
210 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
215 int tuples = PQntuples(result);
219 strError = "Failed to fetch corp's data";
220 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
222 if (RollbackTransaction())
224 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
229 std::stringstream tuple;
230 tuple << PQgetvalue(result, 0, 0);
236 if (CommitTransaction())
238 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to commit transaction'\n");
245 //-----------------------------------------------------------------------------
246 int POSTGRESQL_STORE::AddCorp(const std::string & name) const
248 std::lock_guard lock(m_mutex);
250 if (PQstatus(connection) != CONNECTION_OK)
252 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
255 strError = "Connection lost";
256 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
263 if (StartTransaction())
265 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to start transaction'\n");
269 std::string ename = name;
271 if (EscapeString(ename))
273 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to escape name'\n");
274 if (RollbackTransaction())
276 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
281 std::ostringstream query;
282 query << "INSERT INTO tb_corporations \
285 ('" << ename << "', 0)";
287 result = PQexec(connection, query.str().c_str());
289 if (PQresultStatus(result) != PGRES_COMMAND_OK)
291 strError = PQresultErrorMessage(result);
293 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
294 if (RollbackTransaction())
296 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
303 if (CommitTransaction())
305 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to commit transaction'\n");
312 //-----------------------------------------------------------------------------
313 int POSTGRESQL_STORE::DelCorp(const std::string & name) const
315 std::lock_guard lock(m_mutex);
317 if (PQstatus(connection) != CONNECTION_OK)
319 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
322 strError = "Connection lost";
323 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
330 if (StartTransaction())
332 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to start transaction'\n");
336 std::string ename = name;
338 if (EscapeString(ename))
340 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to escape name'\n");
341 if (RollbackTransaction())
343 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
348 std::ostringstream query;
349 query << "DELETE FROM tb_corporations WHERE name = '" << ename << "'";
351 result = PQexec(connection, query.str().c_str());
353 if (PQresultStatus(result) != PGRES_COMMAND_OK)
355 strError = PQresultErrorMessage(result);
357 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
358 if (RollbackTransaction())
360 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
367 if (CommitTransaction())
369 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to commit transaction'\n");
375 //-----------------------------------------------------------------------------