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 $
30 #include "postgresql_store.h"
32 #include "stg/common.h"
33 #include "stg/locker.h"
34 #include "stg/message.h"
42 //-----------------------------------------------------------------------------
43 int POSTGRESQL_STORE::AddMessage(STG::Message * msg, const std::string & login) const
45 STG_LOCKER lock(&mutex);
47 if (PQstatus(connection) != CONNECTION_OK)
49 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
52 strError = "Connection lost";
53 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
60 if (StartTransaction())
62 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to start transaction'\n");
66 std::string elogin = login;
67 std::string etext = msg->text;
69 if (EscapeString(elogin))
71 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape login'\n");
72 if (RollbackTransaction())
74 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
79 if (EscapeString(etext))
81 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape message text'\n");
82 if (RollbackTransaction())
84 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
89 std::ostringstream query;
90 query << "SELECT sp_add_message("
91 << "'" << elogin << "', "
92 << "CAST(1 AS SMALLINT), " // Here need to be a version, but, it's uninitiated actually
93 << "CAST(" << msg->header.type << " AS SMALLINT), "
94 << "CAST('" << formatTime(msg->header.lastSendTime) << "' AS TIMESTAMP), "
95 << "CAST('" << formatTime(msg->header.creationTime) << "' AS TIMESTAMP), "
96 << msg->header.showTime << ", "
97 << "CAST(" << msg->header.repeat << " AS SMALLINT), "
98 << msg->header.repeatPeriod << ", "
99 << "'" << etext << "')";
101 result = PQexec(connection, query.str().c_str());
103 if (PQresultStatus(result) != PGRES_TUPLES_OK)
105 strError = PQresultErrorMessage(result);
107 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
108 if (RollbackTransaction())
110 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
115 int tuples = PQntuples(result);
119 strError = "Failed to fetch newlly added message ID";
120 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
122 if (RollbackTransaction())
124 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
129 std::stringstream tuple;
130 tuple << PQgetvalue(result, 0, 0);
134 tuple >> msg->header.id;
136 if (CommitTransaction())
138 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to commit transaction'\n");
144 //-----------------------------------------------------------------------------
145 int POSTGRESQL_STORE::EditMessage(const STG::Message & msg,
146 const std::string & login) const
148 STG_LOCKER lock(&mutex);
150 if (PQstatus(connection) != CONNECTION_OK)
152 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
155 strError = "Connection lost";
156 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
163 if (StartTransaction())
165 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to start transaction'\n");
169 std::string elogin = login;
170 std::string etext = msg.text;
172 if (EscapeString(elogin))
174 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape login'\n");
175 if (RollbackTransaction())
177 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
182 if (EscapeString(etext))
184 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape message text'\n");
185 if (RollbackTransaction())
187 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
192 std::ostringstream query;
193 query << "UPDATE tb_messages SET "
194 << "fk_user = (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'), "
195 << "ver = " << msg.header.ver << ", "
196 << "msg_type = " << msg.header.type << ", "
197 << "last_send_time = CAST('" << formatTime(msg.header.lastSendTime) << "' AS TIMESTAMP), "
198 << "creation_time = CAST('" << formatTime(msg.header.creationTime) << "' AS TIMESTAMP), "
199 << "show_time = " << msg.header.showTime << ", "
200 << "repeat = " << msg.header.repeat << ", "
201 << "repeat_period = " << msg.header.repeatPeriod << ", "
202 << "msg_text = '" << etext << "' "
203 << "WHERE pk_message = " << msg.header.id;
205 result = PQexec(connection, query.str().c_str());
207 if (PQresultStatus(result) != PGRES_COMMAND_OK)
209 strError = PQresultErrorMessage(result);
211 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
212 if (RollbackTransaction())
214 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
221 if (CommitTransaction())
223 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to commit transaction'\n");
229 //-----------------------------------------------------------------------------
230 int POSTGRESQL_STORE::GetMessage(uint64_t id,
232 const std::string &) const
234 STG_LOCKER lock(&mutex);
236 if (PQstatus(connection) != CONNECTION_OK)
238 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
241 strError = "Connection lost";
242 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): '%s'\n", strError.c_str());
249 if (StartTransaction())
251 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to start transaction'\n");
255 std::ostringstream query;
256 query << "SELECT ver, msg_type, last_send_time, \
257 creation_time, show_time, repeat, \
258 repeat_period, msg_text \
260 WHERE pk_message = " << id;
262 result = PQexec(connection, query.str().c_str());
264 if (PQresultStatus(result) != PGRES_TUPLES_OK)
266 strError = PQresultErrorMessage(result);
268 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): '%s'\n", strError.c_str());
269 if (RollbackTransaction())
271 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n");
276 int tuples = PQntuples(result);
280 strError = "Failed to fetch message data";
281 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
283 if (RollbackTransaction())
285 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n");
290 str2x(PQgetvalue(result, 0, 0), msg->header.ver);
291 str2x(PQgetvalue(result, 0, 1), msg->header.type);
292 msg->header.lastSendTime = static_cast<unsigned int>(readTime(PQgetvalue(result, 0, 2)));
293 msg->header.creationTime = static_cast<unsigned int>(readTime(PQgetvalue(result, 0, 3)));
294 str2x(PQgetvalue(result, 0, 4), msg->header.showTime);
295 str2x(PQgetvalue(result, 0, 5), msg->header.repeat);
296 str2x(PQgetvalue(result, 0, 6), msg->header.repeatPeriod);
297 msg->text = PQgetvalue(result, 0, 7);
301 if (CommitTransaction())
303 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to commit transaction'\n");
309 //-----------------------------------------------------------------------------
310 int POSTGRESQL_STORE::DelMessage(uint64_t id, const std::string &) const
312 STG_LOCKER lock(&mutex);
314 if (PQstatus(connection) != CONNECTION_OK)
316 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
319 strError = "Connection lost";
320 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
327 if (StartTransaction())
329 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to start transaction'\n");
333 std::ostringstream query;
334 query << "DELETE FROM tb_messages WHERE pk_message = " << id;
336 result = PQexec(connection, query.str().c_str());
338 if (PQresultStatus(result) != PGRES_COMMAND_OK)
340 strError = PQresultErrorMessage(result);
342 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
343 if (RollbackTransaction())
345 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to rollback transaction'\n");
352 if (CommitTransaction())
354 printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to commit transaction'\n");
360 //-----------------------------------------------------------------------------
361 int POSTGRESQL_STORE::GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList,
362 const std::string & login) const
364 STG_LOCKER lock(&mutex);
366 if (PQstatus(connection) != CONNECTION_OK)
368 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
371 strError = "Connection lost";
372 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
379 if (StartTransaction())
381 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to start transaction'\n");
385 std::string elogin = login;
387 if (EscapeString(elogin))
389 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to escape login'\n");
390 if (RollbackTransaction())
392 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
397 std::ostringstream query;
398 query << "SELECT pk_message, ver, msg_type, \
399 last_send_time, creation_time, show_time, \
400 repeat, repeat_period \
403 (SELECT pk_user FROM tb_users \
404 WHERE name = '" << elogin << "')";
406 result = PQexec(connection, query.str().c_str());
408 if (PQresultStatus(result) != PGRES_TUPLES_OK)
410 strError = PQresultErrorMessage(result);
412 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
413 if (RollbackTransaction())
415 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
420 int tuples = PQntuples(result);
422 for (int i = 0; i < tuples; ++i)
424 std::stringstream tuple;
425 STG::Message::Header header;
426 tuple << PQgetvalue(result, i, 0) << " ";
427 tuple << PQgetvalue(result, i, 1) << " ";
428 tuple << PQgetvalue(result, i, 2) << " ";
429 header.lastSendTime = static_cast<unsigned int>(readTime(PQgetvalue(result, i, 3)));
430 header.creationTime = static_cast<unsigned int>(readTime(PQgetvalue(result, i, 4)));
431 tuple << PQgetvalue(result, i, 5) << " ";
432 tuple << PQgetvalue(result, i, 6) << " ";
433 tuple << PQgetvalue(result, i, 7) << " ";
437 tuple >> header.type;
438 tuple >> header.showTime;
439 tuple >> header.repeat;
440 tuple >> header.repeatPeriod;
441 hdrsList->push_back(header);
446 if (CommitTransaction())
448 printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to commit transaction'\n");
454 //-----------------------------------------------------------------------------