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