]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_corporations.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store_corporations.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Corporations manipulation methods
23  *
24  *  $Revision: 1.2 $
25  *  $Date: 2009/06/09 12:32:39 $
26  *
27  */
28
29 #include "postgresql_store.h"
30
31 #include "stg/corp_conf.h"
32 #include "stg/common.h"
33
34 #include <string>
35 #include <vector>
36 #include <sstream>
37
38 #include <libpq-fe.h>
39
40 //-----------------------------------------------------------------------------
41 int POSTGRESQL_STORE::GetCorpsList(std::vector<std::string> * corpsList) const
42 {
43 std::lock_guard lock(m_mutex);
44
45 if (PQstatus(connection) != CONNECTION_OK)
46     {
47     printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
48     if (Reset())
49         {
50         strError = "Connection lost";
51         printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
52         return -1;
53         }
54     }
55
56 PGresult * result;
57
58 if (StartTransaction())
59     {
60     printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to start transaction'\n");
61     return -1;
62     }
63
64 result = PQexec(connection, "SELECT name FROM tb_corporations");
65
66 if (PQresultStatus(result) != PGRES_TUPLES_OK)
67     {
68     strError = PQresultErrorMessage(result);
69     PQclear(result);
70     printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): '%s'\n", strError.c_str());
71     if (RollbackTransaction())
72         {
73         printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to rollback transaction'\n");
74         }
75     return -1;
76     }
77
78 int tuples = PQntuples(result);
79
80 for (int i = 0; i < tuples; ++i)
81     {
82     corpsList->push_back(PQgetvalue(result, i, 0));
83     }
84
85 PQclear(result);
86
87 if (CommitTransaction())
88     {
89     printfd(__FILE__, "POSTGRESQL_STORE::GetCorpsList(): 'Failed to commit transaction'\n");
90     return -1;
91     }
92
93 return 0;
94 }
95
96 //-----------------------------------------------------------------------------
97 int POSTGRESQL_STORE::SaveCorp(const STG::CorpConf & cc) const
98 {
99 std::lock_guard lock(m_mutex);
100
101 if (PQstatus(connection) != CONNECTION_OK)
102     {
103     printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
104     if (Reset())
105         {
106         strError = "Connection lost";
107         printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
108         return -1;
109         }
110     }
111
112 PGresult * result;
113
114 if (StartTransaction())
115     {
116     printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to start transaction'\n");
117     return -1;
118     }
119
120 std::string ename = cc.name;
121
122 if (EscapeString(ename))
123     {
124     printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to escape name'\n");
125     if (RollbackTransaction())
126         {
127         printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
128         }
129     return -1;
130     }
131
132 std::ostringstream query;
133 query << "UPDATE tb_corporations SET "
134           << "cash = " << cc.cash
135       << "WHERE name = '" << ename << "'";
136
137 result = PQexec(connection, query.str().c_str());
138
139 if (PQresultStatus(result) != PGRES_COMMAND_OK)
140     {
141     strError = PQresultErrorMessage(result);
142     PQclear(result);
143     printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): '%s'\n", strError.c_str());
144     if (RollbackTransaction())
145         {
146         printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to rollback transaction'\n");
147         }
148     return -1;
149     }
150
151 PQclear(result);
152
153 if (CommitTransaction())
154     {
155     printfd(__FILE__, "POSTGRESQL_STORE::SaveCorp(): 'Failed to commit transaction'\n");
156     return -1;
157     }
158
159 return 0;
160 }
161
162 //-----------------------------------------------------------------------------
163 int POSTGRESQL_STORE::RestoreCorp(STG::CorpConf * cc, const std::string & name) const
164 {
165 std::lock_guard lock(m_mutex);
166
167 if (PQstatus(connection) != CONNECTION_OK)
168     {
169     printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
170     if (Reset())
171         {
172         strError = "Connection lost";
173         printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
174         return -1;
175         }
176     }
177
178 PGresult * result;
179
180 if (StartTransaction())
181     {
182     printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to start transaction'\n");
183     return -1;
184     }
185
186 std::string ename = name;
187
188 if (EscapeString(ename))
189     {
190     printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to escape name'\n");
191     if (RollbackTransaction())
192         {
193         printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
194         }
195     return -1;
196     }
197
198 std::ostringstream query;
199 query << "SELECT cash FROM tb_corporations WHERE name = '" << ename << "'";
200
201 result = PQexec(connection, query.str().c_str());
202
203 if (PQresultStatus(result) != PGRES_TUPLES_OK)
204     {
205     strError = PQresultErrorMessage(result);
206     PQclear(result);
207     printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): '%s'\n", strError.c_str());
208     if (RollbackTransaction())
209         {
210         printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
211         }
212     return -1;
213     }
214
215 int tuples = PQntuples(result);
216
217 if (tuples != 1)
218     {
219     strError = "Failed to fetch corp's data";
220     printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
221     PQclear(result);
222     if (RollbackTransaction())
223         {
224         printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to rollback transaction'\n");
225         }
226     return -1;
227     }
228
229 std::stringstream tuple;
230 tuple << PQgetvalue(result, 0, 0);
231
232 PQclear(result);
233
234 tuple >> cc->cash;
235
236 if (CommitTransaction())
237     {
238     printfd(__FILE__, "POSTGRESQL_STORE::RestoreCorp(): 'Failed to commit transaction'\n");
239     return -1;
240     }
241
242 return 0;
243 }
244
245 //-----------------------------------------------------------------------------
246 int POSTGRESQL_STORE::AddCorp(const std::string & name) const
247 {
248 std::lock_guard lock(m_mutex);
249
250 if (PQstatus(connection) != CONNECTION_OK)
251     {
252     printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
253     if (Reset())
254         {
255         strError = "Connection lost";
256         printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
257         return -1;
258         }
259     }
260
261 PGresult * result;
262
263 if (StartTransaction())
264     {
265     printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to start transaction'\n");
266     return -1;
267     }
268
269 std::string ename = name;
270
271 if (EscapeString(ename))
272     {
273     printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to escape name'\n");
274     if (RollbackTransaction())
275         {
276         printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
277         }
278     return -1;
279     }
280
281 std::ostringstream query;
282 query << "INSERT INTO tb_corporations \
283               (name, cash) \
284           VALUES \
285               ('" << ename << "', 0)";
286
287 result = PQexec(connection, query.str().c_str());
288
289 if (PQresultStatus(result) != PGRES_COMMAND_OK)
290     {
291     strError = PQresultErrorMessage(result);
292     PQclear(result);
293     printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): '%s'\n", strError.c_str());
294     if (RollbackTransaction())
295         {
296         printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to rollback transaction'\n");
297         }
298     return -1;
299     }
300
301 PQclear(result);
302
303 if (CommitTransaction())
304     {
305     printfd(__FILE__, "POSTGRESQL_STORE::AddCorp(): 'Failed to commit transaction'\n");
306     return -1;
307     }
308
309 return 0;
310 }
311
312 //-----------------------------------------------------------------------------
313 int POSTGRESQL_STORE::DelCorp(const std::string & name) const
314 {
315 std::lock_guard lock(m_mutex);
316
317 if (PQstatus(connection) != CONNECTION_OK)
318     {
319     printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
320     if (Reset())
321         {
322         strError = "Connection lost";
323         printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
324         return -1;
325         }
326     }
327
328 PGresult * result;
329
330 if (StartTransaction())
331     {
332     printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to start transaction'\n");
333     return -1;
334     }
335
336 std::string ename = name;
337
338 if (EscapeString(ename))
339     {
340     printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to escape name'\n");
341     if (RollbackTransaction())
342         {
343         printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
344         }
345     return -1;
346     }
347
348 std::ostringstream query;
349 query << "DELETE FROM tb_corporations WHERE name = '" << ename << "'";
350
351 result = PQexec(connection, query.str().c_str());
352
353 if (PQresultStatus(result) != PGRES_COMMAND_OK)
354     {
355     strError = PQresultErrorMessage(result);
356     PQclear(result);
357     printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): '%s'\n", strError.c_str());
358     if (RollbackTransaction())
359         {
360         printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to rollback transaction'\n");
361         }
362     return -1;
363     }
364
365 PQclear(result);
366
367 if (CommitTransaction())
368     {
369     printfd(__FILE__, "POSTGRESQL_STORE::DelCorp(): 'Failed to commit transaction'\n");
370     return -1;
371     }
372
373 return 0;
374 }
375 //-----------------------------------------------------------------------------
376