X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/8569ecdc2c9368dc0fe650b901cce7b37337ffec..a42349b9f8b4cbdcc6ad763ccf509f84fbaaf39d:/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp index bccef5aa..905b467c 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp @@ -27,20 +27,22 @@ * */ +#include "postgresql_store.h" + +#include "stg/common.h" +#include "stg/locker.h" +#include "stg/message.h" + #include #include #include #include -#include "postgresql_store.h" -#include "stg/locker.h" -#include "stg/message.h" - //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::AddMessage(STG_MSG * msg, const std::string & login) const +int POSTGRESQL_STORE::AddMessage(STG::Message * msg, const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -68,9 +70,9 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -78,9 +80,9 @@ if (EscapeString(etext)) { printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to escape message text'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -89,8 +91,8 @@ query << "SELECT sp_add_message(" << "'" << elogin << "', " << "CAST(1 AS SMALLINT), " // Here need to be a version, but, it's uninitiated actually << "CAST(" << msg->header.type << " AS SMALLINT), " - << "CAST('" << Int2TS(msg->header.lastSendTime) << "' AS TIMESTAMP), " - << "CAST('" << Int2TS(msg->header.creationTime) << "' AS TIMESTAMP), " + << "CAST('" << formatTime(msg->header.lastSendTime) << "' AS TIMESTAMP), " + << "CAST('" << formatTime(msg->header.creationTime) << "' AS TIMESTAMP), " << msg->header.showTime << ", " << "CAST(" << msg->header.repeat << " AS SMALLINT), " << msg->header.repeatPeriod << ", " @@ -118,9 +120,9 @@ if (tuples != 1) printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::AddMessage(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -140,10 +142,10 @@ if (CommitTransaction()) return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::EditMessage(const STG_MSG & msg, +int POSTGRESQL_STORE::EditMessage(const STG::Message & msg, const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -171,9 +173,9 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -181,9 +183,9 @@ if (EscapeString(etext)) { printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to escape message text'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::EditMessage(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -192,8 +194,8 @@ query << "UPDATE tb_messages SET " << "fk_user = (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'), " << "ver = " << msg.header.ver << ", " << "msg_type = " << msg.header.type << ", " - << "last_send_time = CAST('" << Int2TS(msg.header.lastSendTime) << "' AS TIMESTAMP), " - << "creation_time = CAST('" << Int2TS(msg.header.creationTime) << "' AS TIMESTAMP), " + << "last_send_time = CAST('" << formatTime(msg.header.lastSendTime) << "' AS TIMESTAMP), " + << "creation_time = CAST('" << formatTime(msg.header.creationTime) << "' AS TIMESTAMP), " << "show_time = " << msg.header.showTime << ", " << "repeat = " << msg.header.repeat << ", " << "repeat_period = " << msg.header.repeatPeriod << ", " @@ -226,10 +228,10 @@ return 0; } //----------------------------------------------------------------------------- int POSTGRESQL_STORE::GetMessage(uint64_t id, - STG_MSG * msg, - const std::string &) const + STG::Message * msg, + const std::string &) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -242,7 +244,6 @@ if (PQstatus(connection) != CONNECTION_OK) } } -std::string login; PGresult * result; if (StartTransaction()) @@ -280,16 +281,16 @@ if (tuples != 1) printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::GetMessage(): 'Failed to rollback transaction'\n"); + } return -1; } str2x(PQgetvalue(result, 0, 0), msg->header.ver); str2x(PQgetvalue(result, 0, 1), msg->header.type); -msg->header.lastSendTime = static_cast(TS2Int(PQgetvalue(result, 0, 2))); -msg->header.creationTime = static_cast(TS2Int(PQgetvalue(result, 0, 3))); +msg->header.lastSendTime = static_cast(readTime(PQgetvalue(result, 0, 2))); +msg->header.creationTime = static_cast(readTime(PQgetvalue(result, 0, 3))); str2x(PQgetvalue(result, 0, 4), msg->header.showTime); str2x(PQgetvalue(result, 0, 5), msg->header.repeat); str2x(PQgetvalue(result, 0, 6), msg->header.repeatPeriod); @@ -308,7 +309,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::DelMessage(uint64_t id, const std::string &) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -357,10 +358,10 @@ if (CommitTransaction()) return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::GetMessageHdrs(std::vector * hdrsList, +int POSTGRESQL_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -387,9 +388,9 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::GetMessageHdrs(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -421,12 +422,12 @@ int tuples = PQntuples(result); for (int i = 0; i < tuples; ++i) { std::stringstream tuple; - STG_MSG_HDR header; + STG::Message::Header header; tuple << PQgetvalue(result, i, 0) << " "; tuple << PQgetvalue(result, i, 1) << " "; tuple << PQgetvalue(result, i, 2) << " "; - header.lastSendTime = static_cast(TS2Int(PQgetvalue(result, i, 3))); - header.creationTime = static_cast(TS2Int(PQgetvalue(result, i, 4))); + header.lastSendTime = static_cast(readTime(PQgetvalue(result, i, 3))); + header.creationTime = static_cast(readTime(PQgetvalue(result, i, 4))); tuple << PQgetvalue(result, i, 5) << " "; tuple << PQgetvalue(result, i, 6) << " "; tuple << PQgetvalue(result, i, 7) << " ";