X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/4271ab433cd55bbd2612292bcf39e4dc3d7274f1..0907aa4037b12b6b88ee24495d4577a064d4f8db:/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp diff --git a/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp b/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp new file mode 100644 index 00000000..90c3e573 --- /dev/null +++ b/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp @@ -0,0 +1,231 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + + +/* + * Messages manipualtion methods + * + * $Revision: 1.10 $ + * $Date: 2009/03/03 16:16:23 $ + * + */ + +#include "firebird_store.h" + +#include "stg/ibpp.h" +#include "stg/message.h" +#include "stg/common.h" + +//----------------------------------------------------------------------------- +int FIREBIRD_STORE::AddMessage(STG::Message * msg, const std::string & login) const +{ +STG_LOCKER lock(&mutex); + +IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); +IBPP::Statement st = IBPP::StatementFactory(db, tr); + +try + { + tr->Start(); + st->Prepare("execute procedure sp_add_message(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + st->Set(1, login); + st->Set(2, (int32_t)msg->header.ver); + st->Set(3, (int32_t)msg->header.type); + st->Set(4, (int32_t)msg->header.lastSendTime); + st->Set(5, (int32_t)msg->header.creationTime); + st->Set(6, (int32_t)msg->header.showTime); + st->Set(7, msg->header.repeat); + st->Set(8, (int32_t)msg->header.repeatPeriod); + st->Set(9, msg->text); + st->Execute(); + st->Get(1, (int64_t &)msg->header.id); + tr->Commit(); + } + +catch (IBPP::Exception & ex) + { + tr->Rollback(); + strError = "IBPP exception"; + printfd(__FILE__, ex.what()); + return -1; + } + +return 0; +} +//----------------------------------------------------------------------------- +int FIREBIRD_STORE::EditMessage(const STG::Message & msg, + const std::string & login) const +{ +STG_LOCKER lock(&mutex); + +IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); +IBPP::Statement st = IBPP::StatementFactory(db, tr); + +try + { + tr->Start(); + st->Prepare("execute procedure sp_add_message(?, ?, ?, ?, ?, ?, ?, ?, ?)"); + st->Set(1, (int64_t)msg.header.id); + st->Set(2, login); + st->Set(3, (int32_t)msg.header.ver); + st->Set(4, (int32_t)msg.header.type); + st->Set(5, (int32_t)msg.header.lastSendTime); + st->Set(6, (int32_t)msg.header.creationTime); + st->Set(7, (int32_t)msg.header.showTime); + st->Set(8, msg.header.repeat); + st->Set(9, (int32_t)msg.header.repeatPeriod); + st->Set(10, msg.text.c_str()); + st->Execute(); + tr->Commit(); + } + +catch (IBPP::Exception & ex) + { + tr->Rollback(); + strError = "IBPP exception"; + printfd(__FILE__, ex.what()); + return -1; + } + +return 0; +} +//----------------------------------------------------------------------------- +int FIREBIRD_STORE::GetMessage(uint64_t id, + STG::Message * msg, + const std::string &) const +{ +STG_LOCKER lock(&mutex); + +IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); +IBPP::Statement st = IBPP::StatementFactory(db, tr); + +try + { + tr->Start(); + st->Prepare("select * from tb_messages where pk_message = ?"); + st->Set(1, (int64_t)id); + st->Execute(); + if (st->Fetch()) + { + st->Get(1, (int64_t &)msg->header.id); + st->Get(3, (int32_t &)msg->header.ver); + st->Get(4, (int32_t &)msg->header.type); + st->Get(5, (int32_t &)msg->header.lastSendTime); + st->Get(6, (int32_t &)msg->header.creationTime); + st->Get(7, (int32_t &)msg->header.showTime); + st->Get(8, msg->header.repeat); + st->Get(9, (int32_t &)msg->header.repeatPeriod); + st->Get(10, msg->text); + } + else + { + strprintf(&strError, "Message with id = %d not found in database", id); + printfd(__FILE__, "Message with id - %d not found in database\n", id); + tr->Rollback(); + return -1; + } + tr->Commit(); + } + +catch (IBPP::Exception & ex) + { + tr->Rollback(); + strError = "IBPP exception"; + printfd(__FILE__, ex.what()); + return -1; + } + +return 0; +} +//----------------------------------------------------------------------------- +int FIREBIRD_STORE::DelMessage(uint64_t id, const std::string &) const +{ +STG_LOCKER lock(&mutex); + +IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); +IBPP::Statement st = IBPP::StatementFactory(db, tr); + +try + { + tr->Start(); + st->Prepare("delete from tb_messages where pk_message = ?"); + st->Set(1, (int64_t)id); + st->Execute(); + tr->Commit(); + } + +catch (IBPP::Exception & ex) + { + tr->Rollback(); + strError = "IBPP exception"; + printfd(__FILE__, ex.what()); + return -1; + } + +return 0; +} +//----------------------------------------------------------------------------- +int FIREBIRD_STORE::GetMessageHdrs(std::vector * hdrsList, + const std::string & login) const +{ +STG_LOCKER lock(&mutex); + +IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); +IBPP::Statement st = IBPP::StatementFactory(db, tr); + +STG::Message::Header header; + +try + { + tr->Start(); + st->Prepare("select pk_message, ver, msg_type, \ + last_send_time, creation_time, \ + show_time, repeat, repeat_period \ + from tb_messages where \ + fk_user = (select pk_user from tb_users where name = ?)"); + st->Set(1, login); + st->Execute(); + while (st->Fetch()) + { + st->Get(1, (int64_t &)header.id); + st->Get(2, (int32_t &)header.ver); + st->Get(3, (int32_t &)header.type); + st->Get(4, (int32_t &)header.lastSendTime); + st->Get(5, (int32_t &)header.creationTime); + st->Get(6, (int32_t &)header.showTime); + st->Get(7, header.repeat); + st->Get(8, (int32_t &)header.repeatPeriod); + hdrsList->push_back(header); + } + tr->Commit(); + } + +catch (IBPP::Exception & ex) + { + tr->Rollback(); + strError = "IBPP exception"; + printfd(__FILE__, ex.what()); + return -1; + } + +return 0; +} +//----------------------------------------------------------------------------- +