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