diff --git a/src/gxs/rsdataservice.cc b/src/gxs/rsdataservice.cc index 5661627b3..20d2443bc 100644 --- a/src/gxs/rsdataservice.cc +++ b/src/gxs/rsdataservice.cc @@ -570,7 +570,7 @@ int RsDataService::storeMessage(std::map &msg) std::map::iterator mit = msg.begin(); // start a transaction - mDb->execSQL("BEGIN;"); + mDb->beginTransaction(); for(; mit != msg.end(); ++mit){ @@ -654,7 +654,7 @@ int RsDataService::storeMessage(std::map &msg) } // finish transaction - bool ret = mDb->execSQL("COMMIT;"); + bool ret = mDb->commitTransaction(); for(mit = msg.begin(); mit != msg.end(); ++mit) { @@ -685,7 +685,7 @@ int RsDataService::storeGroup(std::map &grp) std::map::iterator sit = grp.begin(); // begin transaction - mDb->execSQL("BEGIN;"); + mDb->beginTransaction(); for(; sit != grp.end(); ++sit) { @@ -769,7 +769,7 @@ int RsDataService::storeGroup(std::map &grp) } } // finish transaction - bool ret = mDb->execSQL("COMMIT;"); + bool ret = mDb->commitTransaction(); for(sit = grp.begin(); sit != grp.end(); ++sit) { @@ -791,7 +791,7 @@ int RsDataService::updateGroup(std::map &grp) std::map::iterator sit = grp.begin(); // begin transaction - mDb->execSQL("BEGIN;"); + mDb->beginTransaction(); for(; sit != grp.end(); ++sit) { @@ -858,7 +858,7 @@ int RsDataService::updateGroup(std::map &grp) mDb->sqlUpdate(GRP_TABLE_NAME, "grpId='" + grpPtr->grpId.toStdString() + "'", cv); } // finish transaction - bool ret = mDb->execSQL("COMMIT;"); + bool ret = mDb->commitTransaction(); for(sit = grp.begin(); sit != grp.end(); ++sit) { @@ -877,7 +877,7 @@ int RsDataService::updateGroupKeys(const RsGxsGroupId& grpId,const RsTlvSecurity RsStackMutex stack(mDbMutex); // begin transaction - mDb->execSQL("BEGIN;"); + mDb->beginTransaction(); /*! * STORE key set @@ -895,7 +895,7 @@ int RsDataService::updateGroupKeys(const RsGxsGroupId& grpId,const RsTlvSecurity mDb->sqlUpdate(GRP_TABLE_NAME, "grpId='" + grpId.toStdString() + "'", cv); // finish transaction - return mDb->execSQL("COMMIT;"); + return mDb->commitTransaction(); } bool RsDataService::validSize(RsNxsGrp* grp) const @@ -1608,7 +1608,7 @@ int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std bool RsDataService::locked_updateMessageEntries(const MsgUpdates& updates) { // start a transaction - bool ret = mDb->execSQL("BEGIN;"); + bool ret = mDb->beginTransaction(); MsgUpdates::const_iterator mit = updates.begin(); @@ -1627,7 +1627,7 @@ bool RsDataService::locked_updateMessageEntries(const MsgUpdates& updates) } } - ret &= mDb->execSQL("COMMIT;"); + ret &= mDb->commitTransaction(); return ret; } @@ -1635,7 +1635,7 @@ bool RsDataService::locked_updateMessageEntries(const MsgUpdates& updates) bool RsDataService::locked_removeMessageEntries(const GxsMsgReq& msgIds) { // start a transaction - bool ret = mDb->execSQL("BEGIN;"); + bool ret = mDb->beginTransaction(); GxsMsgReq::const_iterator mit = msgIds.begin(); @@ -1653,7 +1653,7 @@ bool RsDataService::locked_removeMessageEntries(const GxsMsgReq& msgIds) } } - ret &= mDb->execSQL("COMMIT;"); + ret &= mDb->commitTransaction(); return ret; } @@ -1661,7 +1661,7 @@ bool RsDataService::locked_removeMessageEntries(const GxsMsgReq& msgIds) bool RsDataService::locked_removeGroupEntries(const std::vector& grpIds) { // start a transaction - bool ret = mDb->execSQL("BEGIN;"); + bool ret = mDb->beginTransaction(); std::vector::const_iterator vit = grpIds.begin(); @@ -1672,7 +1672,7 @@ bool RsDataService::locked_removeGroupEntries(const std::vector& g mDb->sqlDelete(GRP_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", ""); } - ret &= mDb->execSQL("COMMIT;"); + ret &= mDb->commitTransaction(); return ret; } diff --git a/src/util/retrodb.cc b/src/util/retrodb.cc index 368c5240e..b6633c3a5 100644 --- a/src/util/retrodb.cc +++ b/src/util/retrodb.cc @@ -264,6 +264,32 @@ std::string RetroDb::getKey() const return mKey; } +bool RetroDb::beginTransaction() +{ + if (!isOpen()) { + return false; + } + + return execSQL("BEGIN;"); +} + +bool RetroDb::commitTransaction() +{ + if (!isOpen()) { + return false; + } + + return execSQL("COMMIT;"); +} +bool RetroDb::rollbackTransaction() +{ + if (!isOpen()) { + return false; + } + + return execSQL("ROLLBACK;"); +} + bool RetroDb::execSQL_bind(const std::string &query, std::list ¶mBindings){ // prepare statement @@ -531,6 +557,43 @@ bool RetroDb::sqlUpdate(const std::string &tableName, std::string whereClause, c return execSQL_bind(sqlQuery, paramBindings); } +bool RetroDb::tableExists(const std::string &tableName) +{ + if (!isOpen()) { + return false; + } + + std::string sqlQuery = "PRAGMA table_info(" + tableName + ");"; + + bool result = false; + sqlite3_stmt* stmt = NULL; + + int rc = sqlite3_prepare_v2(mDb, sqlQuery.c_str(), sqlQuery.length(), &stmt, NULL); + if (rc == SQLITE_OK) { + rc = sqlite3_step(stmt); + switch (rc) { + case SQLITE_ROW: + result = true; + break; + case SQLITE_DONE: + break; + default: + std::cerr << "RetroDb::tableExists(): Error executing statement (code: " << rc << ")" + << std::endl; + return false; + } + } else { + std::cerr << "RetroDb::tableExists(): Error preparing statement\n"; + std::cerr << "Error code: " << sqlite3_errmsg(mDb) + << std::endl; + } + + if (stmt) { + sqlite3_finalize(stmt); + } + + return result; +} /********************** RetroCursor ************************/ diff --git a/src/util/retrodb.h b/src/util/retrodb.h index c0e2f8db6..8b79c0017 100644 --- a/src/util/retrodb.h +++ b/src/util/retrodb.h @@ -38,8 +38,6 @@ #include "contentvalue.h" - - class RetroCursor; /*! @@ -90,7 +88,23 @@ public: /* modifying db */ public: + /*! + * Start transaction + * @return true/false + */ + bool beginTransaction(); + /*! + * Commit transaction + * @return true/false + */ + bool commitTransaction(); + + /*! + * Rollback transaction + * @return true/false + */ + bool rollbackTransaction(); /*! * To a make query which do not return a result \n @@ -153,22 +167,13 @@ public: */ void vacuum(); - /*! - * Build the "VALUE" part of an insertiong sql query - * @param parameter contains place holder query - * @param paramBindings + * Check if table exist in database + * @param tableName table to check + * @return true/false */ - void buildInsertQueryValue(const std::map keyMap, const ContentValue& cv, - std::string& parameter, std::list& paramBindings); + bool tableExists(const std::string& tableName); - /*! - * Build the "VALUE" part of an insertiong sql query - * @param parameter contains place holder query - * @param paramBindings - */ - void buildUpdateQueryValue(const std::map keyMap, const ContentValue& cv, - std::string& parameter, std::list& paramBindings); public: static const int OPEN_READONLY; @@ -179,6 +184,22 @@ private: bool execSQL_bind(const std::string &query, std::list& blobs); + /*! + * Build the "VALUE" part of an insertiong sql query + * @param parameter contains place holder query + * @param paramBindings + */ + void buildInsertQueryValue(const std::map keyMap, const ContentValue& cv, + std::string& parameter, std::list& paramBindings); + + /*! + * Build the "VALUE" part of an insertiong sql query + * @param parameter contains place holder query + * @param paramBindings + */ + void buildUpdateQueryValue(const std::map keyMap, const ContentValue& cv, + std::string& parameter, std::list& paramBindings); + private: sqlite3* mDb;