]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store_messages.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  *  Messages manipualtion methods
24  *
25  *  $Revision: 1.10 $
26  *  $Date: 2009/03/03 16:16:23 $
27  *
28  */
29
30 #include "firebird_store.h"
31
32 #include "stg/ibpp.h"
33 #include "stg/message.h"
34 #include "stg/common.h"
35
36 //-----------------------------------------------------------------------------
37 int FIREBIRD_STORE::AddMessage(STG::Message * msg, const std::string & login) const
38 {
39 std::lock_guard lock(m_mutex);
40
41 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
42 IBPP::Statement st = IBPP::StatementFactory(db, tr);
43
44 try
45     {
46     tr->Start();
47     st->Prepare("execute procedure sp_add_message(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
48     st->Set(1, login);
49     st->Set(2, (int32_t)msg->header.ver);
50     st->Set(3, (int32_t)msg->header.type);
51     st->Set(4, (int32_t)msg->header.lastSendTime);
52     st->Set(5, (int32_t)msg->header.creationTime);
53     st->Set(6, (int32_t)msg->header.showTime);
54     st->Set(7, msg->header.repeat);
55     st->Set(8, (int32_t)msg->header.repeatPeriod);
56     st->Set(9, msg->text);
57     st->Execute();
58     st->Get(1, (int64_t &)msg->header.id);
59     tr->Commit();
60     }
61
62 catch (IBPP::Exception & ex)
63     {
64     tr->Rollback();
65     strError = "IBPP exception";
66     printfd(__FILE__, ex.what());
67     return -1;
68     }
69
70 return 0;
71 }
72 //-----------------------------------------------------------------------------
73 int FIREBIRD_STORE::EditMessage(const STG::Message & msg,
74                                 const std::string & login) const
75 {
76 std::lock_guard lock(m_mutex);
77
78 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
79 IBPP::Statement st = IBPP::StatementFactory(db, tr);
80
81 try
82     {
83     tr->Start();
84     st->Prepare("execute procedure sp_add_message(?, ?, ?, ?, ?, ?, ?, ?, ?)");
85     st->Set(1, (int64_t)msg.header.id);
86     st->Set(2, login);
87     st->Set(3, (int32_t)msg.header.ver);
88     st->Set(4, (int32_t)msg.header.type);
89     st->Set(5, (int32_t)msg.header.lastSendTime);
90     st->Set(6, (int32_t)msg.header.creationTime);
91     st->Set(7, (int32_t)msg.header.showTime);
92     st->Set(8, msg.header.repeat);
93     st->Set(9, (int32_t)msg.header.repeatPeriod);
94     st->Set(10, msg.text.c_str());
95     st->Execute();
96     tr->Commit();
97     }
98
99 catch (IBPP::Exception & ex)
100     {
101     tr->Rollback();
102     strError = "IBPP exception";
103     printfd(__FILE__, ex.what());
104     return -1;
105     }
106
107 return 0;
108 }
109 //-----------------------------------------------------------------------------
110 int FIREBIRD_STORE::GetMessage(uint64_t id,
111                                STG::Message * msg,
112                                const std::string &) const
113 {
114 std::lock_guard lock(m_mutex);
115
116 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
117 IBPP::Statement st = IBPP::StatementFactory(db, tr);
118
119 try
120     {
121     tr->Start();
122     st->Prepare("select * from tb_messages where pk_message = ?");
123     st->Set(1, (int64_t)id);
124     st->Execute();
125     if (st->Fetch())
126         {
127         st->Get(1, (int64_t &)msg->header.id);
128         st->Get(3, (int32_t &)msg->header.ver);
129         st->Get(4, (int32_t &)msg->header.type);
130         st->Get(5, (int32_t &)msg->header.lastSendTime);
131         st->Get(6, (int32_t &)msg->header.creationTime);
132         st->Get(7, (int32_t &)msg->header.showTime);
133         st->Get(8, msg->header.repeat);
134         st->Get(9, (int32_t &)msg->header.repeatPeriod);
135         st->Get(10, msg->text);
136         }
137     else
138         {
139         strprintf(&strError, "Message with id = %d not found in database", id);
140     printfd(__FILE__, "Message with id - %d not found in database\n", id);
141         tr->Rollback();
142         return -1;
143         }
144     tr->Commit();
145     }
146
147 catch (IBPP::Exception & ex)
148     {
149     tr->Rollback();
150     strError = "IBPP exception";
151     printfd(__FILE__, ex.what());
152     return -1;
153     }
154
155 return 0;
156 }
157 //-----------------------------------------------------------------------------
158 int FIREBIRD_STORE::DelMessage(uint64_t id, const std::string &) const
159 {
160 std::lock_guard lock(m_mutex);
161
162 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
163 IBPP::Statement st = IBPP::StatementFactory(db, tr);
164
165 try
166     {
167     tr->Start();
168     st->Prepare("delete from tb_messages where pk_message = ?");
169     st->Set(1, (int64_t)id);
170     st->Execute();
171     tr->Commit();
172     }
173
174 catch (IBPP::Exception & ex)
175     {
176     tr->Rollback();
177     strError = "IBPP exception";
178     printfd(__FILE__, ex.what());
179     return -1;
180     }
181
182 return 0;
183 }
184 //-----------------------------------------------------------------------------
185 int FIREBIRD_STORE::GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList,
186                                    const std::string & login) const
187 {
188 std::lock_guard lock(m_mutex);
189
190 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
191 IBPP::Statement st = IBPP::StatementFactory(db, tr);
192
193 STG::Message::Header header;
194
195 try
196     {
197     tr->Start();
198     st->Prepare("select pk_message, ver, msg_type, \
199                         last_send_time, creation_time, \
200             show_time, repeat, repeat_period \
201          from tb_messages where \
202                 fk_user = (select pk_user from tb_users where name = ?)");
203     st->Set(1, login);
204     st->Execute();
205     while (st->Fetch())
206         {
207         st->Get(1, (int64_t &)header.id);
208         st->Get(2, (int32_t &)header.ver);
209         st->Get(3, (int32_t &)header.type);
210         st->Get(4, (int32_t &)header.lastSendTime);
211         st->Get(5, (int32_t &)header.creationTime);
212         st->Get(6, (int32_t &)header.showTime);
213         st->Get(7, header.repeat);
214         st->Get(8, (int32_t &)header.repeatPeriod);
215         hdrsList->push_back(header);
216         }
217     tr->Commit();
218     }
219
220 catch (IBPP::Exception & ex)
221     {
222     tr->Rollback();
223     strError = "IBPP exception";
224     printfd(__FILE__, ex.what());
225     return -1;
226     }
227
228 return 0;
229 }
230 //-----------------------------------------------------------------------------
231