Reorganize project structure and RPM packages
[platform/core/security/key-manager.git] / misc / ckm_db_tool / ckm-logic-ext.cpp
1 /*
2  *  Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       ckm-logic-ext.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include "ckm-logic-ext.h"
23
24 #include "db-crypto-ext.h"
25
26 namespace CKM {
27
28 DB::SqlConnection::Output CKMLogicExt::Execute(uid_t user,
29                 const std::string &cmd)
30 {
31         /*
32          * We need to access to DB::Crypto::m_connection to call Execute() on it. We don't want to mess
33          * with DB::Crypto too much so adding a friend and extending public interface was not an option.
34          * That's why we need a derived class DB::CryptoExt. m_userDataMap must be left unchanged after
35          * this operation but DB::Crypto can't be copied. According to C++ standard static casting
36          * DB::Crypto pointer to DB::CryptoExt pointer is UB. Therefore DB::Crypto is temporarily moved
37          * into DB::CryptoExt and moved back to m_userDataMap after the call to Execute().
38          */
39         DB::CryptoExt db(std::move(m_userDataMap[user].database));
40
41         try {
42                 DB::SqlConnection::Output output = db.Execute(cmd);
43                 m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
44                 return output;
45         } catch (const DB::SqlConnection::Exception::Base &e) {
46                 m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
47                 throw;
48         }
49 }
50
51 DB::RowVector CKMLogicExt::getRows(uid_t user)
52 {
53         DB::CryptoExt db(std::move(m_userDataMap[user].database));
54
55         try {
56                 DB::RowVector output = db.getRows();
57                 m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
58                 return output;
59         } catch (const DB::SqlConnection::Exception::Base &e) {
60                 m_userDataMap[user].database = std::move(*static_cast<DB::Crypto *>(&db));
61                 throw;
62         }
63 }
64
65 void CKMLogicExt::saveRow(uid_t user, const DB::Row &row)
66 {
67         m_userDataMap[user].database.saveRow(row);
68 }
69
70 } // namespace CKM
71
72