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