Ensure async thread join
[platform/core/security/key-manager.git] / misc / ckm_db_tool / ckm-logic-ext.cpp
index 2eba078..2cb55d1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 
 #include "ckm-logic-ext.h"
 
-#include "db-crypto-ext.h"
+#include <dpl/db/sql_connection.h>
+
+namespace {
+
+const char *DB_CMD_OBJECT_SELECT = "SELECT * FROM [join_name_object_tables];";
+
+const char *DB_CMD_NAME_SELECT_BY_IDX = "SELECT name, label FROM NAMES WHERE idx = ?001;";
+
+} // anonymous namespace
 
 namespace CKM {
 
-DB::SqlConnection::Output CKMLogicExt::Execute(uid_t user,
-               const std::string &cmd)
+DB::SqlConnection::Output CKMLogicExt::Execute(uid_t user, const std::string &cmd)
 {
-       /*
-        * We need to access to DB::Crypto::m_connection to call Execute() on it. We don't want to mess
-        * with DB::Crypto too much so adding a friend and extending public interface was not an option.
-        * That's why we need a derived class DB::CryptoExt. m_userDataMap must be left unchanged after
-        * this operation but DB::Crypto can't be copied. According to C++ standard static casting
-        * DB::Crypto pointer to DB::CryptoExt pointer is UB. Therefore DB::Crypto is temporarily moved
-        * into DB::CryptoExt and moved back to m_userDataMap after the call to Execute().
-        */
-       DB::CryptoExt db(std::move(m_userDataMap[user].database));
-
-       try {
-               DB::SqlConnection::Output output = db.Execute(cmd);
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               return output;
-       } catch (const DB::SqlConnection::Exception::Base &e) {
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               throw;
-       }
+       DB::SqlConnection::Output out;
+       m_userDataMap[user].database.m_connection->ExecCommand(&out, "%s", cmd.c_str());
+       return out;
 }
 
 DB::RowVector CKMLogicExt::getRows(uid_t user)
 {
-       DB::CryptoExt db(std::move(m_userDataMap[user].database));
-
-       try {
-               DB::RowVector output = db.getRows();
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               return output;
-       } catch (const DB::SqlConnection::Exception::Base &e) {
-               m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
-               throw;
-       }
+       DB::Crypto& database = m_userDataMap[user].database;
+       DB::SqlConnection::DataCommandUniquePtr selectCommand =
+               database.m_connection->PrepareDataCommand(DB_CMD_OBJECT_SELECT);
+
+       DB::RowVector rows;
+       while (selectCommand->Step())
+               rows.push_back(database.getRow(selectCommand));
+       return rows;
 }
 
 void CKMLogicExt::saveRow(uid_t user, const DB::Row &row)
@@ -67,6 +57,32 @@ void CKMLogicExt::saveRow(uid_t user, const DB::Row &row)
        m_userDataMap[user].database.saveRow(row);
 }
 
+NameEntry CKMLogicExt::getNameByIdx(uid_t user, int idx)
+{
+       DB::SqlConnection::DataCommandUniquePtr selectCommand =
+               m_userDataMap[user].database.m_connection->PrepareDataCommand(DB_CMD_NAME_SELECT_BY_IDX);
+       selectCommand->BindInteger(1, idx);
+
+       if (!selectCommand->Step())
+               ThrowErr(Exc::DatabaseFailed, "getting name from database failed");
+
+       NameEntry name;
+       name.name = selectCommand->GetColumnString(0);
+       name.owner = selectCommand->GetColumnString(1);
+       return name;
+}
+
+int CKMLogicExt::readDataHelperProtected(
+       const Credentials &cred,
+       DataType dataType,
+       const Name &name,
+       const ClientId &explicitOwner,
+       const Password &password,
+       Crypto::GObjUPtr &obj)
+{
+       return readDataHelper(false, cred, dataType, name, explicitOwner, password, obj);
+}
+
 } // namespace CKM