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