]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_corporations.cpp
Simplified STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_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.5 $
25  *  $Date: 2007/12/23 13:39:59 $
26  *
27  */
28
29 #include "firebird_store.h"
30 #include "stg/ibpp.h"
31
32 //-----------------------------------------------------------------------------
33 int FIREBIRD_STORE::GetCorpsList(std::vector<std::string> * corpsList) const
34 {
35 STG_LOCKER lock(&mutex);
36
37 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
38 IBPP::Statement st = IBPP::StatementFactory(db, tr);
39
40 std::string name;
41
42 try
43     {
44     tr->Start();
45     st->Execute("select name from tb_corporations");
46     while (st->Fetch())
47         {
48         st->Get(1, name);
49         corpsList->push_back(name);
50         }
51     tr->Commit();
52     }
53
54 catch (IBPP::Exception & ex)
55     {
56     tr->Rollback();
57     strError = "IBPP exception";
58     printfd(__FILE__, ex.what());
59     return -1;
60     }
61
62 return 0;
63 }
64 //-----------------------------------------------------------------------------
65 int FIREBIRD_STORE::SaveCorp(const CORP_CONF & cc) const
66 {
67 STG_LOCKER lock(&mutex);
68
69 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
70 IBPP::Statement st = IBPP::StatementFactory(db, tr);
71
72 try
73     {
74     tr->Start();
75     st->Execute("update tb_corporations set cash = ? where name = ?");
76     st->Set(1, cc.cash);
77     st->Set(2, cc.name);
78     st->Execute();
79     tr->Commit();
80     }
81
82 catch (IBPP::Exception & ex)
83     {
84     tr->Rollback();
85     strError = "IBPP exception";
86     printfd(__FILE__, ex.what());
87     return -1;
88     }
89
90 return 0;
91 }
92 //-----------------------------------------------------------------------------
93 int FIREBIRD_STORE::RestoreCorp(CORP_CONF * cc, const std::string & name) const
94 {
95 STG_LOCKER lock(&mutex);
96
97 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
98 IBPP::Statement st = IBPP::StatementFactory(db, tr);
99
100 try
101     {
102     tr->Start();
103     st->Prepare("select cash from tb_corporations where name = ?");
104     st->Set(1, name);
105     st->Execute();
106     if (st->Fetch())
107         {
108         st->Get(1, cc->cash);
109         }
110     else
111         {
112         strError = "Corporation \"" + name + "\" not found in database";
113         tr->Rollback();
114     printfd(__FILE__, "Corporation '%s' not found in database\n", name.c_str());
115         return -1;
116         }
117     tr->Commit();
118     }
119
120 catch (IBPP::Exception & ex)
121     {
122     tr->Rollback();
123     strError = "IBPP exception";
124     printfd(__FILE__, ex.what());
125     return -1;
126     }
127
128 return 0;
129 }
130 //-----------------------------------------------------------------------------
131 int FIREBIRD_STORE::AddCorp(const std::string & name) const
132 {
133 STG_LOCKER lock(&mutex);
134
135 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
136 IBPP::Statement st = IBPP::StatementFactory(db, tr);
137
138 try
139     {
140     tr->Start();
141     st->Prepare("insert into tb_corporations (name, cash), values (?, 0)");
142     st->Set(1, name);
143     st->Execute();
144     tr->Commit();
145     }
146
147 catch (IBPP::Exception & ex)
148     {
149     tr->Rollback();
150     strError = "IBPP exception";
151     printfd(__FILE__, ex.what());
152     return -1;
153     }
154
155 return 0;
156 }
157 //-----------------------------------------------------------------------------
158 int FIREBIRD_STORE::DelCorp(const std::string & name) const
159 {
160 STG_LOCKER lock(&mutex);
161
162 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
163 IBPP::Statement st = IBPP::StatementFactory(db, tr);
164
165 try
166     {
167     tr->Start();
168     st->Prepare("delete from tb_corporations where name = ?");
169     st->Set(1, name);
170     st->Execute();
171     tr->Commit();
172     }
173
174 catch (IBPP::Exception & ex)
175     {
176     tr->Rollback();
177     strError = "IBPP exception";
178     printfd(__FILE__, ex.what());
179     return -1;
180     }
181
182 return 0;
183 }
184 //-----------------------------------------------------------------------------
185