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 $
35 #include "postgresql_store.h"
36 #include "stg_locker.h"
38 //-----------------------------------------------------------------------------
39 int POSTGRESQL_STORE::GetCorpsList(vector<string> * corpsList) const
41 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
43 if (PQstatus(connection) != CONNECTION_OK)
45 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
48 strError = "Connection lost";
49 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
56 if (StartTransaction())
58 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to start transaction'\n");
62 result = PQexec(connection, "SELECT name FROM tb_corporations");
64 if (PQresultStatus(result) != PGRES_TUPLES_OK)
66 strError = PQresultErrorMessage(result);
68 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
69 if (RollbackTransaction())
71 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to rollback transaction'\n");
76 int tuples = PQntuples(result);
78 for (int i = 0; i < tuples; ++i)
80 corpsList->push_back(PQgetvalue(result, i, 0));
85 if (CommitTransaction())
87 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to commit transaction'\n");
94 //-----------------------------------------------------------------------------
95 int POSTGRESQL_STORE::SaveCorp(const CORP_CONF & cc) const
97 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
99 if (PQstatus(connection) != CONNECTION_OK)
101 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
104 strError = "Connection lost";
105 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
112 if (StartTransaction())
114 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to start transaction'\n");
118 std::string ename = cc.name;
120 if (EscapeString(ename))
122 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to escape name'\n");
123 if (RollbackTransaction())
125 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
130 std::stringstream query;
131 query << "UPDATE tb_corporations SET "
132 << "cash = " << cc.cash
133 << "WHERE name = '" << ename << "'";
135 result = PQexec(connection, query.str().c_str());
137 if (PQresultStatus(result) != PGRES_COMMAND_OK)
139 strError = PQresultErrorMessage(result);
141 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
142 if (RollbackTransaction())
144 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
151 if (CommitTransaction())
153 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to commit transaction'\n");
160 //-----------------------------------------------------------------------------
161 int POSTGRESQL_STORE::RestoreCorp(CORP_CONF * cc, const string & name) const
163 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
165 if (PQstatus(connection) != CONNECTION_OK)
167 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
170 strError = "Connection lost";
171 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
178 if (StartTransaction())
180 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to start transaction'\n");
184 std::string ename = name;
186 if (EscapeString(ename))
188 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to escape name'\n");
189 if (RollbackTransaction())
191 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
196 std::stringstream query;
197 query << "SELECT cash FROM tb_corporations WHERE name = '" << ename << "'";
199 result = PQexec(connection, query.str().c_str());
201 if (PQresultStatus(result) != PGRES_TUPLES_OK)
203 strError = PQresultErrorMessage(result);
205 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
206 if (RollbackTransaction())
208 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
213 int tuples = PQntuples(result);
217 strError = "Failed to fetch corp's data";
218 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
220 if (RollbackTransaction())
222 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
227 std::stringstream tuple;
228 tuple << PQgetvalue(result, 0, 0);
234 if (CommitTransaction())
236 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to commit transaction'\n");
243 //-----------------------------------------------------------------------------
244 int POSTGRESQL_STORE::AddCorp(const string & name) const
246 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
248 if (PQstatus(connection) != CONNECTION_OK)
250 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
253 strError = "Connection lost";
254 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
261 if (StartTransaction())
263 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to start transaction'\n");
267 std::string ename = name;
269 if (EscapeString(ename))
271 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to escape name'\n");
272 if (RollbackTransaction())
274 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
279 std::stringstream query;
280 query << "INSERT INTO tb_corporations \
283 ('" << ename << "', 0)";
285 result = PQexec(connection, query.str().c_str());
287 if (PQresultStatus(result) != PGRES_COMMAND_OK)
289 strError = PQresultErrorMessage(result);
291 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
292 if (RollbackTransaction())
294 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
301 if (CommitTransaction())
303 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to commit transaction'\n");
310 //-----------------------------------------------------------------------------
311 int POSTGRESQL_STORE::DelCorp(const string & name) const
313 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
315 if (PQstatus(connection) != CONNECTION_OK)
317 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
320 strError = "Connection lost";
321 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
328 if (StartTransaction())
330 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to start transaction'\n");
334 std::string ename = name;
336 if (EscapeString(ename))
338 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to escape name'\n");
339 if (RollbackTransaction())
341 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
346 std::stringstream query;
347 query << "DELETE FROM tb_corporations WHERE name = '" << ename << "'";
349 result = PQexec(connection, query.str().c_str());
351 if (PQresultStatus(result) != PGRES_COMMAND_OK)
353 strError = PQresultErrorMessage(result);
355 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
356 if (RollbackTransaction())
358 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
365 if (CommitTransaction())
367 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to commit transaction'\n");
373 //-----------------------------------------------------------------------------