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