]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_services.cpp
Добавление исходников
[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 #include "ibpp.h"
32
33 //-----------------------------------------------------------------------------
34 int FIREBIRD_STORE::GetServicesList(vector<string> * servicesList) const
35 {
36 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
37
38 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
39 IBPP::Statement st = IBPP::StatementFactory(db, tr);
40
41 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, __FILE__, __LINE__);
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, 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 string & name) const
102 {
103 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
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         st->Get(5, sc->payDay);
119         }
120     else
121         {
122         strError = "Service \"" + name + "\" not found in database";
123     printfd(__FILE__, "Service '%s' not found in database\n", name.c_str());
124         tr->Rollback();
125         return -1;
126         }
127     tr->Commit();
128     }
129
130 catch (IBPP::Exception & ex)
131     {
132     tr->Rollback();
133     strError = "IBPP exception";
134     printfd(__FILE__, ex.what());
135     return -1;
136     }
137
138 return 0;
139 }
140 //-----------------------------------------------------------------------------
141 int FIREBIRD_STORE::AddService(const string & name) const
142 {
143 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
144
145 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
146 IBPP::Statement st = IBPP::StatementFactory(db, tr);
147
148 try
149     {
150     tr->Start();
151     st->Prepare("insert into tb_services (name, comment, cost, pay_day) \
152             values (?, '', 0, 0)");
153     st->Set(1, name);
154     st->Execute();
155     tr->Commit();
156     }
157
158 catch (IBPP::Exception & ex)
159     {
160     tr->Rollback();
161     strError = "IBPP exception";
162     printfd(__FILE__, ex.what());
163     return -1;
164     }
165
166 return 0;
167 }
168 //-----------------------------------------------------------------------------
169 int FIREBIRD_STORE::DelService(const string & name) const
170 {
171 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
172
173 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
174 IBPP::Statement st = IBPP::StatementFactory(db, tr);
175
176 try
177     {
178     tr->Start();
179     st->Prepare("execute procedure sp_delete_service(?)");
180     st->Set(1, name);
181     st->Execute();
182     tr->Commit();
183     }
184
185 catch (IBPP::Exception & ex)
186     {
187     tr->Rollback();
188     strError = "IBPP exception";
189     printfd(__FILE__, ex.what());
190     return -1;
191     }
192
193 return 0;
194 }
195 //-----------------------------------------------------------------------------
196