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