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