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 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::stringstream 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 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::stringstream 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 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::stringstream 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 /*std::stringstream tuple;
 
 291 for (int i = 0; i < 8; ++i)
 
 293     tuple << PQgetvalue(result, 0, i) << " ";
 
 296 str2x(PQgetvalue(result, 0, 0), msg->header.ver);
 
 297 str2x(PQgetvalue(result, 0, 1), msg->header.type);
 
 298 msg->header.lastSendTime = TS2Int(PQgetvalue(result, 0, 2));
 
 299 msg->header.creationTime = TS2Int(PQgetvalue(result, 0, 3));
 
 300 str2x(PQgetvalue(result, 0, 4), msg->header.showTime);
 
 301 str2x(PQgetvalue(result, 0, 5), msg->header.repeat);
 
 302 str2x(PQgetvalue(result, 0, 6), msg->header.repeatPeriod);
 
 303 msg->text = PQgetvalue(result, 0, 7);
 
 307 /*tuple >> msg->header.ver;
 
 308 tuple >> msg->header.type;
 
 309 tuple >> msg->header.lastSendTime;
 
 310 tuple >> msg->header.creationTime;
 
 311 tuple >> msg->header.showTime;
 
 312 tuple >> msg->header.repeat;
 
 313 tuple >> msg->header.repeatPeriod;
 
 314 tuple >> msg->text;*/
 
 316 if (CommitTransaction())
 
 318     printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to commit transaction'\n");
 
 324 //-----------------------------------------------------------------------------
 
 325 int POSTGRESQL_STORE::DelMessage(uint64_t id, const string &) const
 
 327 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 329 if (PQstatus(connection) != CONNECTION_OK)
 
 331     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
 
 334         strError = "Connection lost";
 
 335         printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
 
 342 if (StartTransaction())
 
 344     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to start transaction'\n");
 
 348 std::stringstream query;
 
 349 query << "DELETE FROM tb_messages WHERE pk_message = " << id;
 
 351 result = PQexec(connection, query.str().c_str());
 
 353 if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
 355     strError = PQresultErrorMessage(result);
 
 357     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): '%s'\n", strError.c_str());
 
 358     if (RollbackTransaction())
 
 360         printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to rollback transaction'\n");
 
 367 if (CommitTransaction())
 
 369     printfd(__FILE__, "POSTGRESQL_STORE::DelMessage(): 'Failed to commit transaction'\n");
 
 375 //-----------------------------------------------------------------------------
 
 376 int POSTGRESQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList,
 
 377                                    const string & login) const
 
 379 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 381 if (PQstatus(connection) != CONNECTION_OK)
 
 383     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
 
 386         strError = "Connection lost";
 
 387         printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
 
 394 if (StartTransaction())
 
 396     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to start transaction'\n");
 
 400 std::string elogin = login;
 
 402 if (EscapeString(elogin))
 
 404     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to escape login'\n");
 
 405     if (RollbackTransaction())
 
 407         printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
 
 412 std::stringstream query;
 
 413 query << "SELECT pk_message, ver, msg_type, \
 
 414                  last_send_time, creation_time, show_time, \
 
 415                  repeat, repeat_period \
 
 418                 (SELECT pk_user FROM tb_users \
 
 419           WHERE name = '" << elogin << "')";
 
 421 result = PQexec(connection, query.str().c_str());
 
 423 if (PQresultStatus(result) != PGRES_TUPLES_OK)
 
 425     strError = PQresultErrorMessage(result);
 
 427     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): '%s'\n", strError.c_str());
 
 428     if (RollbackTransaction())
 
 430         printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n");
 
 435 int tuples = PQntuples(result);
 
 437 for (int i = 0; i < tuples; ++i)
 
 439     std::stringstream tuple;
 
 441     tuple << PQgetvalue(result, i, 0) << " ";
 
 442     tuple << PQgetvalue(result, i, 1) << " ";
 
 443     tuple << PQgetvalue(result, i, 2) << " ";
 
 444     header.lastSendTime = TS2Int(PQgetvalue(result, i, 3));
 
 445     header.creationTime = TS2Int(PQgetvalue(result, i, 4));
 
 446     tuple << PQgetvalue(result, i, 5) << " ";
 
 447     tuple << PQgetvalue(result, i, 6) << " ";
 
 448     tuple << PQgetvalue(result, i, 7) << " ";
 
 452     tuple >> header.type;
 
 453     tuple >> header.showTime;
 
 454     tuple >> header.repeat;
 
 455     tuple >> header.repeatPeriod;
 
 456     hdrsList->push_back(header);
 
 461 if (CommitTransaction())
 
 463     printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to commit transaction'\n");
 
 469 //-----------------------------------------------------------------------------