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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 * Messages manipualtion methods
26 * $Date: 2009/07/15 11:19:42 $
36 #include "postgresql_store.h"
37 #include "stg/locker.h"
38 #include "stg/message.h"
40 //-----------------------------------------------------------------------------
41 int POSTGRESQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
43 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
45 if (PQstatus(connection) != CONNECTION_OK)
47 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
50 strError = "Connection lost";
51 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
58 if (StartTransaction())
60 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to start transaction'\n");
64 std::string elogin = login;
65 std::string etext = msg->text;
67 if (EscapeString(elogin))
69 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape login'\n");
70 if (RollbackTransaction())
72 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
77 if (EscapeString(etext))
79 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape message text'\n");
80 if (RollbackTransaction())
82 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
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 << "')";
99 result = PQexec(connection, query.str().c_str());
101 if (PQresultStatus(result) != PGRES_TUPLES_OK)
103 strError = PQresultErrorMessage(result);
105 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
106 if (RollbackTransaction())
108 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
113 int tuples = PQntuples(result);
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);
120 if (RollbackTransaction())
122 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
127 std::stringstream tuple;
128 tuple << PQgetvalue(result, 0, 0);
132 tuple >> msg->header.id;
134 if (CommitTransaction())
136 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to commit transaction'\n");
142 //-----------------------------------------------------------------------------
143 int POSTGRESQL_STORE::EditMessage(const STG_MSG & msg,
144 const std::string & login) const
146 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
148 if (PQstatus(connection) != CONNECTION_OK)
150 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
153 strError = "Connection lost";
154 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
161 if (StartTransaction())
163 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to start transaction'\n");
167 std::string elogin = login;
168 std::string etext = msg.text;
170 if (EscapeString(elogin))
172 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape login'\n");
173 if (RollbackTransaction())
175 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
180 if (EscapeString(etext))
182 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape message text'\n");
183 if (RollbackTransaction())
185 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
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;
203 result = PQexec(connection, query.str().c_str());
205 if (PQresultStatus(result) != PGRES_COMMAND_OK)
207 strError = PQresultErrorMessage(result);
209 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
210 if (RollbackTransaction())
212 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
219 if (CommitTransaction())
221 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to commit transaction'\n");
227 //-----------------------------------------------------------------------------
228 int POSTGRESQL_STORE::GetMessage(uint64_t id,
230 const std::string &) const
232 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
234 if (PQstatus(connection) != CONNECTION_OK)
236 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
239 strError = "Connection lost";
240 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): '%s'\n", strError.c_str());
248 if (StartTransaction())
250 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to start transaction'\n");
254 std::ostringstream query;
255 query << "SELECT ver, msg_type, last_send_time, \
256 creation_time, show_time, repeat, \
257 repeat_period, msg_text \
259 WHERE pk_message = " << id;
261 result = PQexec(connection, query.str().c_str());
263 if (PQresultStatus(result) != PGRES_TUPLES_OK)
265 strError = PQresultErrorMessage(result);
267 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): '%s'\n", strError.c_str());
268 if (RollbackTransaction())
270 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n");
275 int tuples = PQntuples(result);
279 strError = "Failed to fetch message data";
280 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
282 if (RollbackTransaction())
284 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n");
289 str2x(PQgetvalue(result, 0, 0), msg->header.ver);
290 str2x(PQgetvalue(result, 0, 1), msg->header.type);
291 msg->header.lastSendTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, 0, 2)));
292 msg->header.creationTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, 0, 3)));
293 str2x(PQgetvalue(result, 0, 4), msg->header.showTime);
294 str2x(PQgetvalue(result, 0, 5), msg->header.repeat);
295 str2x(PQgetvalue(result, 0, 6), msg->header.repeatPeriod);
296 msg->text = PQgetvalue(result, 0, 7);
300 if (CommitTransaction())
302 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to commit transaction'\n");
308 //-----------------------------------------------------------------------------
309 int POSTGRESQL_STORE::DelMessage(uint64_t id, const std::string &) const
311 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
313 if (PQstatus(connection) != CONNECTION_OK)
315 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
318 strError = "Connection lost";
319 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
326 if (StartTransaction())
328 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to start transaction'\n");
332 std::ostringstream query;
333 query << "DELETE FROM tb_messages WHERE pk_message = " << id;
335 result = PQexec(connection, query.str().c_str());
337 if (PQresultStatus(result) != PGRES_COMMAND_OK)
339 strError = PQresultErrorMessage(result);
341 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
342 if (RollbackTransaction())
344 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to rollback transaction'\n");
351 if (CommitTransaction())
353 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to commit transaction'\n");
359 //-----------------------------------------------------------------------------
360 int POSTGRESQL_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList,
361 const std::string & login) const
363 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
365 if (PQstatus(connection) != CONNECTION_OK)
367 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
370 strError = "Connection lost";
371 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
378 if (StartTransaction())
380 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to start transaction'\n");
384 std::string elogin = login;
386 if (EscapeString(elogin))
388 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to escape login'\n");
389 if (RollbackTransaction())
391 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
396 std::ostringstream query;
397 query << "SELECT pk_message, ver, msg_type, \
398 last_send_time, creation_time, show_time, \
399 repeat, repeat_period \
402 (SELECT pk_user FROM tb_users \
403 WHERE name = '" << elogin << "')";
405 result = PQexec(connection, query.str().c_str());
407 if (PQresultStatus(result) != PGRES_TUPLES_OK)
409 strError = PQresultErrorMessage(result);
411 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
412 if (RollbackTransaction())
414 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
419 int tuples = PQntuples(result);
421 for (int i = 0; i < tuples; ++i)
423 std::stringstream tuple;
425 tuple << PQgetvalue(result, i, 0) << " ";
426 tuple << PQgetvalue(result, i, 1) << " ";
427 tuple << PQgetvalue(result, i, 2) << " ";
428 header.lastSendTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, i, 3)));
429 header.creationTime = static_cast<unsigned int>(TS2Int(PQgetvalue(result, i, 4)));
430 tuple << PQgetvalue(result, i, 5) << " ";
431 tuple << PQgetvalue(result, i, 6) << " ";
432 tuple << PQgetvalue(result, i, 7) << " ";
436 tuple >> header.type;
437 tuple >> header.showTime;
438 tuple >> header.repeat;
439 tuple >> header.repeatPeriod;
440 hdrsList->push_back(header);
445 if (CommitTransaction())
447 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to commit transaction'\n");
453 //-----------------------------------------------------------------------------