Rewrite checking if row exists when deleting from db
authorZofia Abramowska <z.abramowska@samsung.com>
Mon, 21 Jul 2014 12:51:13 +0000 (14:51 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:59:16 +0000 (14:59 +0200)
Change-Id: Id953004b2026b649505cada853413232f23519db

src/manager/service/ckm-logic.cpp
src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h
tests/test_db_crypto.cpp

index 948411f..0ed8a43 100755 (executable)
@@ -249,31 +249,16 @@ RawBuffer CKMLogic::removeData(
     int retCode = CKM_API_SUCCESS;
 
     if (0 < m_userDataMap.count(cred.uid)) {
-       // check if the data exists or not
-        DBCrypto::DBRowOptional row_optional;
-        if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
-            row_optional = m_userDataMap[cred.uid].database.getDBRow(alias, cred.smackLabel, dataType);
-        } else if ((static_cast<int>(dataType) >= static_cast<int>(DBDataType::DB_KEY_FIRST))
-                && (static_cast<int>(dataType) <= static_cast<int>(DBDataType::DB_KEY_LAST)))
-        {
-            row_optional = m_userDataMap[cred.uid].database.getKeyDBRow(alias, cred.smackLabel);
-        } else {
-            LogError("Unknown type of requested data" << (int)dataType);
-            retCode = CKM_API_ERROR_BAD_REQUEST;
-        }
-        if(!row_optional) {
-            LogError("No row for given alias, label and type");
-            retCode = CKM_API_ERROR_DB_ALIAS_UNKNOWN;
-        }
-
-        // remove if the data exists
-        if(retCode == CKM_API_SUCCESS) {
-            Try {
-                m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
-            } Catch (CKM::Exception) {
-                LogError("Error in deleting row!");
-                retCode = CKM_API_ERROR_DB_ERROR;
+        Try {
+            auto erased = m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
+            // check if the data existed or not
+            if(!erased) {
+                LogError("No row for given alias and label");
+                retCode = CKM_API_ERROR_DB_ALIAS_UNKNOWN;
             }
+        } Catch (CKM::Exception) {
+            LogError("Error in deleting row!");
+            retCode = CKM_API_ERROR_DB_ERROR;
         }
     } else {
         retCode = CKM_API_ERROR_DB_LOCKED;
index daced42..b151c02 100644 (file)
@@ -78,6 +78,10 @@ namespace {
             //                                       1
             "SELECT label FROM CKM_TABLE WHERE alias=? AND restricted=0;";
 
+    const char *select_count_rows_cmd =
+            //                                   1           2
+            "SELECT COUNT(alias) FROM CKM_TABLE WHERE alias=? AND label=?;";
+
     const char *select_key_alias_cmd =
             //                                   1
             "SELECT * FROM CKM_TABLE WHERE alias=?"
@@ -409,19 +413,34 @@ using namespace DB;
                 "Couldn't get key aliases for label " << label);
     }
 
-    void DBCrypto::deleteDBRow(
+    int DBCrypto::countRows(const Alias &alias, const std::string &label) {
+        SqlConnection::DataCommandUniquePtr checkCmd =
+                    m_connection->PrepareDataCommand(select_count_rows_cmd);
+        checkCmd->BindString(1, alias.c_str());
+        checkCmd->BindString(2, label.c_str());
+        if(checkCmd->Step()) {
+            return checkCmd->GetColumnInteger(0);
+        } else {
+            LogDebug("Row does not exist for alias=" << alias << "and label=" << label);
+            return 0;
+        }
+    }
+    int DBCrypto::deleteDBRow(
             const Alias &alias,
             const std::string &label)
     {
         Try {
             Transaction transaction(this);
-            SqlConnection::DataCommandUniquePtr deleteCommand =
-                    m_connection->PrepareDataCommand(delete_alias_cmd);
-            deleteCommand->BindString(1, alias.c_str());
-            deleteCommand->BindString(2, label.c_str());
-            deleteCommand->Step();
-            transaction.commit();
-            return;
+            unsigned int count;
+            if((count = countRows(alias, label)) > 0) {
+                SqlConnection::DataCommandUniquePtr deleteCommand =
+                        m_connection->PrepareDataCommand(delete_alias_cmd);
+                deleteCommand->BindString(1, alias.c_str());
+                deleteCommand->BindString(2, label.c_str());
+                deleteCommand->Step();
+                transaction.commit();
+            }
+            return count;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare delete statement");
         } Catch (SqlConnection::Exception::InternalError) {
index 9ff5c10..05e360d 100644 (file)
@@ -77,7 +77,7 @@ namespace CKM {
             void getKeyAliases(
                     const std::string &label,
                     AliasVector &aliases);
-            void deleteDBRow(
+            int deleteDBRow(
                     const Alias& alias,
                     const std::string &label);
 
@@ -158,6 +158,9 @@ namespace CKM {
                     const std::string &alias,
                     const std::string &label);
             bool checkGlobalAliasExist(const std::string& alias);
+            int countRows(
+                    const std::string &alias,
+                    const std::string &label);
             void getSingleType(
                     DBDataType type,
                     const std::string& label,
index 4933e6a..bdd1f76 100644 (file)
@@ -75,7 +75,9 @@ void checkDBIntegrity(const DBRow &rowPattern, DBCrypto &db) {
     alias_duplicate.dataSize = alias_duplicate.data.size();
 
     BOOST_REQUIRE_THROW(db.saveDBRow(alias_duplicate), DBCrypto::Exception::AliasExists);
-    BOOST_REQUIRE_NO_THROW(db.deleteDBRow("alias", "label"));
+    unsigned int erased;
+    BOOST_REQUIRE_NO_THROW(erased = db.deleteDBRow("alias", "label"));
+    BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
 
     DBCrypto::DBRowOptional row_optional;
     BOOST_REQUIRE_NO_THROW(row_optional = db.getDBRow("alias", "label", DBDataType::BINARY_DATA));