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 : Maksym Mamontov <stg@madf.info>
23 * Messages manipualtion methods
26 * $Date: 2009/07/15 11:19:42 $
36 #include "stg/common.h"
37 #include "postgresql_store.h"
38 #include "stg/locker.h"
39 #include "stg/message.h"
41 //-----------------------------------------------------------------------------
42 int POSTGRESQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
44 STG_LOCKER lock(&mutex);
46 if (PQstatus(connection) != CONNECTION_OK)
48 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
51 strError = "Connection lost";
52 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
59 if (StartTransaction())
61 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to start transaction'\n");
65 std::string elogin = login;
66 std::string etext = msg->text;
68 if (EscapeString(elogin))
70 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape login'\n");
71 if (RollbackTransaction())
73 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
78 if (EscapeString(etext))
80 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape message text'\n");
81 if (RollbackTransaction())
83 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
88 std::ostringstream query;
89 query << "SELECT sp_add_message("
90 << "'" << elogin << "', "
91 << "CAST(1 AS SMALLINT), " // Here need to be a version, but, it's uninitiated actually
92 << "CAST(" << msg->header.type << " AS SMALLINT), "
93 << "CAST('" << formatTime(msg->header.lastSendTime) << "' AS TIMESTAMP), "
94 << "CAST('" << formatTime(msg->header.creationTime) << "' AS TIMESTAMP), "
95 << msg->header.showTime << ", "
96 << "CAST(" << msg->header.repeat << " AS SMALLINT), "
97 << msg->header.repeatPeriod << ", "
98 << "'" << etext << "')";
100 result = PQexec(connection, query.str().c_str());
102 if (PQresultStatus(result) != PGRES_TUPLES_OK)
104 strError = PQresultErrorMessage(result);
106 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): '%s'\n", strError.c_str());
107 if (RollbackTransaction())
109 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
114 int tuples = PQntuples(result);
118 strError = "Failed to fetch newlly added message ID";
119 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
121 if (RollbackTransaction())
123 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n");
128 std::stringstream tuple;
129 tuple << PQgetvalue(result, 0, 0);
133 tuple >> msg->header.id;
135 if (CommitTransaction())
137 printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to commit transaction'\n");
143 //-----------------------------------------------------------------------------
144 int POSTGRESQL_STORE::EditMessage(const STG_MSG & msg,
145 const std::string & login) const
147 STG_LOCKER lock(&mutex);
149 if (PQstatus(connection) != CONNECTION_OK)
151 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
154 strError = "Connection lost";
155 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
162 if (StartTransaction())
164 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to start transaction'\n");
168 std::string elogin = login;
169 std::string etext = msg.text;
171 if (EscapeString(elogin))
173 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape login'\n");
174 if (RollbackTransaction())
176 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
181 if (EscapeString(etext))
183 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape message text'\n");
184 if (RollbackTransaction())
186 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
191 std::ostringstream query;
192 query << "UPDATE tb_messages SET "
193 << "fk_user = (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'), "
194 << "ver = " << msg.header.ver << ", "
195 << "msg_type = " << msg.header.type << ", "
196 << "last_send_time = CAST('" << formatTime(msg.header.lastSendTime) << "' AS TIMESTAMP), "
197 << "creation_time = CAST('" << formatTime(msg.header.creationTime) << "' AS TIMESTAMP), "
198 << "show_time = " << msg.header.showTime << ", "
199 << "repeat = " << msg.header.repeat << ", "
200 << "repeat_period = " << msg.header.repeatPeriod << ", "
201 << "msg_text = '" << etext << "' "
202 << "WHERE pk_message = " << msg.header.id;
204 result = PQexec(connection, query.str().c_str());
206 if (PQresultStatus(result) != PGRES_COMMAND_OK)
208 strError = PQresultErrorMessage(result);
210 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): '%s'\n", strError.c_str());
211 if (RollbackTransaction())
213 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n");
220 if (CommitTransaction())
222 printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to commit transaction'\n");
228 //-----------------------------------------------------------------------------
229 int POSTGRESQL_STORE::GetMessage(uint64_t id,
231 const std::string &) const
233 STG_LOCKER lock(&mutex);
235 if (PQstatus(connection) != CONNECTION_OK)
237 printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
240 strError = "Connection lost";
241 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>(readTime(PQgetvalue(result, 0, 2)));
292 msg->header.creationTime = static_cast<unsigned int>(readTime(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);
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);
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>(readTime(PQgetvalue(result, i, 3)));
429 header.creationTime = static_cast<unsigned int>(readTime(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 //-----------------------------------------------------------------------------