PrivilegeDb: don't require pkgId argument for application removal 88/23688/1
authorRafal Krypa <r.krypa@samsung.com>
Tue, 1 Jul 2014 13:00:17 +0000 (15:00 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Tue, 1 Jul 2014 13:00:49 +0000 (15:00 +0200)
Drop second argument (pkgId) from RemoveApplication() method.
Add new public method GetAppPkgId() for getting application's pkgId and
use it inside RemoveApplication().
This is needed because uninstallation request will contain only appId.

Change-Id: Ic7f618a9c223a501e61a167fb7870e22e1926e20
Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
src/server/db/db.sql
src/server/db/include/privilege_db.h
src/server/db/privilege_db.cpp

index cd72162..680c2ed 100644 (file)
@@ -94,7 +94,7 @@ DROP TRIGGER IF EXISTS app_pkg_view_delete_trigger;
 CREATE TRIGGER app_pkg_view_delete_trigger
 INSTEAD OF DELETE ON app_pkg_view
 BEGIN
-    DELETE FROM app WHERE app_id=OLD.app_id AND pkg_id=OLD.pkg_id;
+    DELETE FROM app WHERE app_id=OLD.app_id;
     DELETE FROM pkg WHERE pkg_id NOT IN (SELECT DISTINCT pkg_id from app);
 END;
 
index 4b7aa25..77ab4c2 100644 (file)
@@ -47,6 +47,7 @@ enum class QueryType {
     EAddAppPrivileges,
     ERemoveAppPrivileges,
     EPkgIdExists,
+    EGetPkgId,
 };
 
 class PrivilegeDb {
@@ -59,10 +60,11 @@ private:
     const std::map<QueryType, const char * const > Queries = {
         { QueryType::EGetPkgPrivileges, "SELECT privilege_name FROM app_privilege_view WHERE pkg_name=?"},
         { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name) VALUES (?, ?)" },
-        { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND pkg_name=?" },
+        { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=?" },
         { QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, privilege_name) VALUES (?, ?)" },
         { QueryType::ERemoveAppPrivileges, "DELETE FROM app_privilege_view WHERE app_name=?" },
-        { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" }
+        { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" },
+        { QueryType::EGetPkgId, " SELECT pkg_name FROM app_pkg_view WHERE app_name = ?" },
     };
 
     /**
@@ -114,6 +116,16 @@ public:
     void RollbackTransaction(void);
 
     /**
+     * Return package id associated with a given application id
+     *
+     * @param appId - application identifier
+     * @param[out] pkgId - return application's pkgId
+     * @return true is application exists, false otherwise
+     * @exception DB::SqlConnection::Exception::InternalError on internal error
+     */
+    bool GetAppPkgId(const std::string &appId, std::string &pkgId);
+
+    /**
      * Retrieve list of privileges assigned to a pkgId
      *
      * @param pkgId - package identifier
@@ -138,12 +150,10 @@ public:
      * Remove an application from the database
      *
      * @param appId - application identifier
-     * @param pkgId - package identifier
      * @param[out] pkgIdIsNoMore - return info if pkgId is in the database
      * @exception DB::SqlConnection::Exception::InternalError on internal error
      */
-    void RemoveApplication(const std::string &appId, const std::string &pkgId,
-            bool &pkgIdIsNoMore);
+    void RemoveApplication(const std::string &appId, bool &pkgIdIsNoMore);
 
     /**
      * Remove privileges assigned to application
index d0dcd6f..f407c6b 100644 (file)
@@ -111,6 +111,23 @@ bool PrivilegeDb::PkgIdExists(const std::string &pkgId)
     });
 }
 
+bool PrivilegeDb::GetAppPkgId(const std::string &appId, std::string &pkgId)
+{
+    return try_catch<bool>([&] {
+        DB::SqlConnection::DataCommandAutoPtr command =
+            mSqlConnection->PrepareDataCommand(Queries.at(QueryType::EGetPkgId));
+        command->BindString(1, appId.c_str());
+
+        if (!command->Step()) {
+            // No application with such appId
+            return false;
+        }
+
+        pkgId = command->GetColumnString(0);
+        return true;
+    });
+}
+
 void PrivilegeDb::AddApplication(const std::string &appId,
         const std::string &pkgId, bool &pkgIdIsNew)
 {
@@ -135,15 +152,20 @@ void PrivilegeDb::AddApplication(const std::string &appId,
 }
 
 void PrivilegeDb::RemoveApplication(const std::string &appId,
-        const std::string &pkgId, bool &pkgIdIsNoMore)
+        bool &pkgIdIsNoMore)
 {
     try_catch<void>([&] {
+        std::string pkgId;
+        if (!GetAppPkgId(appId, pkgId)) {
+            pkgIdIsNoMore = false;
+            return;
+        }
+
         DB::SqlConnection::DataCommandAutoPtr command =
                 mSqlConnection->PrepareDataCommand(
                         Queries.at(QueryType::ERemoveApplication));
 
         command->BindString(1, appId.c_str());
-        command->BindString(2, pkgId.c_str());
 
         if (command->Step()) {
             LogPedantic("Unexpected SQLITE_ROW answer to query: " <<
@@ -151,7 +173,7 @@ void PrivilegeDb::RemoveApplication(const std::string &appId,
         };
 
         command->Reset();
-        LogPedantic( "Removed appId: " << appId << ", pkgId: " << pkgId);
+        LogPedantic( "Removed appId: " << appId);
 
         pkgIdIsNoMore = !(this->PkgIdExists(pkgId));
     });