From 616d70b470bb6c12c37eda8841cbba02535422d5 Mon Sep 17 00:00:00 2001 From: Rafal Krypa Date: Tue, 9 Dec 2014 13:29:08 +0100 Subject: [PATCH] PrivilegeDb: introduce convenient private method for fetching prepared query Create an internal method for repeating code pattern fetching the prepared query from internal data structure and resetting it before use. Change-Id: I346e5a0790d869632181737b52d6d5ba78da79c3 Signed-off-by: Rafal Krypa --- src/common/include/privilege_db.h | 9 +++++++ src/common/privilege_db.cpp | 52 ++++++++++++--------------------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/common/include/privilege_db.h b/src/common/include/privilege_db.h index 1df7723..5135d93 100644 --- a/src/common/include/privilege_db.h +++ b/src/common/include/privilege_db.h @@ -96,6 +96,15 @@ private: void initDataCommands(); /** + * Return prepared query for given query type. + * The query will be reset before returning. + * + * @param queryType query identifier + * @return reference to prepared, reset query + */ + DB::SqlConnection::DataCommandAutoPtr & getQuery(QueryType queryType); + + /** * Check if pkgId is already registered in database * * @param pkgId - package identifier diff --git a/src/common/privilege_db.cpp b/src/common/privilege_db.cpp index cca5aaa..4d87cdd 100644 --- a/src/common/privilege_db.cpp +++ b/src/common/privilege_db.cpp @@ -76,6 +76,13 @@ void PrivilegeDb::initDataCommands() } } +DB::SqlConnection::DataCommandAutoPtr & PrivilegeDb::getQuery(QueryType queryType) +{ + auto &command = m_commands.at(static_cast(queryType)); + command->Reset(); + return command; +} + PrivilegeDb::~PrivilegeDb() { delete mSqlConnection; @@ -111,10 +118,7 @@ void PrivilegeDb::RollbackTransaction(void) bool PrivilegeDb::PkgIdExists(const std::string &pkgId) { return try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::EPkgIdExists)); - - command->Reset(); + auto &command = getQuery(QueryType::EPkgIdExists); command->BindString(1, pkgId.c_str()); return command->Step(); }); @@ -123,10 +127,7 @@ bool PrivilegeDb::PkgIdExists(const std::string &pkgId) bool PrivilegeDb::GetAppPkgId(const std::string &appId, std::string &pkgId) { return try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::EGetPkgId)); - - command->Reset(); + auto &command = getQuery(QueryType::EGetPkgId); command->BindString(1, appId.c_str()); if (!command->Step()) { @@ -147,10 +148,7 @@ void PrivilegeDb::AddApplication(const std::string &appId, pkgIdIsNew = !(this->PkgIdExists(pkgId)); try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::EAddApplication)); - - command->Reset(); + auto &command = getQuery(QueryType::EAddApplication); command->BindString(1, appId.c_str()); command->BindString(2, pkgId.c_str()); command->BindInteger(3, static_cast(uid)); @@ -174,10 +172,7 @@ void PrivilegeDb::RemoveApplication(const std::string &appId, uid_t uid, return; } - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::ERemoveApplication)); - - command->Reset(); + auto &command = getQuery(QueryType::ERemoveApplication); command->BindString(1, appId.c_str()); command->BindInteger(2, static_cast(uid)); @@ -196,10 +191,7 @@ void PrivilegeDb::GetPkgPrivileges(const std::string &pkgId, uid_t uid, std::vector ¤tPrivileges) { try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::EGetPkgPrivileges)); - - command->Reset(); + auto &command = getQuery(QueryType::EGetPkgPrivileges); command->BindString(1, pkgId.c_str()); command->BindInteger(2, static_cast(uid)); @@ -214,10 +206,7 @@ void PrivilegeDb::GetPkgPrivileges(const std::string &pkgId, uid_t uid, void PrivilegeDb::RemoveAppPrivileges(const std::string &appId, uid_t uid) { try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::ERemoveAppPrivileges)); - - command->Reset(); + auto &command = getQuery(QueryType::ERemoveAppPrivileges); command->BindString(1, appId.c_str()); command->BindInteger(2, static_cast(uid)); if (command->Step()) { @@ -233,10 +222,7 @@ void PrivilegeDb::UpdateAppPrivileges(const std::string &appId, uid_t uid, const std::vector &privileges) { try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::EAddAppPrivileges)); - - command->Reset(); + auto &command = getQuery(QueryType::EAddAppPrivileges); command->BindString(1, appId.c_str()); command->BindInteger(2, static_cast(uid)); @@ -255,10 +241,7 @@ void PrivilegeDb::GetPrivilegeGroups(const std::string &privilege, std::vector &groups) { try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::EGetPrivilegeGroups)); - - command->Reset(); + auto &command = getQuery(QueryType::EGetPrivilegeGroups); command->BindString(1, privilege.c_str()); while (command->Step()) { @@ -272,10 +255,7 @@ void PrivilegeDb::GetPrivilegeGroups(const std::string &privilege, void PrivilegeDb::GetUserApps(uid_t uid, std::vector &apps) { try_catch([&] { - DB::SqlConnection::DataCommandAutoPtr &command = - m_commands.at(static_cast(QueryType::EGetUserApps)); - - command->Reset(); + auto &command = getQuery(QueryType::EGetUserApps); command->BindInteger(1, static_cast(uid)); apps.clear(); while (command->Step()) { -- 2.7.4