]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_services.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store_services.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 /*
23  *  Services manipulation methods
24  *
25  *  $Revision: 1.6 $
26  *  $Date: 2009/05/13 13:19:33 $
27  *
28  */
29
30 #include "firebird_store.h"
31
32 #include "stg/ibpp.h"
33 #include "stg/service_conf.h"
34 #include "stg/common.h"
35
36 //-----------------------------------------------------------------------------
37 int FIREBIRD_STORE::GetServicesList(std::vector<std::string> * servicesList) const
38 {
39 std::lock_guard lock(m_mutex);
40
41 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
42 IBPP::Statement st = IBPP::StatementFactory(db, tr);
43
44 try
45     {
46     tr->Start();
47     st->Execute("select name from tb_services");
48     while (st->Fetch())
49         {
50         std::string name;
51         st->Get(1, name);
52         servicesList->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::SaveService(const STG::ServiceConf & sc) const
69 {
70 std::lock_guard lock(m_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->Prepare("update tb_services set \
79             comments = ?, \
80             cost = ?, \
81             pay_day = ? \
82          where name = ?");
83     st->Set(1, sc.comment);
84     st->Set(2, sc.cost);
85     st->Set(3, static_cast<int16_t>(sc.payDay));
86     st->Set(4, sc.name);
87     st->Execute();
88     tr->Commit();
89     }
90
91 catch (IBPP::Exception & ex)
92     {
93     tr->Rollback();
94     strError = "IBPP exception";
95     printfd(__FILE__, ex.what());
96     return -1;
97     }
98
99 return 0;
100 }
101 //-----------------------------------------------------------------------------
102 int FIREBIRD_STORE::RestoreService(STG::ServiceConf * sc,
103                                    const std::string & name) const
104 {
105 std::lock_guard lock(m_mutex);
106
107 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
108 IBPP::Statement st = IBPP::StatementFactory(db, tr);
109
110 try
111     {
112     tr->Start();
113     st->Prepare("select * from tb_services where name = ?");
114     st->Set(1, name);
115     st->Execute();
116     if (st->Fetch())
117         {
118         st->Get(3, sc->comment);
119         st->Get(4, sc->cost);
120         int16_t pd;
121         st->Get(5, pd);
122         sc->payDay = static_cast<uint8_t>(pd);
123         }
124     else
125         {
126         strError = "Service \"" + name + "\" not found in database";
127     printfd(__FILE__, "Service '%s' not found in database\n", name.c_str());
128         tr->Rollback();
129         return -1;
130         }
131     tr->Commit();
132     }
133
134 catch (IBPP::Exception & ex)
135     {
136     tr->Rollback();
137     strError = "IBPP exception";
138     printfd(__FILE__, ex.what());
139     return -1;
140     }
141
142 return 0;
143 }
144 //-----------------------------------------------------------------------------
145 int FIREBIRD_STORE::AddService(const std::string & name) const
146 {
147 std::lock_guard lock(m_mutex);
148
149 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
150 IBPP::Statement st = IBPP::StatementFactory(db, tr);
151
152 try
153     {
154     tr->Start();
155     st->Prepare("insert into tb_services (name, comment, cost, pay_day) \
156             values (?, '', 0, 0)");
157     st->Set(1, name);
158     st->Execute();
159     tr->Commit();
160     }
161
162 catch (IBPP::Exception & ex)
163     {
164     tr->Rollback();
165     strError = "IBPP exception";
166     printfd(__FILE__, ex.what());
167     return -1;
168     }
169
170 return 0;
171 }
172 //-----------------------------------------------------------------------------
173 int FIREBIRD_STORE::DelService(const std::string & name) const
174 {
175 std::lock_guard lock(m_mutex);
176
177 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
178 IBPP::Statement st = IBPP::StatementFactory(db, tr);
179
180 try
181     {
182     tr->Start();
183     st->Prepare("execute procedure sp_delete_service(?)");
184     st->Set(1, name);
185     st->Execute();
186     tr->Commit();
187     }
188
189 catch (IBPP::Exception & ex)
190     {
191     tr->Rollback();
192     strError = "IBPP exception";
193     printfd(__FILE__, ex.what());
194     return -1;
195     }
196
197 return 0;
198 }
199 //-----------------------------------------------------------------------------
200