]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp
Simplified STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_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.6 $
26  *  $Date: 2009/07/15 11:19:42 $
27  *
28  */
29
30 #include <string>
31 #include <vector>
32 #include <sstream>
33
34 #include <libpq-fe.h>
35
36 #include "postgresql_store.h"
37 #include "stg/locker.h"
38 #include "stg/message.h"
39
40 //-----------------------------------------------------------------------------
41 int POSTGRESQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
42 {
43 STG_LOCKER lock(&mutex);
44
45 if (PQstatus(connection) != CONNECTION_OK)
46     {
47     printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
48     if (Reset())
49         {
50         strError = "Connection lost";
51         printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
52         return -1;
53         }
54     }
55
56 PGresult * result;
57
58 if (StartTransaction())
59     {
60     printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to start transaction'\n");
61     return -1;
62     }
63
64 std::string elogin = login;
65 std::string etext = msg->text;
66
67 if (EscapeString(elogin))
68     {
69     printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape login'\n");
70     if (RollbackTransaction())
71         {
72         printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
73         }
74     return -1;
75     }
76
77 if (EscapeString(etext))
78     {
79     printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape message text'\n");
80     if (RollbackTransaction())
81         {
82         printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
83         }
84     return -1;
85     }
86
87 std::ostringstream query;
88 query << "SELECT sp_add_message("
89       << "'" << elogin << "', "
90       << "CAST(1 AS SMALLINT), " // Here need to be a version, but, it's uninitiated actually
91       << "CAST(" << msg->header.type << " AS SMALLINT), "
92       << "CAST('" << Int2TS(msg->header.lastSendTime) << "' AS TIMESTAMP), "
93       << "CAST('" << Int2TS(msg->header.creationTime) << "' AS TIMESTAMP), "
94       << msg->header.showTime << ", "
95       << "CAST(" << msg->header.repeat << " AS SMALLINT), "
96       << msg->header.repeatPeriod << ", "
97       << "'" << etext << "')";
98
99 result = PQexec(connection, query.str().c_str());
100
101 if (PQresultStatus(result) != PGRES_TUPLES_OK)
102     {
103     strError = PQresultErrorMessage(result);
104     PQclear(result);
105     printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
106     if (RollbackTransaction())
107         {
108         printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
109         }
110     return -1;
111     }
112
113 int tuples = PQntuples(result);
114
115 if (tuples != 1)
116     {
117     strError = "Failed to fetch newlly added message ID";
118     printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
119     PQclear(result);
120     if (RollbackTransaction())
121         {
122         printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
123         }
124     return -1;
125     }
126
127 std::stringstream tuple;
128 tuple << PQgetvalue(result, 0, 0);
129
130 PQclear(result);
131
132 tuple >> msg->header.id;
133
134 if (CommitTransaction())
135     {
136     printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to commit transaction'\n");
137     return -1;
138     }
139
140 return 0;
141 }
142 //-----------------------------------------------------------------------------
143 int POSTGRESQL_STORE::EditMessage(const STG_MSG & msg,
144                                   const std::string & login) const
145 {
146 STG_LOCKER lock(&mutex);
147
148 if (PQstatus(connection) != CONNECTION_OK)
149     {
150     printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
151     if (Reset())
152         {
153         strError = "Connection lost";
154         printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
155         return -1;
156         }
157     }
158
159 PGresult * result;
160
161 if (StartTransaction())
162     {
163     printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to start transaction'\n");
164     return -1;
165     }
166
167 std::string elogin = login;
168 std::string etext = msg.text;
169
170 if (EscapeString(elogin))
171     {
172     printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape login'\n");
173     if (RollbackTransaction())
174         {
175         printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
176         }
177     return -1;
178     }
179
180 if (EscapeString(etext))
181     {
182     printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape message text'\n");
183     if (RollbackTransaction())
184         {
185         printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
186         }
187     return -1;
188     }
189
190 std::ostringstream query;
191 query << "UPDATE tb_messages SET "
192           << "fk_user = (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'), "
193           << "ver = " << msg.header.ver << ", "
194           << "msg_type = " << msg.header.type << ", "
195           << "last_send_time = CAST('" << Int2TS(msg.header.lastSendTime) << "' AS TIMESTAMP), "
196           << "creation_time = CAST('" << Int2TS(msg.header.creationTime) << "' AS TIMESTAMP), "
197           << "show_time = " << msg.header.showTime << ", "
198           << "repeat = " << msg.header.repeat << ", "
199           << "repeat_period = " << msg.header.repeatPeriod << ", "
200           << "msg_text = '" << etext << "' "
201       << "WHERE pk_message = " << msg.header.id;
202
203 result = PQexec(connection, query.str().c_str());
204
205 if (PQresultStatus(result) != PGRES_COMMAND_OK)
206     {
207     strError = PQresultErrorMessage(result);
208     PQclear(result);
209     printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
210     if (RollbackTransaction())
211         {
212         printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
213         }
214     return -1;
215     }
216
217 PQclear(result);
218
219 if (CommitTransaction())
220     {
221     printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to commit transaction'\n");
222     return -1;
223     }
224
225 return 0;
226 }
227 //-----------------------------------------------------------------------------
228 int POSTGRESQL_STORE::GetMessage(uint64_t id,
229                                STG_MSG * msg,
230                                const std::string &) const
231 {
232 STG_LOCKER lock(&mutex);
233
234 if (PQstatus(connection) != CONNECTION_OK)
235     {
236     printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
237     if (Reset())
238         {
239         strError = "Connection lost";
240         printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): '%s'\n", strError.c_str());
241         return -1;
242         }
243     }
244
245 PGresult * result;
246
247 if (StartTransaction())
248     {
249     printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to start transaction'\n");
250     return -1;
251     }
252
253 std::ostringstream query;
254 query << "SELECT ver, msg_type, last_send_time, \
255                  creation_time, show_time, repeat, \
256                  repeat_period, msg_text \
257           FROM tb_messages \
258           WHERE pk_message = " << id;
259
260 result = PQexec(connection, query.str().c_str());
261
262 if (PQresultStatus(result) != PGRES_TUPLES_OK)
263     {
264     strError = PQresultErrorMessage(result);
265     PQclear(result);
266     printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): '%s'\n", strError.c_str());
267     if (RollbackTransaction())
268         {
269         printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n");
270         }
271     return -1;
272     }
273
274 int tuples = PQntuples(result);
275
276 if (tuples != 1)
277     {
278     strError = "Failed to fetch message data";
279     printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
280     PQclear(result);
281     if (RollbackTransaction())
282         {
283         printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n");
284         }
285     return -1;
286     }
287
288 str2x(PQgetvalue(result, 0, 0), msg->header.ver);
289 str2x(PQgetvalue(result, 0, 1), msg->header.type);
290 msg->header.lastSendTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, 0, 2)));
291 msg->header.creationTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, 0, 3)));
292 str2x(PQgetvalue(result, 0, 4), msg->header.showTime);
293 str2x(PQgetvalue(result, 0, 5), msg->header.repeat);
294 str2x(PQgetvalue(result, 0, 6), msg->header.repeatPeriod);
295 msg->text = PQgetvalue(result, 0, 7);
296
297 PQclear(result);
298
299 if (CommitTransaction())
300     {
301     printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to commit transaction'\n");
302     return -1;
303     }
304
305 return 0;
306 }
307 //-----------------------------------------------------------------------------
308 int POSTGRESQL_STORE::DelMessage(uint64_t id, const std::string &) const
309 {
310 STG_LOCKER lock(&mutex);
311
312 if (PQstatus(connection) != CONNECTION_OK)
313     {
314     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
315     if (Reset())
316         {
317         strError = "Connection lost";
318         printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
319         return -1;
320         }
321     }
322
323 PGresult * result;
324
325 if (StartTransaction())
326     {
327     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to start transaction'\n");
328     return -1;
329     }
330
331 std::ostringstream query;
332 query << "DELETE FROM tb_messages WHERE pk_message = " << id;
333
334 result = PQexec(connection, query.str().c_str());
335
336 if (PQresultStatus(result) != PGRES_COMMAND_OK)
337     {
338     strError = PQresultErrorMessage(result);
339     PQclear(result);
340     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
341     if (RollbackTransaction())
342         {
343         printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to rollback transaction'\n");
344         }
345     return -1;
346     }
347
348 PQclear(result);
349
350 if (CommitTransaction())
351     {
352     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to commit transaction'\n");
353     return -1;
354     }
355
356 return 0;
357 }
358 //-----------------------------------------------------------------------------
359 int POSTGRESQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList,
360                                    const std::string & login) const
361 {
362 STG_LOCKER lock(&mutex);
363
364 if (PQstatus(connection) != CONNECTION_OK)
365     {
366     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
367     if (Reset())
368         {
369         strError = "Connection lost";
370         printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
371         return -1;
372         }
373     }
374
375 PGresult * result;
376
377 if (StartTransaction())
378     {
379     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to start transaction'\n");
380     return -1;
381     }
382
383 std::string elogin = login;
384
385 if (EscapeString(elogin))
386     {
387     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to escape login'\n");
388     if (RollbackTransaction())
389         {
390         printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
391         }
392     return -1;
393     }
394
395 std::ostringstream query;
396 query << "SELECT pk_message, ver, msg_type, \
397                  last_send_time, creation_time, show_time, \
398                  repeat, repeat_period \
399           FROM tb_messages \
400           WHERE fk_user IN \
401                 (SELECT pk_user FROM tb_users \
402           WHERE name = '" << elogin << "')";
403
404 result = PQexec(connection, query.str().c_str());
405
406 if (PQresultStatus(result) != PGRES_TUPLES_OK)
407     {
408     strError = PQresultErrorMessage(result);
409     PQclear(result);
410     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
411     if (RollbackTransaction())
412         {
413         printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
414         }
415     return -1;
416     }
417
418 int tuples = PQntuples(result);
419
420 for (int i = 0; i < tuples; ++i)
421     {
422     std::stringstream tuple;
423     STG_MSG_HDR header;
424     tuple << PQgetvalue(result, i, 0) << " ";
425     tuple << PQgetvalue(result, i, 1) << " ";
426     tuple << PQgetvalue(result, i, 2) << " ";
427     header.lastSendTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, i, 3)));
428     header.creationTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, i, 4)));
429     tuple << PQgetvalue(result, i, 5) << " ";
430     tuple << PQgetvalue(result, i, 6) << " ";
431     tuple << PQgetvalue(result, i, 7) << " ";
432
433     tuple >> header.id;
434     tuple >> header.ver;
435     tuple >> header.type;
436     tuple >> header.showTime;
437     tuple >> header.repeat;
438     tuple >> header.repeatPeriod;
439     hdrsList->push_back(header);
440     }
441
442 PQclear(result);
443
444 if (CommitTransaction())
445     {
446     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to commit transaction'\n");
447     return -1;
448     }
449
450 return 0;
451 }
452 //-----------------------------------------------------------------------------
453