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/locker.h"
33 #include "stg/common.h"
41 //-----------------------------------------------------------------------------
42 int POSTGRESQL_STORE::GetCorpsList(std::vector<std::string> * corpsList) const
44 STG_LOCKER lock(&mutex);
46 if (PQstatus(connection) != CONNECTION_OK)
48 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
51 strError = "Connection lost";
52 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
59 if (StartTransaction())
61 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to start transaction'\n");
65 result = PQexec(connection, "SELECT name FROM tb_corporations");
67 if (PQresultStatus(result) != PGRES_TUPLES_OK)
69 strError = PQresultErrorMessage(result);
71 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
72 if (RollbackTransaction())
74 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to rollback transaction'\n");
79 int tuples = PQntuples(result);
81 for (int i = 0; i < tuples; ++i)
83 corpsList->push_back(PQgetvalue(result, i, 0));
88 if (CommitTransaction())
90 printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to commit transaction'\n");
97 //-----------------------------------------------------------------------------
98 int POSTGRESQL_STORE::SaveCorp(const STG::CorpConf & cc) const
100 STG_LOCKER lock(&mutex);
102 if (PQstatus(connection) != CONNECTION_OK)
104 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
107 strError = "Connection lost";
108 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
115 if (StartTransaction())
117 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to start transaction'\n");
121 std::string ename = cc.name;
123 if (EscapeString(ename))
125 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to escape name'\n");
126 if (RollbackTransaction())
128 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
133 std::ostringstream query;
134 query << "UPDATE tb_corporations SET "
135 << "cash = " << cc.cash
136 << "WHERE name = '" << ename << "'";
138 result = PQexec(connection, query.str().c_str());
140 if (PQresultStatus(result) != PGRES_COMMAND_OK)
142 strError = PQresultErrorMessage(result);
144 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
145 if (RollbackTransaction())
147 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
154 if (CommitTransaction())
156 printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to commit transaction'\n");
163 //-----------------------------------------------------------------------------
164 int POSTGRESQL_STORE::RestoreCorp(STG::CorpConf * cc, const std::string & name) const
166 STG_LOCKER lock(&mutex);
168 if (PQstatus(connection) != CONNECTION_OK)
170 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
173 strError = "Connection lost";
174 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
181 if (StartTransaction())
183 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to start transaction'\n");
187 std::string ename = name;
189 if (EscapeString(ename))
191 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to escape name'\n");
192 if (RollbackTransaction())
194 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
199 std::ostringstream query;
200 query << "SELECT cash FROM tb_corporations WHERE name = '" << ename << "'";
202 result = PQexec(connection, query.str().c_str());
204 if (PQresultStatus(result) != PGRES_TUPLES_OK)
206 strError = PQresultErrorMessage(result);
208 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
209 if (RollbackTransaction())
211 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
216 int tuples = PQntuples(result);
220 strError = "Failed to fetch corp's data";
221 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
223 if (RollbackTransaction())
225 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
230 std::stringstream tuple;
231 tuple << PQgetvalue(result, 0, 0);
237 if (CommitTransaction())
239 printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to commit transaction'\n");
246 //-----------------------------------------------------------------------------
247 int POSTGRESQL_STORE::AddCorp(const std::string & name) const
249 STG_LOCKER lock(&mutex);
251 if (PQstatus(connection) != CONNECTION_OK)
253 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
256 strError = "Connection lost";
257 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
264 if (StartTransaction())
266 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to start transaction'\n");
270 std::string ename = name;
272 if (EscapeString(ename))
274 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to escape name'\n");
275 if (RollbackTransaction())
277 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
282 std::ostringstream query;
283 query << "INSERT INTO tb_corporations \
286 ('" << ename << "', 0)";
288 result = PQexec(connection, query.str().c_str());
290 if (PQresultStatus(result) != PGRES_COMMAND_OK)
292 strError = PQresultErrorMessage(result);
294 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
295 if (RollbackTransaction())
297 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
304 if (CommitTransaction())
306 printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to commit transaction'\n");
313 //-----------------------------------------------------------------------------
314 int POSTGRESQL_STORE::DelCorp(const std::string & name) const
316 STG_LOCKER lock(&mutex);
318 if (PQstatus(connection) != CONNECTION_OK)
320 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
323 strError = "Connection lost";
324 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
331 if (StartTransaction())
333 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to start transaction'\n");
337 std::string ename = name;
339 if (EscapeString(ename))
341 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to escape name'\n");
342 if (RollbackTransaction())
344 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
349 std::ostringstream query;
350 query << "DELETE FROM tb_corporations WHERE name = '" << ename << "'";
352 result = PQexec(connection, query.str().c_str());
354 if (PQresultStatus(result) != PGRES_COMMAND_OK)
356 strError = PQresultErrorMessage(result);
358 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
359 if (RollbackTransaction())
361 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
368 if (CommitTransaction())
370 printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to commit transaction'\n");
376 //-----------------------------------------------------------------------------